mysql简单代码_mysql数据库简单语句

1.创建数据库 :create database 名称 [character 字符集 collate 校队规则;

2.查看校队规则 :show collation;

3.查看数据库/表 :show databases/tables;

4.显示创建的数据库/表 :show create database/table 数据库名/表名[\G];

5.查看当前使用的数据库 :select database();

6.创建表 :create table 表名(

字段名 数据类型 [该字段约束条件],

字段名 数据类型 [该字段约束条件],

......

)charcater set 字符集 collate 校队规则;

7.删除数据集/表 :drop database/table 名;

8.查看数据表 :describe 表名 ;

或者 desc 表名;

9.修改表名 :alter table 表名 rename 新表名;

或者 rename table 表名 to 新表名;

10.主键(=非空+唯一) :primary key

唯一约束 :unique

非空约束 :not unll

默认约束 :default 默认值

检查约束 :check 条件

自动增长 :auto_increment

11.添加数据 :insert into 表名(字段名,字段名,字段名...不写默认全部字段) values

(数据,数据,数据),

(数据,数据,数据),

...

(数据,数据,数据);

或者 :insert into 表名

set 字段名=值,字段名=值,...;

12.更新数据 :update 表名

set 字段名=值,字段名=值...where 过滤条件;

13.删除数据 :delete from 表名 where 过滤条件;

或者 truncate 表名;

14.复制表 :create table 新表名 like 旧表名; (拷贝结构)

create table 新表名 as (select * from 旧表名); (拷贝数据)

create table 新表名 like 旧表名;

insert into 新表名 as (select * from 旧表名); (结构数据都拷贝)

15.查询 :select * from 表名;

select 字段名,字段名,...from 表名;

过滤:where 过滤条件

in(集合) between...and(范围) is null(空值) distinct(排除重复值) like'%/_' (模糊查询) and(并且) or(或者)

16.聚合函数 :count()返回某列的行数 sum()返回某列的和 avg()返回某列的平均值 max()返回某列的最大值 min()返回某列的最小值 IFNULL(参数,如果为空则改为的值)

17.排序 :order by 字段名 asc/desc;

asc为升序 desc为降序 默认为asc

18.分组 :group by 字段名 having 过滤条件;

19.限制查询结果 :limit a,b;

a代表开始的标号(第一条为0) b代表显示的条数

20.子查询 :select 字段名,(子查询) from 表名;

select * from (子查询);

select * from 表名 where (子查询);

子查询关键字:exists(子查询)  只要里面的子查询返回了行,exists的值为真,外部查询就执行

字段名 (比较运算符如>) all (子查询) 大于子查询里的每一个值

字段名 (比较运算符如>) any (子查询) 大于子查询里的任何值,也就是最小值

in(子查询) 存在

21.集合操作 :select * from a

union

select * from b;

将两张表纵向连接起来,但是两张表的字段数必须相同

22.外键约束 :alter table 表名

add constraint 外键名 foreign key (外键字段名) references 外键表名 (主键字段名);

删除外键 :alter table 表名

drop foreign key 外键名

23.联表查询 :内连接 :select * from 表1 [inner] join 表2 [on/where 表1.关系字段=表2.关系字段]; (on是内联前过滤 ,where是产生笛卡尔乘积之后再过滤)

cross join(交叉连接,在MySQL中等价于inner join)

外连接(必须写关联条件) :左外 select * from 表1 left [outer] join 表2;

右外 select * from 表1 right [outer] join 表2;

全外 select * from 表1 full [outer] join 表2 (MySQL不支持,可以用union)

自连接 :select * from 表 别名1 join 表 别名2;

24.创建视图 :create view 视图名

as

select语句

查看视图 : ①desc 视图名

②show table statas like '视图名'

③show create view 视图名

修改视图 :create or replace view 视图名

as

select语句

或者 alter view 视图名

as

select语句

25 case when 表名=数据1 then '数据别名' when 表名=数据2 then '数据别名2'... else '别名3’ end 表别名

例如:select id,name,(case when ismale=1 then '男' when ismale=2 then '不明' else '女' end) 性别 from employee;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值