DDL——操作数据库、表等
show databases;
create database db1;
create database if not exists db1;
drop database db1;
drop database if exists db1;
use db1;
select database();
show tables;
desc tab1;
create table tab1(
id int,
name varchar(20)
);
create table if not exists tab1(
id int,
name varchar(20)
);
drop table tab1;
drop table if exists tab1;
alter table tab1 rename to tab2;
alter table tab1 add score double(5,2);
alter table tab1 modify name char(10);
alter table tab1 change name newname char(10);
alter table tab1 drop score;
sql中的数据类型
- 数值
- 日期
- 字符串
char(10):定长,存储性能高,浪费空间
varchar(10):变长,存储性能低,节省空间
DML——对表中的数据进行增删改
insert into tab1(id,score) values(1,99);
insert into tab1(id,score) values(1,99),(2,98);
insert into tab1 values(1,'a',99);
insert into tab1 values(1,'a',99),(2,'b',98);
update tab1 set score=0;
update tab1 set score=99,name='a' where id=1;
delete from tab1;
delete from tab1 where id=1;
DQL——对表中的数据进行查询
select * from tab1;
select id,score from tab1;
select distinct id from tab1;
select distinct * from tab1;
select id,score as 分数 from tab1;
select * from stu where age > 20;
select * from stu where age >= 20;
select * from stu where age >= 20 and age <= 30;
select * from stu where age between 20 and 30;
select * from stu where age > 20;
select * from stu where hire_day between '2001-09-01' and '2022-01-01';
select * from stu where age = 20;
select * from stu where age != 20;
select * from stu where age <> 20;
select * from stu where age = 20 or age = 18 or age = 22;
select * from stu where age in (18, 20, 22);
select * from stu where score is null;
select * from stu where name like 'a%';
select * from stu where name like '_a%';
select * from stu where name like '%a%';
select id,score from tab1 order by id asc,score desc;
select * from stu order by math desc, english asc;
select count(id) from stu;
select count(*) from stu;
select max(math) from stu;
select min(math) from stu;
select sum(math) from stu;
select avg(math) from stu;
select sex,avg(math) from stu group by sex;
select sex,avg(math),count(*) from stu group by sex;
select sex,avg(math),count(*) from stu where math > 70 group by sex;
select sex,avg(math),count(*) from stu where math > 70 group by sex having count(*) > 2;
select * from stu limit 0,3;
select * from stu limit 0,3;
select * from stu limit 3,3;
select * from stu limit 6,3;
DCL——对数据库进行权限控制
约束
create table emp(
id int primary key auto_increment,
ename varchar(20) not null unique,
joindate date not null,
salary double(8,2) not null,
bonus double(8,2) default 0
);
alter table emp modify ename varchar(20) not null;
alter table emp modify ename varchar(20) unique;
alter table emp add primary key(id);
alter table emp alter bonus set default 0;
alter table emp modify ename varchar(20);
alter table emp drop index ename;
alter table emp drop primary key;
alter table emp alter bonus drop default;
create table dept(
id int primary key auto_increment,
dep_name varchar(20),
addr varchar(20)
);
create table emp(
id int primary key auto_increment,
name varchar(20),
age int,
dep_id int,
constraint fk_emp_dept foreign key(dep_id) references dept(id)
);
alter table drop foreign key fk_emp_dept;
alter table emp add constraint fk_emp_dept foreign key(dep_id) references dept(id);
数据库设计
表
字段
表关系
表关系
1. 一对一
人 身份证
用户 用户详情
实现方式:在任意一方加入外键,关联另一方的主键,并且设置外键为唯一
create table tb_user(
id int primary key auto_increment,
nickname varchar(20),
age int
);
create table tb_user_desc(
id int primary key auto_increment,
city varchar(20),
gender varchar(20)
tb_user_id int unique
);
alter table tb_user_desc add constraint fk_desc_user foreign key(tb_user_id) references tb_user(id);
2. 一对多(多对一)
部门 员工
实现方式:在多的一方建立外键,指向一的一方的主键
create table dept(
id int primary key auto_increment,
dep_name varchar(20),
addr varchar(20)
);
create table emp(
id int primary key auto_increment,
name varchar(20),
age int,
dep_id int
);
alter table emp add constraint fk_emp_dept foreign key(dep_id) references dept(id);
3. 多对多
商品 订单
实现方式:建立第三张中间表,中间表至少包含两个外键,分别关联两方主键
create table tb_order(
id int primary key auto_increment,
payment double(10,2),
payment_type tinyint,
status tinyint
);
create table tb_goods(
id int primary key auto_increment,
title varchar(20),
price double(10,2)
);
create table tb_order_goods(
id int primary key auto_increment,
order_id int,
goods_id int,
count int
);
alter table tb_order_goods add constraint fk_order_id foreign key(order_id) references tb_order(id);
alter table tb_order_goods add constraint fk_goods_id foreign key(goods_id) references tb_goods(id);
多表查询
select * from table1,table2;
连接查询
内连接
外连接
左外连接
右外连接
子查询
create table dept(
id int primary key auto_increment,
dep_name varchar(20),
addr varchar(20)
);
create table emp(
id int primary key auto_increment,
name varchar(20),
age int,
dep_id int
);
alter table emp add constraint fk_emp_dept foreign key(dep_id) references dept(id);
连接查询
select 字段列表 from table1,table2,... where 条件 and 条件 and ...;
select t1.name, t2.dep_name from emp as t1, dept as t2 where t1.dep_id = t2.id;
select 字段列表 from table1 [inner] join table2 on 条件
[inner] join table2 on 条件
...;
select * from emp inner join dept on emp.dep_id = dept.id;
select * from emp join dept on emp.dep_id = dept.id;
select 字段列表 from table1 left [outer] join table2 on 条件;
select * from emp left outer join dept on emp.dep_id = dept.id;
select * from emp left join dept on emp.dep_id = dept.id;
select 字段列表 from table1 right [outer] join table2 on 条件;
select * from emp right outer join dept on emp.dep_id = dept.id;
select * from emp right join dept on emp.dep_id = dept.id;
子查询
单行单列
多行单列
多行多列
select 字段列表 from tab where 字段名 = (子查询);
select 字段列表 from tab where 字段名 in (子查询);
select 字段列表 from (子查询) where 条件;
select * from emp where age > (select age from emp where name = 'aaa');
select * from emp where dep_id in (select id from dept where dep_name in ('部门1','部门2'));
select * from (select * from emp where age > 20) as t1, dept where t1.dep_id = dept.id;
事务
开启事务
start transaction;
或begin;
无异常提交事务
commit;
出异常则回滚事务
rollback;
begin;
update account set money = money - 500 where name = 'a';
update account set money = money + 500 where name = 'b';
commit;
rollback;
事务的四大特征(ACID)
原子性(Atomicity):
事务是不可分割的最小操作单位,要么同时成功,要么同时失败
一致性(Consistency):
事务完成时,必须使所有的数据都保持一致
隔离性(Isolation):
多个事务之间,操作的可见性
持久性(Durability):
事务一旦提交或回滚,他对数据库中的数据的改变就是永久的
select @@autocommit;
set @@autocommit = 0;
set @@autocommit = 1;