SQL基础语句

不区分大小写,但建议关键字大写;语句最好用分号结尾;根据需要进行缩进换行注释:
* 单行注释 #或者-- 注释
* 多行注释 /* 注释 */

1、查看当前所有的数据库show databases;2、打开指定的库use 库名3、查看当前库的所有表show tables;4、查看其它库的所有表show tables from 库名;5、创建一个表create table 表名( 列名 类型 id int, name varchar(20));6、查看表结构desc 表名;7、查看数据库版本select version();---------(表操作)建表:重点:
* create table 表名(

            列名  类型,                列名  类型            );
* 添加主键:primary key (列名),
* 添加外键:foreign key (列名) references 表名(列名),
* 添加外键等操作时,使用到的列名要加括号!

删表:重点:
* drop table 表名;

改表:重点:
* 在表内添加字段: alter table 表名 add 列名(属性);
* 在表内删除字段: alter table 表名 drop 列名(属性);

查表:重点:
* select * from 表名;

---------(表内容操作)*****重点!!!!添加:重点:
* insert into 表名 (属性,属性) value (内容,内容);------------------属性如不添加,则默认要按顺序添加所有的内容
* insert into major(mno,mname) value(2,'软件工程');

删除:重点:
* delete from 表名 where 属性=值;------------------如不添加限制条件,则默认删除所有
* delete from stu where sno=1;

修改:重点:
* updata 表名 set 属性=值 where 属性=值'
* update stu set sname="张锦波" where sex="男";

查询:基础:
* select

单表查询: 基础查询方法 distinct去重 like模糊搜索 in(条件1,条件2)在条件范围内
* select * from sc;
* -- 1、查询全部学生的全部信息
* -- 2、查询全部学生的学号sno和姓名sname
* -- 3、查询全部学生的姓名和出生年份
* -- 4、查询有选修课程的学生学号,需要去重
* -- 5、查询姓名是‘小十’的学生的全部信息
* -- 6、查询课号是20201且学生成绩高于80的学生学号
* -- 7、查询年龄在18-19之间的学生姓名
* -- 8、查询专业号为01,02,04的学生信息
* -- 9、查询专业号不是01,02,04的学生信息
* -- 10、查询所有姓彭的学生信息
* -- 11、查询名字带有"小"字的学生信息
* -- 12、查询名字中第二字为"小"的学生信息
* -- 13、查询有选课记录但没有考试成绩的选课信息

答案:
1. select * from stu;
2. select sno,sname from stu;
3. select sname,2020-age as birth_date from stu;
4. select distinct sno from sc;
5. select * from stu where sname like "小十";
6. select sno from sc where cno=20201 && grade>80;
7. select sname,age from stu where age between 18 and 19;select sname,age from stu where age>=18 && age<=19;
8. select * from stu where mno in(01,02,04);
9. select * from stu where mno not in(01,02,04);
10. select * from stu where sname like "彭%";
11. select * from stu where sname like "%小%";
12. select * from stu where sname like "_小%";
13. select * from sc where grade is null;

        order by----聚集函数-----group by                                       从低到高 :order by根据条件                                     从高到低: order by 根据条件 desc                                   查询表列数:count(*)                                  查询某记录数:count(条件)                                                                      count统计的是条件中不为空的值的数量                                where是分组前过滤数据,不可包含聚集函数                                having是分组后过滤数据,经常包含聚集函数        order by排序
* -- 1、查询学生成绩,由低到高
* -- 2、查询学生成绩,由高到低

    count()统计
* -- 3、查询学生总人数
* -- 4、查询选修了课程的学生人数

    AVG()平均    MAX()最高    MIN()最低
* -- 5、查询选修20201课程的学生平均成绩
* -- 6、查询选修20201课程的学生最高成绩

    group by根据字段进行分组----------不需要特别加入distinct去重
* -- 7、求各个课程号以及相应的选修人数
* -- 8、查询平均成绩大于等于90的学生学号和平均成绩

答案:
1. select * from sc order by grade;
2. select * from sc order by grade desc;
3. select count(*) from stu;
4. select count(distinct sno) from stu;
5. select AVG(grade) from sc where cno=20201;
6. select MAX(grade) from sc where cno=20201;
7. select cno,count(sno) from sc group by cno;
8. select sno,AVG(grade) from sc group by sno having AVG(grade)>=90;

