启动数据库
mysql -u root -p
创建数据库
create database test;
展示数据库
show databases;
选择数据库
use test;
创建表
create table tep( name varchar(10), sex char(1), dirth date );
展示表
show tables;
展示信息
describe pet;
插入数据
insert into tep values( 'qwq','o','20220202' );
查询
select * from pet;
删除
delete from tep where name='aaa';
修改
update tep set name='qqq' where dirth='20220812';
六大约束
主键
create table user( id int primary key, name varchar(20) );
联合主键
create table user2 ( id int, name varchar(20), password varchar(10), primary key(id,name) );
自增约束
create table user3( id int primary key auto_increment, name varchar(10) );
添加主键
create table user4( id int, name varchar(10) );
alter table user4 add primary key(id);
alter table user4 drop primary key;
alter table user4 modify id int primary key;
create table user5( id int, name varchar(10) );
create table user5( id int, name varchar(10), unique(id,name) );
create table user5( id int, name varchar(10) unique );
alter table user5 add unique(name);
alter table user5 drop index name;
alter table user5 modify name varchar(10) unique;
create table user9( id int, name varchar(10) not null );
create table user10( id int, name varchar(10), age int default 20 );
外键约束:
涉及两个表:子表,父表
主表,副标
create table classes( id int primary key, name varchar(20) );
mysql> create table students( id int primary key, name varchar(10), class_id int, foreign key(class_id) references classes(id) );
foreign key(class_id) references classes(id)
主表中没有的数据在副表中是不能使用的
主表中的记录在副表中被引用,在主表中是不能被删除的
数据库的三大范式
第一范式:拆字段
范式设计的越详细对于某些操作越好,但不一定都很好
第二范式:
在满足第一范式的前提下,第二范式要求,除主键外的每一列都依赖与主键,如果出现不完全以来只可能出现在联合主键下。
第三范式:
必须满足第二范式,除主键外,其他列不能右传递 依赖
查询练习:
select 查询的东西或者求的值 from 表
where 条件 group by 查询的条件 order by 顺序
distinct
关键字
select distinct depart from teacher;
查询在60但80中间的分数
select * from score where degree between 60 and 80;
select * from score where degree > 60 and degree <80;
in
关键字
select * frrom score where degree in(90,80,70);
or
关键字
select * from student where class = '111' or ssex = 'nan';
desc
asc
select * from students order by class desc;
con
升序degree
降序
select * from score order by cno asc,degree desc;
count
关键字
select count(*) from student where class = '111';
子查询
max
关键字
select con , son from score where
degree =(select max(degree) from sore);
limit
关键字
select son,con from score order by degree limit 0,1;
avg
关键字
select avg(segree) from score where con = '111';
group by
分组
select avg(degree) from score group by cno;
having
,like
not like
select con,avg(degree) from score
group by con
having count(cno)>=2
and con like '3%';
多表查询
select sname, con, degree from ,score
where student.son=score.son;
select sname, cname, degree from student, course, score
where student.son=score.son and course.con=score.con;
查询班级111的课程平均成绩
select con, avg(degree)
from score
where sno in(select sno from student where class='111')
group by con;
查询3-105中成绩大于109号的人
select * from score where con ='3-105' and
degree>(select degree from score where son='109'
and con = '3-105');
选课人数大于5的老师
select tname from teacher where tno =
(select ton from course where con = (
select con from score group by cno having count(*)>5));
all
any
year(now)
获得当前时间
union
链接连个sql
句子
as
取别名
四种链接查询
- 内连接
inner join
select * from person inner join card on person.cardid=card.id;
左连接 left join
2. 左边的表不变
select * from person left join card on person.cardid=card.id;
- 右连接
right join
右边的表不变
select * from person right join card on person.cardid=card.id;
- 全外连接
full join
mysql不支持
select * from person full join card on person.cardid=card.id;
数字函数
函数名 | 描述 | 实例 |
---|---|---|
abs(x) | 返回x的绝对值 | select abs(x) |
acos(x) | 求 x 的反余弦值(单位为弧度),x 为一个数值 | select acos(0.25) |
avg(expression) | 返回一个表达式的平均值,expression 是一个字段 | 返回 Products 表中Price 字段的平均值 : select avg(Price) as AveragePrice from Products |
ceil(x) | 返回大于或等于 x 的最小整数 | select ceil(1.5) |
count(expression) | 返回查询的记录总数,expression 参数是一个字段或者 * 号 | 号返回 Products 表中 products 字段总共有多少条记录:`select count(ProductID) as NumberOfProducts from Products`` |
floor(x) | 返回小于或等于 x 的最大整数 | 小于或等于 1.5 的整数:select floor(1.5) – 返回1 |
max(expression) | 返回字段 expression 中的最大值 | 返回数据表 Products 中字段 Price 的最大值:select max(Price) as LargestPrice from Products ; |
round(x) | 返回离 x 最近的整数 | select eound(3.1415926) |
sum(expression) | 返回指定字段的总和 | 计算 OrderDetails 表中字段 Quantity 的总和:select sum(Quantity) as TotalItemsOrdered from OrderDetails |
truntace(x,y) | 返回数值 x 保留到小数点后 y 位的值(与 ROUND 最大的区别是不会进行四舍五入) | selectr truncate(1.23456,3) – 1.234 |
事务
begin
开启事务
commit
持久化
bollback
回滚