数据库定义:create drop alter
数据操作:insert update delete truncate
数据控制:grant revoke while if...else
数据查询:select from where group by having order by
2 数据库操作
2.1创建数据库
create database <database>;
create database <database> charactor set <charsetype>;
create database <database> charactor set <charsetype> collate <校对规则>;
2.2 查看数据库
desc <database>;
show databases;
show create database <database>;
select database(); //查看当前使用的数据库
2.3 修改数据库
alter database <database> charactor set <charactortype>;
2.5 删除数据库
drop database <database>;
2.6 切换数据库
use <database>;
3 表操作
3.1 创建表
create table <table> (
col-1 datatype 约束,
col-2 datatype 约束,
col-3 datatype 约束
) character set <charset> collate <校对规则>;
datatype:
int char varchar boolean double float date time datatime blob text bit
约束:primary key unique not null
3.2 查看表
show tables;
show create table <table>;
3.3 修改表
rename table <table> to <new-table>;
alter table <table> add <col-name> <datatype> <约束>;
alter table <table> modify <col-name> <datatype> <约束>;
alter table <table> change <col-name> <new-col-name> <datatype> <约束>;
alter table <table> character set <charset>;
alter table <table> drop <col-name>;
3.4 删除表
drop table <table>; //清空表中的数据及表结构
truncate table <table>; //清空表中的数据
4 数据CRUD语句
4.1 insert
insert into <table> (col-1,col-2,...) values (value-1,value-2,...);
//当插入的数据为全列数据时,可以省略col
insert into <table> values(value-1,value-2,...);
4.2 update
update <table> set col-1=expr1,col-2=expr2,... where ...;
example:
update employee set salary=5000;
update employee set salary=3000 where name='zs';
update employee set salary=4000,job=ccc where name='ls';
update employee set salary=salary+1000 where name='wu';
4.3 delete
delete from <table> where...;
example:
delete from employee where name='zs';
drop table <table>;
truncate table <table>;
4.4 select
select [distinct] <*> from <table> where...;
example:
select * from student;
select sname,score from student;
select distinct * from student;
select from where group by having order by...;
4.5 函数
select count(column) from <table> where...;
example:
select count(sid) from student;
select count(sid) from student where math > 90;
select sum(column) from <table> where...;
example:
select sum(math) from student;
select sum(math),sum(chinese),sum(english) from student;
select avg(column),avg(column),avg(column)... from <table> where...;
select avg(math) from student;
select <col-1,col-2,...> from <table> group by <column> having...;
5 进阶内容
5.1 外键约束
alter table <table> add foreign key <table>(column) refrences <table>(column);