DDL:
1:创建库/删除库
create database test; drop database test;
2:创建表/删除表:
create table test (id int(255) NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(100) NOT NULL, password varchar(255) , friends text)ENGINE=MYISAM;
create table test (d1 int(255) NOT NULL AUTO_INCREMENT ,d2 int(255) not null, name varchar(255),index(d2,d1))engine=myisam; index()是建立组合索引,myisam存储规则是先按照d2进行排序之后再d1自增,innodb格式只能是d1为key且优先自增。
create table test2 engine=memory select name,sal from test1 group by sal; 用test1表的name和sal数据创建一个test2表,
drop table test;
3:查看详细表信息:
show create table test \G;
4:修改表:
alter table test modify name varchar(100) first; 原varchar(10)-->改为varchar(100)
alter table test add column age int(3) after id; 添加一个字段
alter table test drop column name ;删除一个字段
alter table test change name username varchar(100); 改name字段名为username
alter table test rename to newname ; 只改名字
DML:
1:插入
insert into test (name, password) values('yingying', '123456'),('yanyan','111111'); 连续插入两条记录
2:更新
update test set password='qqqqqq' where name='yingying';
update test a,test1 b set a.times=b.id*a.times,b.username=a.name where a.id=b.id; 用a b代替test test1两个表,test.times=test1的id乘test的times,同时改test1的uwename为test的name。
3:删除
delete from test where name='yingying';
delete from test; 清空信息
4:查询
select * from test;
select name,password from test where name='yingying' and times='10'; (条件可以是< <= > >= != 条件之间可以是 or and)
5:排序
select * from test order by id,salary desc; 按照id和salary逆序排序 如果没有desc按升序排 没有重复按id排,重复数据按desc排
select * from test order by sal limit 1,10 ;按sal排序后从第二条开始显示10条
6:聚合
select count(1) from test 统计记录数
select sal,count(1) from test group by sal with rollup; group by按sal进行分类聚合 相同的sal累加在一起 with rollup:对聚合进行最终汇总
select sal,count(1) from test group by sal having count(1) > 100; 聚合后按条件count(1)>100查看符合条件的数据
select min(sal),max(sal),sum(sal) from test; 分别按需要计算
7:表连接
内部链接:相互匹配的记录
select name,department from test,departable where test.depart=departable.depart; 例如test定义了用户属于部门1、2、3、而depart表记录了1是财务部,2是人事部,3是销售部等等 这样一句就会自动把成员与部门对应起来就不必再手动去对了。
外连接:匹配和不匹配的记录(很少用)
select name,department from test left join departable on test.depart=departable.depart 例如test有一个用户不属于departable的任何一个部门也会被列出
select name,department from departable rigth join test on departable.depart = test.depart 作用同上
8:子查询
select * from test where sal in(select sal from test1) 查询在test1中存在sal的sal in可以换成(in not in = =! exists not exists)
9:记录联合
>select name from test
>union all 联合all显示所有信息 如果是union没有all 不显示重复信息
>select name from chat.chat_user;
DCL:
1:grant select ,insert on test.* to 'user1'@'localhost' identified by '123' 创建一个数据库用户user1 密码是123 具有insert和select 权限
2:revoke insert on test.* from 'user1'@'localhost';
3:use mysql; insert into user () values(); 在user中加用户
帮助:
1:? contents
2:? data types
3:? int
4:? show
5:? create table