数据库创建及管理
管路表
修改表的表名称
alter table student1 rname to student;
查询
查看所有的数据
select *from student;
查询全体学生的学号和姓名
select sno aname from student;
查询选修了课程的学生学号(distinct:表示在查询结果中去掉重复值)
select distinct san from sc;
查询学生表的前三条数据(limit:返回查询结果中的前N行)
select *from student limit 3;==select *from student limit 0,3;
输出全体学生表中第2条记录后的2条记录
select *from student limit 2,2;
查询全体学生的姓名及年龄(curdate()函数返回当前系统日期和时间、year()函数返回指定日期的年部分的整数)
select sname,year(curdate())-year(sbirthday) from student;
查询全体学生的姓名、出生年份、所在院系,同时为姓名列指定别名为姓名,出生年份列指定别名年份,所在院系列指定别名为系别。
select sname 姓名,'出生年份',year(sbirthday) 年份,sdept as 系别 from student;
快速复制表的数据
insert into student(sno,sname,ssex,sbirthday,saddress,sdept,speciality) select sno+2,sname,ssex,sbirthday,saddress,sdept,speciality from student;
insert into teacher(tno,tname,tsex,tbirthday,tdept) select tno+1,tname,tsex,tbirthday,tdept from teacher;
复制表中的两列
create table student1 select sname,sdept from student;
备份表中 的数据到磁盘
select *from student where ssex='女' into outfile 'e:student.txt' fields terminated by ',';
备份表中所以的数据到磁盘
select *from student into outfile 'e:/student1.txt';
将文本文件导入到数据表中
load data infile 'e:/3.txt' into table student;
范围运算符
查询成绩在60-70分的学生学号及成绩
select sno,degree from sc where degree between 60 and 10;
字符匹配符
查询所以姓李的学生信息
select *from student where sname like '李%';
查询生源地不在山东省的所有学生信息
select *from student where saddress not like '%山东%';
查询名字中第二个字为阳的学生的姓名和学号
select sno,sname from student where saddress like '_阳%';
正则表达式
查询家庭地址一‘济’开头的学号是呢过信息
select *from student where saddress regexp '^济';
列表运算符
查询信息工程系、软件工程系、计算机工程系的学生姓名和性别
select sname,ssex from student where sdept in('信息工程系', '软件工程系', '计算机工程系');
涉及空值得查询
查询缺少成绩的学生的学号和课程号
select sno,cno from sc where degree is null;