1. 方案操作:
显示所有方案:
show schemas;
创建方案:
create schema 方案名;
删除方案:
drop schema 方案名;
2. 总表操作:
显示所有表:
show tables;
查询表的结构:
desc 表名;
创建表:
create table 表名;
eg:
create table emp
(
`id` int primary key not null,
`name` varchar(50) not null,
`emp_ID` int not null
foreign key (emp_ID) references depart(person_ID)
);
删除表:
drop table 表名
修改表名
alter table 表名 rename to 新表名
3. 表中属性的操作:
添加属性:
alter table 表名 add 属性名 类型 约束;
添加外键 :
alter table 表名1 add foreign key (属性名) references 表名2(属性名)
删除属性:
alter table 表名 drop 属性名
修改属性数据类型:
alter table 表名 modify 属性名 新数据类型(长度);
修改属性名和属性类型:
alter table 表名 change 旧属性名 新属性名 类型(长度) 约束;
4. 数据操作
添加数据:
insert into 表名 (属性1, 属性2,……) values (值1, 值2……); //指定属性
insert into 表名 values (值1, 值2……); //全部属性
删除数据:
delete from 表名 where ;
修改数据:
update 表名 set 属性1=值1, 属性2=值2,…… where ;
查询数据:
select 属性1,属性2…… from 表名 where(条件) / group by(分组) / order by(排序) column / limit(分页);
4.1 where细节:
4.2 group by细节:
where和having的区别:
(1) 执行时机不同:where是分组前进行过滤,不满足where条件的不参与分组;having是对分组后的结果进行过滤。
(2) 判断条件不同:where不能对聚合函数进行判断,而having可以。
(3) 常见聚合函数:
4.3 order by细节:
(1) ASC:升序(默认)
(2) DESC:降序
4.4 limit细节:
select * from table limit 0,10; //查询第一页数据,展示10条
select * from table limit 10,10; //查询第二页
注意事项
(1) 起始索引从0开始,起始索引=(查询页面-1)* 每页显示记录数码.
(2) 分页查询是数据库的方言,不同数据库有不同实现,MySQL是LIMIT.
(3) 如果查询的是第一页数据,起始索引可以省略,直接简写 LIMIT 10.