多表查询: left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录   right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录  inner join(等值连接) 只返回两个表中联结字段相等的行 普通多表 from多表,在where之后添加表关联
* -- 1、查询选修了20201课程的学生名字
* -- 2、查询每个学生的信息和选修课程的信息与学时

        左外链接        from 主表 left outer join 从表 on 表关联
* -- 3、查询所有学生的信息和选课信息,没有选修的学生也要显示出来
* -- 4、查询每个专业的学生人数,假设每个专业都有人(不需要使用链接,使用group by进行分组即可)
* -- 5、查询每个专业的学生人数,有的专业可能没有人(在保证所有专业都在列表的前提下,就需要用到左外连接)

答案:
1. select sname from stu,sc where stu.sno=sc.sno and sc.cno=20201;
2. select stu.,sc.,ctime from stu,sc,cou where stu.sno=sc.sno and sc.cno=cou.cno;
3. select stu.,sc. from stu left outer join sc on stu.sno=sc.sno;
4. select mno,count(sno) from stu group by mno having mno between 1 and 4;
5. select major.mno,count(stu.sno) from major left outer join stu on major.mno=stu.mno group by major.mno;

嵌套查询: 不相关嵌套查询 子查询不依赖父查询 类型(两个不同表):父查询输出信息,子查询进行判断,两者各司其职
* -- 1、查询选修了20201课程的学生名字

        相关嵌套查询        子查询依赖父查询        类型(两个不同表):父查询规范限制条件,子查询输出对应集合进行匹配,子查询需要与父查询建立联系        类型(单表双查询):通过别名来区分同一个表,查找逻辑与上面类似        类型(派生表查询):通过派生表来进行基本的多表查询操作        类型(exists查询):通过exists字句对条件进行判断,在内部需要建立联系,会根据条件是否吻合对应所建立的联系输出true或false
* -- 2、查询选修了20201课程的学生名字
* -- 3、查询选择了C语言课程的学生学号
* -- 4、查询每个学生超过他平均分的课程号
* -- 5、查询每个学生超过他平均分的课程号(派生表实现)
* -- 6、查询选修了20201课程的学生名字,带有exists

答案:
1. select sname from stu where sno in (select sno from sc where cno=20201);
2. select sname from stu where 20201 in (select cno from sc where sc.sno=stu.sno);
3. select sno from sc where "C语言" in (select cname from cou where cou.cno=sc.cno);
4. select sno,cno from sc x where grade>(select AVG(grade) from sc y group by sno having x.sno=y.sno);
5. select sno,cno from sc,(select sno,AVG(grade) from sc group by sno) as avg_sc(avg_sno,avg_grade) where sc.sno=avg_sc.avg_sno and sc.grade>avg_sc.avg_grade;
6. select sname from stu where exists(select * from sc where cno=20201 and stu.sno=sc.sno);

                    集合操作:union并操作、intersect交操作、except差操作    是SQL server 的语法 
* -- 1、查询年龄是18且mno=1的学生学号        union并操作
* -- 2、查询年龄是18且mno=1的学生学号        except差操作
* -- 3、查询选修‘20201’号课程且‘20203’的学生学号        intersect交操作

答案:
1. select sno from stu where age=18 union select sno from stu where mno=1;
2. select sno from stu where age=18 except select sno from stu where mno=1;
3. select sno from sc where cno=20201 intersect select sno from sc where cno=20203;

-- major-- mno mnamecreate table major ( mno int, mname varchar(20), primary key (mno));-- stu-- sno sname age sex mnocreate table stu ( sno int, sname varchar(20), age smallint, sex varchar(2), mno int, primary key(sno), foreign key (mno) references major(mno));-- cou-- cno cname ctime ccreditcreate table cou ( cno int, cname varchar(20), ctime smallint, ccredit decimal(4,2),-- 四位,两个小数点 primary key (cno));-- sc-- sno cno gradecreate table sc( sno int, cno int, grade decimal(5,2), primary key (sno,cno), foreign key (sno) references stu(sno));-- 外键补充alter table sc add constraint fk_sc foreign key(cno) references cou(cno);

上一篇:数据库高级数据库学习--上机练习9-2(触发器)


下一篇:SqlServer2012-创建表、删除表 增加字段 删除字段操作