C:Create(创造) R:Retrieve(查询) U:Update(修改) D:Delete(删除)
注释:在SQL中,注释使用 -- 加空格
-- 创建一张学生表
create table student(
id int,
sn int comment"学号",
name varchar(10) comment "姓名",
qq_mail varchar(20) comment "QQ邮箱"
);
注意:数据类型要与上面类型对应
-- 插入两行数据
insert into student values(100,10000,"张三",NULL);
insert into student values(101,10001,"张四","2222223");
注意:没有数据的部分即为NULL
-- 多行数据插入
insert into student(id,sn,name) values
(001,10002,"八戒"),
(002,10003,"悟空");
3.1全列查询
-- student 是之前创建的表格
select * from student;
3.2指定列查询
select id,sn,qq_emai from student;
3.3查询字段为表达式
-- 1.表达式不包含字段
select id,sn,10 from student;
-- 2.表达式包含一个字段
select id+10,name,qq_mail from student;
-- 3.表达式包含多个字段
select id+sn,name from student;
3.4 别名
select id+sn sum,name from student;
-- 等于select id+sn as sum,name from student;
3.5去重:Distinct
假如math列中有两个数字重复了
-- 在列前加 distinct即可
select distinct math from student;
3.6排序:Order by
-- 1.默认升序 asc加不加都行
select name,id from student order by id;
-- 2.降序
select name,id from student order by id desc;
-- 3.别名降序
-- 先执行as再执行order by,因此可以运行
select name,id+sn as sum from student order by sum desc;
-- 4.按多个字段进行排序时,按照书写顺序排优先级
select math,chinese,english,name from student order by english desc,math,chinese;
--先排英语,英语相同的排数学,数学再相同的排语文