Mysql知识点总结
- 
查看表 show tables; 
- 
查看表结构 describe pet; 
- 
mysql常用数据类型 
 数值型 int、float、double
 日期 date yyyy-mm-dd
 time hh:mm:ss
 字符串型 char 定长字符串
 varchar 变长字符串
 text 长文本数据
 删除数据 delete from pet where death='3000-12-02';
- 
增 insert into 表名 values(); 
 删 delete
 改 update
 查 select
- 
联合主键 primary key (id,name) 
- 
自增约束 auto_increment 
- 
修改表结构,建表忘记g给id添加主键约束 alter table 表名 add primary key(id); 
 删除主键约束 alter table 表名 drop primary key(id);
- 
添加唯一约束 later table 表名 add unique(id) 
 创建表时直接添加 name varchar(20) unique
 /unique(id,name)
 删除唯一约束 alter table 表名 drop index name;
 利用motify的方式添加 alter table 表名 modify name varchar(20) unique;
- 
默认约束 age int default 18, 
- 
外键约束 foreign key(class_id) references classes(id) 
 子表里面不能添加父表没有的id
 父表的值被子表引用后,是不可以删除的
- 
排重distinct 
- 
排序order by asc | desc 
 以什么升序,以什么降序 cno asc , id desc
- 
在什么什么之间 between and 
 not between and
- 
or或者 ----> in 
 where age in(18,19,20);
- 
limit配合排序 找出最高分 
 select sno,cno from score order by degree desc limit 0,2;
 从0条开始取两条数据
 1,2从1条开始取2条数据
- 
查询score表中至少2名学生选修的并且以3开头的课程的平均分 
 select cno,avg(degree) from score group by cno
 having count(cno)>=2 and cno like '3%' ;
- 
分组查询 group by 里面的条件要用having 而不是where 
 group by class having count(*)>1;
- 
多表查询 
 mysql> select sname,cno,degree from student,score
 -> where student.sno = score.sno;
 三表查询 where ... and ...
- 
求并集union 
 select * from student id='1'
 union
 select * from student id='2';
- 
至少大于其中一个 
 where degree > any(select degree from......);
- 
且大于 
 where degree > all(select degree from......);
- 
连接查询: 
 内连接 inner jion
 左连接 left jion
 右连接 right jion
 全连接 full join(mysql不支持)
- 
select * from person inner jion card on person.cardid = card.id; 
- 
事务默认开启 select @@autocommit; 
 事务回滚 rollback;
- 
自动提交 @@autocommit = 1 
 手动提交 commit;
- 
set autocommit=0; --> 插入一条数据 -->rollback; 可以撤销 
 若手动提交 commit; 则rollback也不可以撤销
- 
事务前手动开启事务 begin / start transaction 
 插入数据后可rollback
 但 commit手动提交后就不可以rollback
- 
事物的四大特性: 
 A 原子性:事务是最小的单位,不可以在分割
 C 一致性:事务要求,同一事务中的sql语句,必须保证同时成功或者失败
 I 隔离性:事务1 和事务2 之间是具有隔离性的
 D 持久性:事务一旦结束(commit,rollback),就不可返回
- 
事务开启: 
 set autocommit=0
 begin
 start transaction
 事务手动提交:
 commit
 事务手动回滚:
 rollback
- 
事务的隔离性: 
- 
read uncommitted 读未提交的 
- 
read committed 读已经提交的 
- 
repeatable read 可重复读 
- 
serializable 串行化 
 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号