一、SQL语言
1、DDL
DDL是SQL语言的四大功能之一。用于定义数据库的三级结构,包括外模式、概念模式、内模式及其相互之间的映像,定义数据的完整性、安全控制等约束。DDL不需要commit.
CREATE、ALTER、DROP、TRUNCATE、COMMENT、RENAME
create drop alter show
1.库
1.创建库
create database [if not exists] dbname;
eg:create database [if not exists] CY1269;
2.删除库
drop database [if exists] dbname;
drop database [if exists] CY1269;
3.查看所有库
show databases;
2.表
1.创建表
create table tbname(字段名称 字段类型 [字段约束],字段名称 字段类型 [字段约束],....);
create table stu(
id varchar(10) primary key COMMENT "学号",
name varchar(10) not null COMMENT "姓名",
sex enum("man","woman") COMMENT "性别",
age int COMMENT "年龄"
);
2.删除表
drop table tbname;
3.修改表
1.修改字段类型 modify
alter table stu modify name varchar(50);
2.修改字段名称 change
alter table stu change id sid varchar(20);
3.添加字段 add [after first]
alter table stu add score1 float not null first;
4.删除字段 drop
alter table stu drop score1;
5.修改表名 rename
alter table stu rename student;
4.查看表
1.查看表的创建
show create table tbname;
2.查看字段信息
desc tbname;
学生表:学号、 姓名、性别、年龄
成绩表: 学号、课程编号、成绩
insert into result values("001","p01",78),
("001","p02",56),
("002","p01",100),
("002","p02",88),
("003","p01",34),
("003","p02",99);
create table result
(
sid varchar(10) not null comment '学号',
pid varchar(10) not null comment '课程编号',
score float comment '成绩'
);
CREATE TABLE `teach` (
`id` varchar(10) NOT NULL COMMENT '教师编号',
`name` varchar(10) NOT NULL COMMENT '姓名',
`sex` enum('man','woman') DEFAULT NULL COMMENT '性别',
`age` int(11) DEFAULT NULL COMMENT '年龄',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
主键 primary key
外键 foreign key
唯一 unique
非空 not null
为空 default null
2、DML
insert delete update select
1.插入数据 insert load
insert into tbname[(field1,field2,...)] values(data1,data2,...);
insert into stu values("002","lisi","woman",19),
("003","wangwu","man",20);
insert into stu(id,name) values("004","zhaoliu");
2.删除数据
delete from stu;
delete from stu where id = "001";
3.修改数据 update
update stu set age = 22;
update stu set age = 22 where name = "lisi";
4.查询数据 select
1.普通查询
1.select * from stu;
select id,name,sex,age from stu;
2.select name from stu where age > 20;
2.去重查询 distinct
select distinct age from stu;
3.排序方式 order by asc|desc
select distinct age
from stu
order by age desc;
4.分组查询 group by
select sid,SUM(score) all_score
from result
group by sid;
5.多表查询
查询年龄小于二十岁的学生不及格信息
1.等值查询
select name,score
from stu,result
where id = sid and age < 20 and score <60;
2.连接查询
1.外连接查询
1.左外连接查询
select name,score
from
(select id,name from stu where age < 20) a
left join
(select sid,score from result where score < 60) b
on id= sid
WHERE score is not null;
2.右外连接查询
select name,score
from
(select id,name from stu where age < 20) a
right join
(select sid,score from result where score < 60) b
on id= sid
WHERE name is not null;
3.全外连接查询
select name,score
from
(select id,name from stu where age < 20) a
full join
(select sid,score from result where score < 60) b
on id= sid
WHERE name is not null;
2.内连接查询
1.内连接查询
select name,score
from
(select id,name from stu where age < 20) a
inner join
(select sid,score from result where score < 60) b
on id= sid;
6.联合查询 union | union all
select * from stu
union all
select * from teach;
mysql多select合并_mysql 多个字段合并:
栗子:SELECT sno FROM students WHERE Name = '张三' UNION SELECT teacher FROM teachers WHERE Name = '老师';
3、DCL 权限
grant
grant all on CY1269.* to user1;
revoke
revoke select on CY1269.* from user1;
2.MySQL
存储引擎 索引 事务
存储过程 触发器
1.字段类型 date time datetime
2.函数 count sum year
4.一些栗子
1) select :将多个查询结果合并成一行
效果:以id分组,把name字段的值打印在一行,逗号分隔(默认)
mysql> select id,group_concat(name) from aa group by id;
效果:以id分组,把name字段的值打印在一行,分号分隔
mysql> select id,group_concat(name separator ';') from aa group by id;
效果:以id分组,把去冗余的name字段的值打印在一行,逗号分隔
mysql> select id,group_concat(distinct name) from aa group by id;
2)mysql offset