美文网首页
SQL语句基本用法

SQL语句基本用法

作者: 丛鹏 | 来源:发表于2019-12-11 15:35 被阅读0次

    一、SQL 分类

    数据定义语言(DDL):用来定义数据库对象 ==> 数据库、表、列等。关键字:create、alter、drop 等

    数据操作语言(DML):用来对数据库中表的记录进行更新。关键字:insert、delete、update 等

    数据控制语言(DCL):用来定义数据库的访问权限和安全级别以及创建用户

    数据查询语言(DQL):用来查询数据库中表的记录。关键字:select、from、where 等

    常用数据类型

    二、数据库操作

    1、创建数据库

    create database 数据库名;    #默认编码创建

    create database 数据库名 character set 字符集;    #指定编码创建

    2、查看数据库

    show databases;    #查看所有数据库

    show database 数据库名;    #查看某个数据库定义的信息

    select database();    #查看正在使用的数据库

    3、删除数据库

    drop database 数据库名;    #删除指定数据库

    4、切换数据库

    use 数据库名;    #切换指定数据库

    三、表操作

    1、创建表

    在创建表之前,先要指定在哪个数据库中创建表,即先使用数据库,使用:use 数据库名;来指定

    #创建表

    create table 表名(

        字段名 类型(长度) 约束,

        字段名 类型(长度) 约束

    );

    eg:

    #创建学生表

    CREATE TABLE S(

        Sno char(9) primary key,    #主键约束

        Sname varchar(8) not null,

        Ssex char(2)

    );

    2、主键约束

    主键是用于标识当前记录的字段,是飞空的、唯一的,在开发过程中,主键是不具备任何含义,只是用于标识当前记录,创建主键有两种方法,一种是在创建表时,在字段后面加上 primary key;另一种是在创建表时,在表创建的最后来指定主键。

    (1) 创建主键

    create table tablename(

        id int primary key,    #字段后面加上主键约束

        ....

    )

    在字段后面加上 primary key

    create table tablename(

        id int,

        ....

        primary key(id)    #指定主键

    )

    在表的最后指定主键

    (2) 删除主键

    alter table 表名 drop primary key;

    alter table tablename drop primary key;

    3、查看表

    查看数据库中所有表

    show tables;

    查看表结构:

    desc 表名;

    4、删除表

    drop table 表名;

    5、修改表结构

    (1) 修改表添加列

    alter table 表名 add 列名 类型(长度) 约束;

    eg:为学生表 S 添加新的字段 Sage smallint

    alter table S add Sage smallint;

    (2) 修改表列的类型和长度以及约束

    alter table 表名 modify 列名 类型(长度) 约束;

    eg:对学生表的 Sname 以及其长度和约束进行修改

    alter table S modify Sname varchar(50) not null;

    (3) 修改表列名

    alter table 表名 change 旧列名 新列名 类型(长度) 约束;

    eg:对学生表的 Sname 列名修改为 Name varchar(20)

    alter table S change Sname Name varchar(20);

    (4) 修改表删除列

    alter table 表名 drop 列名;

    eg:对学生表的 Sage 进行删除

    alter table S drop Sage;

    (5) 修改表名

    rename table 表名 to 新表名;

    eg:修改表名 S 为 Student

    rename table S to Student;

    (6) 修改表的字符集

    alter table 表名 character set 字符集;

    eg:将 Student 表编码表改为 gbk  

    alter table Student character set gbk;

    6、插入表数据

    语法:

    insert into 表(列名1,列名2,列名3...) values (值1,值2,值3....);         #向表中插入某些列

    insert into 表(值1,值2,值3...);       #向表中插入所有列

    eg:往学生表中插入数据

    insert into Student(Sno,Sname,Ssex) values('15450132','一颗星','男');

    insert into Student(Sno,Sname,Ssex) values('15450133','两颗星','男');

    insert into Student values('15450134','三颗星','男');

    insert into Student values('15450135','四颗星','男');

    注意:

    插入的数据应与对应的数据类型相同

    数据的大小应在列的长度范围内

    在 values 中列出的数据位置必须与被加入列的排列位置对应

    除了数值类型外,其他的字段类型的值必须使用引号引起

    如果要插入空值,可以不写字段或者插入 null

    对于自动增长的列操作时,直接插入 null 值即可

    7、更新表数据

    语法:

    update 表名 set 字段名=值,字段名=值;

    update 表名 set 字段名=值,字段名=值 where 条件;

    eg:修改 Student 学生表中的 Sname 中的值

    update Student set Sname='星星';

    update Student set Sname='派大星' where Sno='15450132';

    注意:

     列名的类型与修改的值要一致

    修改值的时候不能超过最大长度

    值如果是字符串或者日期需要加 ‘ ’

    8、删除表数据

    语法:

    delete from 表名 [where 条件];

    truncate table 表名;

    eg:删除表中数据

    delete from Student where Sname='派大星';    #删除一条数据

    truncate table Student;    #删除表中所有数据

    delete from Student;    #删除表中所有数据

    四、查询语句

    1、基本查询

    (1) 查询指定字段

    select 字段1,字段2,...from 表名;

    select Sno,Sname from Student;

    (2) 查询表中所有字段

    select * from 表名;

    select * from Student;

    (3) 除去重复记录查询

    select distinct 字段 from 表名;

    select distinct Sname from Student;

    (4) 别名查询

    别名可以给表中的字段,表设置别名,在查询语句复杂的时候,使用别名极大的简便操作

    select * from 表名 as 别名;

    select * from 表名 别名;

    select 字段名 as 别名 from 表名;

    select 字段名 别名 from 表名;

    2、排序查询

    使用 order by 进行升序降序排序

    select * from 表名 order by 字段 ASC;        #升序(默认)

    select * from 表名 order by 字段 DESC;        #降序

    select * from Student order by Sage asc;    #升序

    select * from Student order by Sage desc;    #降序

    3、聚合查询

    聚合函数查询是纵向查询,它是对一列的值进行计算,然后返回一个单一的值,聚合函数会忽略空值,列出五个常用的聚合函数。

    count:统计指定列不为NULL的记录行数

    sum:计算指定列的数值和

    max:计算指定列的最大值

    min:计算指定列的最小值

    avg:计算指定列的平均值

    #查询学生表中有多少条记录

    select count(*) from Student;

    #查询学生表中年龄大于15岁的有多少条记录

    select count(*) from Student where Sage>15;

    #查询学生表中所有年龄的总和

    select sum(Sage) from Student;

    #查询学生表中最大和最小的年龄

    select max(Sage),min(Sage) from Student;

    #查询学生表中所有年龄的平均值

    select avg(Sage) from Student;

    4、分组查询

    分组查询是指使用group by字句对查询信息进行分组,格式如下:

    select 字段1,字段2......from 表名 group by 字段 having 条件;

    eg:对学生表分姓名统计,求出每个学生对应姓名的年龄

    select Sname,sum(Sage) from Student group by Sname;

    相关文章

      网友评论

          本文标题:SQL语句基本用法

          本文链接:https://www.haomeiwen.com/subject/stoogctx.html