mysql alter user语法_MySQL基本语法

MySQL基本语法

数据库操作

格式:

create database 表名;

示例:

create database mydatabase;

格式:

drop database 表名;

示例:

drop database mydatabase;

直接在文件夹中修改

格式:

show databases;

示例:

show databases;

数据表操作

注意:删、改、查等操作是在使用use 表名之后进行的

格式:

create table 表名(

字段名称 数据类型 [列的完整性约束],

字段名称 数据类型 [列的完整性约束],

...

)engine=myisam charset=utf8;

示例:

create table test_table(

id int(11),

username varchar(255),

password char(32)

)engine=myisam charset=utf8;

格式:

drop table 表名;

drop table if exists 表名;

示例:

drop table test_table;

drop table if exists test_table; #表名不存在时不会报错

格式:

alter table 表名 rename 新的表名;

或者:

alter table 表名 rename to 新的表名;

示例:

alter table test_table rename my_table;

或者:

alter table my_table rename to test_table;

格式:

查看所有的表

show tables;

查看创建表的语句

show create table 表名;

示例:

show tables;

show create table test_table;

字段操作

语法:

1、默认

alter table 表名 add 字段名称 数据类型 [列的完整性约束];

2、添加到第一个

alter table 表名 add 字段名称 数据类型 [列的完整性约束] first;

3、添加到某个字段之后

alter table 表名 add 字段名称 数据类型 [列的完整性约束] after 已有字段名称;

示例:

alter table test_table add test_field int; /* 默认添加到最后 */

alter table test_table add test_field int first; -- 添加到第一个

alter table test_table add test_field decimal(11,2) after my_field; # 添加到my_field字段之后,在需要存入金钱信息时,使用decimal,11和2分别表示小数点之前和之后的位数

语法:

alter table 表名 drop 字段名称;

示例:

alter table test_table drop test_field;

语法:

1、只修改位置

alter table 表名 modify 字段名称 数据类型 位置信息;

2、只修改字段名称

alter table 表名 change 原有字段名称 新的字段名称 数据类型;

3、修改位置和数据类型

alter table 表名 modify 原有字段名称 新的数据类型 位置信息;

4、修改字段名称和数据类型

alter table 表名 change 原有字段名称 新的字段名称 新的数据类型;

示例:

/* 只修改位置 */

alter table test_table modify test_field int first; -- 移动到最前面

alter table test_table modify test_field int after my_field; # 移动到某个字段之后

/* 只修改字段名称 */

alter table test_table change test_field test2_field int(11); # 这里的数据类型与原有相同

/* 修改位置和数据类型 */

alter table test_table modify test_field char(255) first;

/* 修改字段名称和数据类型 */

alter table test_table change test_field test2_field char(20); # 这里的数据类型与原有不同

语法:

desc 表名;

查询内容的解释:

Field 字段名称

Type 数据类型

NULL 是否允许为空

Key 是否是主键、唯一键

Default 默认值

Extra 扩展内容

示例:

desc test_table; #查询test_table表的所有信息

列的完整性约束

Null

NULL 允许为空

NOT NULL 不允许为空

Key

PRIMARY KEY 主键

UNIQUE 唯一键

Default

默认值

Extra

UNSIGNED 设置当前字段不允许出现负数

AUTO_INCREMENT 设置当前字段自增

使用列的完整性约束建立数据表

create table test_table(

field1 int(11) unsigned not null auto_increment,

field2 varchar(255) not null unique,

field3 char(32) not null,

field4 tinyint(1) not null default 0,

field5 date not null,

field6 date null,

primary key(id)

)engine=myisam charset=utf8 collate utf8_general_ci;

数据操作

注意:删除和修改时必须加where语句

语法:

1、插入一条

insert into 表名 (字段1,字段2,字段3,...) values (值1,值2,值3,...);

2、一次插入多条

insert into 表名 (字段1,字段2,字段3,...) values (值1,值2,值3,...),(值1,值2,值3,...),...;

3、省略字段

insert into 表名 values (值1,值2,值3,...);

注意:值需要按顺序全部填写

示例:

/*

插入时的注意事项:

1 字段和值一一对应

2 主键和唯一键不能重复

3 自动递增的字段会找到当前最大值加1

4 不能为空的字段必须插入内容(主键自增可以不写)

*/

/* 插入一条 */

insert into test_table (field1,field2,field3) values (val1,val2,val3);

/* 一次插入多条 */

insert into test_table (field1,field2,field3) values (val11,val12,val13),(val21,val22,val23);

语法:

