MySQL基础语法

MySQL DDL语句

mysql -u 用户名 -p --登录MySQL
show databases; --查看数据库列表
create database 数据库名;	--创建数据库
drop database 数据库名; --删除数据库
use 数据库; --选择数据库
show tables; --查看数据库中的数据表
create table 数据库名(字段1 数据类型 约束条件,字段2 数据类型 约束条件,...)[defualt charset=utf8]; --创建数据表[default charset=utf-8 表默认字符集]
desc 表名; --查看数据定义
create table 表名(字段1 int,字段2,数据类型,primary key(字段1[,字段2]));  --创建主键1,主键数据类型一般为int
create table 表名(字段1 int primary key,字段2,数据类型); --创建主键2
create table 表名(字段1 int auto_increment,primary key(字段1)); --自增,auto_increment不能单独存在,必须与主键搭配
show create table 表名\G --查看创建数据表的SQL语句
drop table 表名; --删除表
alter table 表名 add primary key(字段名); --修改/添加主键
alter table 表名 drop primary key; --删除主键,删除主键之前必须先删除auto_increment
alter table 表名 modify [column] 字段 字段类型 auto_increment; --修改/添加自增
alter table 表名 modify [column] 字段 字段类型; --修改表字段属性(可以删除auto_increment)
alter table 表名 modify [column] 字段 字段类型 not null; --设置字段是否为NULL
alter table 表名 modify [column] 字段 字段类型 default 默认值; --设置default默认值
alter table 表名 add [column] 字段 字段类型; --增加表字段属性
alter table 表名 drop [column] 字段名; --删除表字段
alter table 表名 change [column] 旧字段 新字段 字段类型; --修改表字段名或者属性
alter table 表名 modify [column] 字段 字段类型 [first|after 字段名]; --字段排序
alter table 表名 rename [to] 新的表名;--修改表名

MySQL DML语句

insert into 表名 (字段1,字段2,……) values(值1,值2,……); --插入记录
insert into 表名 (字段1,字段2,……) values(值1,值2,……), (值1,值2,……),(值1,值2,……), (值1,值2,……), (……,……,……); --插入多条记录
update 表名 set 字段1=值1,字段2=值2,…… [where 条件]; --更新记录
update 表1,表2,…… set 表1.字段x=值,表2.字段x=值,…… [where 表1.字段=值,表2.字段=值,……]; --更新多个表
delete from 表名 [where 条件]; --删除记录
delete from 表1,表2,…… [where 条件]; --删除多个表中的记录
select * from 表名; --查询表中的所有记录
select 字段1,字段2,…… from 表名; --查询指定字段
select distinct 字段1,字段2,…… from 表名; --查询不重复记录
select 字段1,字段2,…… from 表名 [from where(条件)]; --条件查询
select * from 表名 order by 排序字段 asc|desc; --排序(默认asc)低到高
select * from 表名 order by 字段1 asc|desc,字段n asc|desc; --多字段排序
select * from 表名 order by 字段1 asc|desc,字段n asc|desc limit n; --排序限制,n可以为数字,也可以是一个或多个区间
select sum(字段1)[,sum(字段2),……] from 表名; --求和
select count(*|字段名) from 表名; --记录总数(count一般count(*)这样用)
select max(字段名) from 表名; --最大值
select min(字段名) from 表名; --最小值
select [分组字段,sum(字段)] from 表名 group by 分组字段; --分组求和
select [sum(字段)] from 表名 group by 分组字段 with rollup; --分组求和再汇总
select [分组字段,sum(字段)] from 表名 group by 分组字段 having sum(字段)>n; --having后面跟比较运算符(n为一个具体的值)
select min(字段1),sum(字段2),max(字段3)count(*) from 表名; --聚合混合使用
select * from 表1,表2,…… where(表1.字段=表2.字段,……); --表连接,内连接,查询多表中匹配的字段
select 表1.字段1,表2.字段1,from 表1,表2,…… where(表1.字段=表2.字段,……); --表连接,内连接,查询多表中匹配的字段并显示指定字段
select 表1.字段 别名,表2.字段 别名,from 表1,表2,…… where(表1.字段=表2.字段); --表连接,内连接,查询多表中匹配的字段并给指定"字段"起别名
select * from 表1 表别名1,表2 表别名2,…… where(表别名1.字段=表别名2.字段); --表连接,内连接,查询多表中匹配的字段并给指定"表"起别名
select * from 表1 left join 表2 on 表1.字段=表2.字段; --表连接,外连接,左连接(left join),on连接方法(左连接,把左边一张表全部列出来,右边自动补全(null))
select * from 表1 right join 表2 on 表1.字段=表2.字段; --表连接,外连接,右连接(right join),on连接方法(右连接,把右边一张表全部列出来,左边自动补全(null))
select * from 表1 where 字段1 in(select 字段1 from 表2); --子查询,查询表1的字段在表2中(查询结果只有一条,in可以用=代替)与not in相反
select * from 表1 where 字段1 exists(select 字段1 from 表2); --子语句有没有查询出来,有true否则false,null返回true,与not exists相反
select * from 表1 union select * from 表2 union select * from 表3; --联合查询,注意:多个表的“列数”要相等,有去除重复功能
select * from 表1 union all select * from 表2 union all select * from表3; --联合查询,注意:多个表的“列数”要相等,没有去除重复功能

MySQL 字符集

create table 表名 [default character set 字符集 [default collate 校对规则]]; --创建字符集和校对规则
set character_set_client=字符集; --设置客户端字符集(先选择表)
set character_set_results=字符集; --设置返回字符集(先选择表)
set names 字符集; --设置字符集全部一样(先选择表)
use 表名; --选择表以后(重点,没有这步,后面的执行会出错)
show variables like 'character_set_database'; --才可以查看字符集
show variables like 'collation_database'; --和校对规则
show variables like '字符集前缀%'; --查看字符集的校对规则
mysqldump -u 用户名 -p 数据库名 > 位置/数据库名称 --命令提示符备份数据库
mysqldump -u 用户名 -p 数据库名 < 位置/数据库名称 --命令提示符回复数据库
alter database 表名 [default character set 字符集名 [default collate校对规则名]]; --修改字符集或校对规则

MySQL 字符集

show character set;

注意:_ci(不区分大小写)
   _cs(大小写敏感)
   _bin(二元,即比较基于字符编码的值,与language(语言)无关)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值