1、创建数据库:
create database myschool;
2、删除数据库:
drop database myschool;
3、创建表:
create table [if not exists] 表名(
字段1 数据类型 [字段属性|约束] [ 索引 ] [ 注释],
……
) [ 表类型 ] [表字符集] [注释];
create table `student`(
`studentNo` int(4) not null comment '学号' primary key, #非空,主键
`name` char(10),
……
) comment="学生表";
4、添加字段
alter table demo02 add `password` varchar(32) not null;
5、修改字段
alter table 表名 change 原字段名 新字段名 数据类型 [ 属性 ];
alter table demo02 change `name` `username` char(10) not null;
6、设置默认值约束
alter table grade alter column gtype set default '一年级';
7、删除字段
alter table 表名 drop 字段名
alter table demo02 drop `password`;
8、添加主键约束
alter table 表名 add constraint 主键名 primary key 表名(主键字段);
alter table `grade` add constraint `pk_grade` primary key `grade`(`gradeId`);
9、添加外键约束
alter table 表名 add constraint 外键名 foreign key (外键字段) references 关联表名(关联字段);
alter table `student` add constraint fk_student_grade foreign key(`gradeId`) references `grade` (`gradeId`);
10、添加检查约束
alter table test add constraint CK_test_tprice check( tprice > = 100);
11、插入数据
insert into `subject`(`subjectName`,`classHour`,`gradeId`)
values('logic java',220,1),('HTML',160,1);
12、将查询结果插入到新表
create table 新表(select 字段1,字段2,……FROM 原表);
create table `phoneList`(select `studentName,`phone` from `student`);
13、更新数据
update 表名 set 列名 = 更新值 [ where 更新条件];
update student set sex = '女' where id = 1;
14、删除数据
delete [ from ] 表名 [ where ];
delete from student where id = 1;
15、使用select语句进行查询
select
from
[where ]
[order by [ASC或DESC] ];
select studentNo AS 学生编号,studentName AS 学生姓名, address AS 学生地址
from student
where id = 1;
或者:
select firstName+'.' + lastName AS 姓名 FROM employee;
16、 查询空值
select studentName from student where email is null ;
17、在查询中使用常量列
select studentName AS 姓名,address AS 地址, '北京' AS 学校名称 from student;
18、 聚合函数
AVG( )
COUNT( )
MAX( )
MIN( )
SUM( )
select sum(studentResult) from result;
select AVG(studentResult) from result;
19、字符串函数
CONCAT(str1,str2,……,strn) select CONCAT('MY','S','QL'); 返回:MYSQL
INSERT(str,pos,len,newstr) select INSERT('这是Oracle数据库',3,6,'MySQL'); 返回:这是MySQL数据库
LOWER(str) select lower('MYSQL'); 返回:mysql
upper(str)
substring(str,num,len) select substring('JavaMySQLOracle',5,5); 返回:MySQL
20、时间日期函数
select datediff(now(),'2008-8-8'); 返回3327,返回日期参数date1和date2之间相隔的天数
select adddate(now(),5);
21、数学函数:
CEIL(x) 返回大于或等于数值x的最小整数 select ceil(2.3) 返回: 3
FLOOR(x) 返回小于或等于数值x的最大整数 select floor(2.3) 返回:2
22、order by 字句
ASC:升序 DESC:降序
例:要在学生成绩排序的基础上,再按照课程ID进行排序的语句如下:
select studentId AS 学生编号,studentResult AS 成绩,courseID AS 课程ID,
from result
whre studentResult>60
order by studentResult,courseID;
23、group by 分组
select
from
[where ]
[group by ]
[order by [ASC或DESC] ]
[LIMIT [位置偏移量,] 行数];
多列分组查询
select count(*) AS 人数,grade AS 年级, sex AS 性别 from student
group by grade ,sex
order by grade;
24、简单子查询
select `studentNo`,`studentName`,`sex`,`bornDate`,`address` from `student`
where `bornDate` >
(select `bornDate` from `student` where `studentName` = '李斯文');
25、IN和NOT IN 子查询
查询Logic Java课程至少一次考试刚好等于60分的名单:
select `studentName` from `student`
where `studentNo` IN(
select `studentNo` from `result`
where `subjectNo` =(
select `subjectNo` from `subject`
where `subjectName` = `Logic Java`
) and `studentResult` = 60
);
26、EXISTS子查询
EXISTS关键字后面的参数是一个任意的子查询,如果该子查询有返回行,则EXISTS子查询的结果为true,此时再执行外层查询语句。如果子查询没有返回行,则EXISTS子查询的结果为false,此时外层语句不再执行查询。
检查Java课程最近一次考试。如果有成绩达到80分以上者,则显示分数排在前5名学员的学号和分数。
select `studentNo` AS 学号,`studentResult` 成绩 from `result`
where exists (
#查询Logic Java最后一次考试成绩大于80的记录
select * from `result` where `subjectNo` = (
select `subjectNo` from `subject` where `subjectName` = 'Logic Java'
) AND `examDate` = (
select MAX(`examDate`) FROM `result` where `subjectNo` = (
select `subjectNo` from `subject`
where `subjectName` = 'Logic Java'
)
) AND `studentResult` > 80
) AND `subjectNo` = ( select `subjectNo` from `subject` where `subjectName` = 'Logic Java')
ORDER BY `studentResult` DESC LIMIT 5;
27、使用HAVING字句对分组后的数据进行筛选
select count(*) AS 人数, gradeId AS 年级 from student
group by gradeId
having count(*) > 2;
28、内连接查询
select student.studentName,result.subjectNo,result.studentResult
from student,result
where student.studentNo = result.studentNo;
或者:
select s.studentName,r.subjectNo,r.studentResult
from student AS s
INNER JOIN result AS r on(s.student.No = r.studentNo);
29、 左外连接查询
select s.studentName,r.subjectNo,r.studentResult
from student AS s
left outer join result as r on s.studentNo = r.studentNo;