delete from 表名 where语句;

示例:

delete from test_table where id=1;

语法:

update 表名 set 字段1=值1,字段2=值2,... where语句;

示例:

update test_table set field1=val2,field2=val1 where id=1;

1、查询所有字段对应的内容

select * from 表名;

2、查询指定字段对应的内容

select 字段1,字段2,字段3,... from 表名;

3、查询时过滤掉重复的内容

select distinct(字段1) from 表名;

4、同时查询多个表

select * from 表1,表2,...;

select 表1.字段1,表1.字段2,表2.字段1,表2.字段1,... from 表1,表2,...;

示例:

/* 查询所有字段 */

select * from test_table;

/* 查询指定字段 */

select field1,field2,field3 from test_table;

/* 过滤重复内容 */

select distinct(field1) from test_table;

/* 查询多个表 */

select * from table1,table2;

select table1.field11,table1.field12,table2.field21,table2.field22 from table1,table2;

where条件

(可以使用在 查询、修改、删除语句中)

示例:

# 查询

select * from test_table where id=1; # id=1

select * from test_table where id!=1; # id!=1

select * from test_table where id>1; # id>1

select * from test_table where id<10; # id<10

select * from test_table where id>2 and id<5; # 2

select * from test_table where id<2 or id>5; # id<2 || id>5

select * from test_table where field1 is null;

select * from test_table where field1 is not null;

select * from test_table where id in(1,2,3); # id=1、id=2、id=3

select * from test_table where id not in(1,2,3); # id!=1...

select * from test_table where id between 3 and 25; # 3

####################################################################

# count() 数量

select count(id) from test_table where id>3; # 查询id>3的数据条数

# sum() 总和

select sum(age) from test_table where id>3; # 查询id>3的所有age的总和

# avg() 平均

select avg(age) from test_table where id>3; # 查询id>3的所有age的平均值

# max() 最大值

select max(age) from test_table where id>3; # 查询id>3的age的最大值

# min() 最小值

select min(age) from test_table where id>3; # 查询id>3的age的最小值

# concat() 合并多列

select concat(field1,':',field2) from test_table where id>3;

####################################################################

limit条件

(限制查询的条数)

示例:

select * from test_table limit 3; # 查询3条,默认从第一条开始,等于limit 0,3

select * from test_table where id>3 limit 3; # 配合where使用,放在where之后

select * from test_table limit 3,4; # 从3开始,查询4条(第1条为0)

order by条件

(查询的顺序,asc升序、desc降序,不写默认升序)

示例:

select * from test_table order by id; # 默认升序

select * from test_table order by id desc; # 降序

select * from test_table order by id asc; # 升序

select * from test_table where id>20 order by id desc limit 3; # 配合where和limit使用

group by条件

(分组查询)

示例:

select field1,count(id) from test_table group by field1; #结果如下

# 聚合

# +--------+-----------+

# | field1 | count(id) |

# +--------+-----------+

# | aa | 3 |

# | bb | 2 |

# +--------+-----------+

select field1,count(id) from test_table where id>3 group by field1 order by id desc; # 和其它条件配合使用

like条件

(模糊查询,要和where一起使用)

示例:

select * from test_table where field1 like '___'; # 查询field1中有三位的数据,eg. aaa,bbb,哇哈哈...

select * from test_table where field1 like 'aa_'; # 查询的结果eg. aa1,aaa,aab,aar...

select * from test_table where field1 like 'a%'; # 查询以a开头的数据,eg. a1,aae,adf,adgfs...

select * from test_table where field1 like '%a'; # 查询以a结尾的数据,eg. asfa,fjksa,9uioa...

select * from test_table where field1 like '%a%'; # 查询包含a的数据,eg. adf,kdjfakj...

各个条件的顺序

where --> group by --> order by --> limit

多表联合查询

语法:

select * from 表1,表2 where 表1.字段=表2.字段

select 表1.字段,表1.字段2,表2.字段,表2.字段2... from 表1,表2 where 表1.字段=表2.字段

left join ... on

select * from 表1 left join 表2 on 表1.字段=表2.字段

right join ... on

select * from 表1 right join 表2 on 表1.字段=表2.字段

子查询

语法:

select field from table where id in (select field2 from table where field3=val);

给字段、表取别名

语法:

1、表

select name1.field1,name2.field2 from table1 as name1,table2 as name2 where name1.field3=name2.field4

2、字段类似

可以省略as,使用空格代替

select name1.field1,name2.field2 from table1 as name1,table2 as name2 where name1.field3=name2.field4;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值