天天看点

Mysql常用语法大全Mysql常用语法汇总utf-8 编码gbk 编码可以不使用分号创建用户删除用户修改用户修改密码查看权限授权取消权限基本语法:表还存在,表内容清空添加列:删除列:修改列:添加主键:删除主键:添加外键:删除外键:修改默认值:删除默认值:更改表名插入单条数据插入多条数据插入另一条语句的查询结果as 做别名

Mysql常用语法汇总

数据库操作

1:查看数据库

SHOW DATABASES;

#默认数据库:

mysql -用户权限相关数据

test -用于用户测试数据

information_sechema -mysql本身架构相关数据

2:创建数据库

utf-8 编码

CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

gbk 编码

CREATE DATABASE 数据库名称 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

3:使用数据库

USE db_name;

可以不使用分号

4:用户管理

创建用户

create user ‘用户名’@‘IP地址’ identified by ‘密码’;

删除用户

drop user ‘用户名’@‘IP地址’;

修改用户

rename user ‘用户名’@‘IP地址’; to ‘新用户名’@‘IP地址’;;

修改密码

set password for ‘用户名’@‘IP地址’ = Password(‘新密码’)

PS:用户权限相关数据保存在mysql数据库的user表中,所以也可以直接对其进行操作(不建议)

5:授权管理

查看权限

show grants for '用户'@'IP地址'
           

授权

grant 权限 on 数据库.表 to '用户'@'IP地址'
           

取消权限

revoke 权限 on 数据库.表 from '用户'@'IP地址'
           

常用权限:

all privileges 除grant外的所有权限

select 仅查权限

select,insert 查和插入权限

usage 无访问权限

对于目标数据库以及内部其他:

数据库名.* 数据库中的所有

数据库名.表 指定数据库中某张表

数据库名.存储过程 指定数据库中的存储过程

. 所有数据库

6:授权局域网内主机远程连接数据库

#百分号匹配法

grant all on . to ‘test’@‘192.168.200.%’ identified by ‘test123’;

#子网掩码配置法

grant all on . to ‘test’@‘192.168.200.0/255.255.255.0’ identified by ‘test123’;

#刷新权限

flush privileges;

#远程登陆连接

mysql -utest -ptest123 -h 192.168.200.96

表操作

1、创建表

基本语法:

如果表存在就删除

drop table if exists 表名;

create table 表名(

列名 类型 是否可以为空 默认值 自增 主键,

列名 类型 是否可以为空

)ENGINE=InnoDB DEFAULT CHARSET=utf8

not null # 不可以为空

default 1 # 默认值为1

auto_increment # 自增

primary key # 主键

constraint 外键名 foreign key (从表字段’自己‘) references 主表(主键字段) # 外键

是否可空,null表示空,非字符串

not null - 不可空

null - 可空

默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值

create table tb1(

nid int not null defalut 2,

num int not null

)

自增,如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)

create table tb1(

nid int not null auto_increment primary key,

num int null

)

create table tb1(

nid int not null auto_increment,

num int null,

index(nid)

)

2、删除表

drop table 表名

3、清空表

表还存在,表内容清空

delete from 表名

truncate table 表名

4、修改表

添加列:

alter table 表名 add 列名 类型
           

删除列:

alter table 表名 drop column 列名
           

修改列:

alter table 表名 modify column 列名 类型; -- 类型
    alter table 表名 change 原列名 新列名 类型; -- 列名,类型
           

添加主键:

alter table 表名 add primary key(列名);
           

删除主键:

alter table 表名 drop primary key;
    alter table 表名 modify 列名 int, drop primary key;
           

添加外键:

alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
           

删除外键:

alter table 表名 drop foreign key 外键名称
           

修改默认值:

ALTER TABLE testalter_tbl ALTER i SET DEFAULT 1000;
           

删除默认值:

ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
           

更改表名

rename table 原表名 to 新表名;
           

增删改表的字段

#增加表字段,altertable法。

1> 语法: altertable 表名 add 字段 类型 其他;

eg:给student表添加一个sex列:alter table student add sex char(4);

eg:给student表添加一列name,且该字段在sex的前面:alter table student add name vacrhar(20) after sex;

eg:给student表添加一列class,且该字段位于表的第一列:alter table student add class varchar(20) first;

#更改表名字,rename法:

2>语法:rename table 原表名 to 新表名;

eg:把AAA表改名为BBB表:rename table AAA to BBB;

#删除表

1> 语法:drop table <表名>;

eg:删除掉student表:drop table student;

表内容操作

1、增

语法:insert into 表 (列名,列名…) values (值,值,值…)

插入单条数据

insert into 表 (列名,列名...) values (值,值,值...)
           

插入多条数据

insert into 表 (列名,列名...) values (值,值,值...),(值,值,值...)
           

插入另一条语句的查询结果

insert into 表 (列名,列名...) select 列名,列名... from 表
           

2、删

语法:delete from 表

delete from 表;

delete from 表 where id=1;

3、改

语法:update 表 set name = ‘nick’ where id>1

update 表 set name = ‘nick’ where id>1

4、查

语法:select * from 表

select * from 表

select * from 表 where id > 1

select nid,name,gender as gg from 表 where id > 1

as 做别名

5、条件

语法:select * from 表 where id > 1

6、通配符

语法:select * from 表 where name like ‘_n%’

7、限制

语法:select * from 表 limit 9,5;

eg: select * from 表名 limit 5; #查询表中的前五行数据

select * from 表名 limit 9,5; #从第九行开始的五行数据

select * from 表名 limit 5 offset 9;#从第九行开始的五行数据

8、排序

语法:select * from 表 order by 列1 desc,列2 asc

9、分组

语法:select num from 表 group by num

注:group by 必须在where之后,order by之前

10、连表

语法:inner join . on、left join . on、right join . on

11、组合

语法:union、union all