极简!一看就会的MySQL schema、table操作!

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细节:

wherehaving的区别:

(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.

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值