数据库对象:
数据库
表
字段
索引
视图
触发器
事件
存储过程
存储函数
用户
表:关系描述
create table IF NOT EXISTS stu(); 表是否存在
SHOW WARNINGS;显示警告内容
CHECKSUM 1 对表校验
COMMENT 描述信息
DELAY_KEY_WRITE 索引更新
从另外表中提出一部分字段到新表:
create table course2 select * from course where id < 100;
create table course3 LIKE course; 不复制数据
向表中添加数据:INSERT
INSERT [INTO] tb_name VALUES (val1,val2,...),(val1,val2,...)……
唯一键约束:
alter table course add UNIQUE KEY(course);
替换原来的字段:
REPLACE into course() values();
两表内容相同:
insert into course3 selecft * from course;
用SET添加数据:
insert into course set course='';
修改记录,前三行ID+1:
UPDATE course SET id=id+1 ORDER BY id LIMIT3;
删除记录:
DELETE FROM course where id=7;
查询的结果给以别名:AS name
select course AS case from course where id<2;
范围内查询:
select * from course where id IN (1,2,3);
select * from course where id BETWEEN 1 AND 4;
select * from course where course LIKE 'L%';
select * from course where course LIKE 'L_maishenjian';
有重复的只显示一次:
select DISTINCT stardate FROM course;
select DISTINCT stardate FROM course ORDER BY startdate DESC;
分组:
select * FROM course GROUP BY startdate;
多表查询:
连接
交叉,内连接,外连接(左,右,完全),自连接,UNION
内连接:
select s.name,c.course from stu AS s,course AS c where s.course=c.id;
外连接:
左连接
select * from stu AS s LEFT JOIN course AS c ON s.course=c.id;
右连接
select * from stu AS s RIGHT JOIN course AS c ON s.course=c.id;
自连接:
select s1.name AS HOME,s2.name AS CUSTOMER FROM stu AS s1 INNER JOIN stu AS s2 WHERE s1.name <> s2.name;
子查询:
select name,course from stu where gender='m' AND course=2;
view,视图:
虚表,基表
create view test AS select name,course from stu;
with check option
索引:
scanning, 全表扫描
create table
INDEX index_name(col1,col2,..)
ALTER TABLE tb ADD INDEX index_name() USING {BTREE|RTREE|HASH|FULLTEXT} USING指定索引类型
CREAT INDEX index_name ON table_name(col1,col2)
各引擎支持的索引类型
事务:transaction
看作一个整体,全部执行
ACID:
原子性
一致性
隔离性
持久性
MyISAM, table 表级别锁
InnoDB, row 行级别锁
xtradb,InnoDB的扩充
读锁——共享,写锁——独有 优先于读锁
START TRANSACTION;开启事务
ROLLBACK;不提交,回去
COMMIT;提交
SAVEPOINT point_num;保存点
ROLLBACK TO SAVEPOINT point_num;回滚的点
事务四个隔离级别:
READ UNCOMMITTED 读未提交
READ COMMITTED 读提交
REPEATABLE READ 重读
SERIALIZABLE 串行化
查看隔离级别:
SELECT @@tx_isolation;
变量:
全局变量
会话变量
变量:
静态,动态
SET GLOBAL variable=VALUE 设置全局变量
存储过程,存储函数,触发器,调度事件
存储过程:
\d //
create procedure name()
begin
statement;
statement;
end//
\d;
call procedure_name(); 调用过程
drop procedure name; 删除存储过程
存储函数:
select function_name(); 调用函数
触发器:trigger
六种
insert,delete,upadte
调度事件: 定时执行
show create procedure what_is_today; 查看
ENUM 枚举
IF(condition,1,2) 满足条件显1,否则显2