MYSQL语句

来源:互联网 发布:煤矿主井提升系统优化 编辑:程序博客网 时间:2024/04/29 17:13

一、数据库创建:

要使用mysql需要先创建一个数据库。安装安mysql以后会自带两个数据库:test mysql

1.查看当前mysql中的数据库语法:

Show databases

2.创建数据库:

Create  database  数据库名

3.使用数据库:

Use 数据库名

4.删除数据库:

Drop  database  数据库名;

二、表管理

1.查看当前数据库种的表语法:

Show tables

2.查看表的结构:

Describe 表名;或  desc  表名

3.创建表:

Create [temporary] table [ if not exists ] 表名 (字段1 数据类型 ,

                            字段2  数据类型)

Temporary 临时表

4.select结果创建新数据表:

Create table 新表名 Select * from 表名

5.修改表:

添加列:

Alter table 表名 add 字段名 数据类型

修改列:

Alter table 表名 modify column字段名 数据类型

修改字段名:

Alter table 表名 change 老字段名 新字段名 数据类型

删除列:

Alter table 表名 drop column 字段名

删除表:

Drop table 表名

三、约束

1.主键

Alter table 表名 add constraint 约束名 primary key(字段)

2.外键

Alter table 表名 add constraint 约束名 foreign key (字段)

References (字段)

3.唯一约束

Alter table 表名 add constraint 约束名 unique(字段)

4.非空约束

Alter table 表名 modify column 字段 数据类型 not null

四、管理数据

1.插入数据:

Insert into 表名 values (值,值)

2.插入多行数据:

Insert into 表名 select * from 表名

3.用一条语句插入多条新的数据记录

Insert into 表名 values (值,值),(值,值),(值,值)

4.更行数据:

Update 表名 set 字段= where 条件

5.删除数据:

Delete from 表名 where 条件

6.删除排序清单里的数据记录

Delete from 表名 order by … limit …

五、查询数据

1.查看全部列中的数据

Select * from 表名

2.查看指定列中的数据

Select 字段名 from  表名

3.消除重复数据项:

Select distinct 字段名 from  表名

4.限制查询结果中的数据个数:

Select * from 表名 limit 3

Select * from 表名 limit 33

Select sql_calc_found_rows字段名from 表名 limit 33

Select found_rows( )

5.排序

Select * from 表名 Order by asc | desc

6.Where 子句

比较运算符:日期、数字、字符串

In 运算符:

Like 运算符:_  %

Between … and …

合并查询结果:union [all]

7.连接查询

Inner join

Left outer join   |  right outer join  

cross join

join … on …

join … using …

表名,表名 where 条件

8.分组

Group by

Having

Group_concat(字段名 order by 字段名 ) 统计函数

注:从mysql 4.1开始,group by 语法又增加一个新的关键字

with rollup 作用:将在查询结果的最后一行降自动增加一条

总数统计记录。这个记录的id字段名字永远是null

 

原创粉丝点击