MySQL 5 数据库基础语句总结

来源:互联网 发布:淘宝客服外包多少钱 编辑:程序博客网 时间:2024/06/06 06:46

近来开始接触MySQL,为了方便学习使用,将一些常用的基本语句做了总结。以“库-表-字段-数据”的结构框架为顺序,以“增改查删”为线索,总结了如下这些语句。


数据类型

整数型:

类型 大小 范围(无符号) tinyint 1B 0 - 255 smallint 2B 0 - 65 535 mediumint 3B 0 - 16 777 215 int 4B 0 - 4 294 967 295 bigint 8B 0 - 18 446 744 073 709 551 615

浮点型:

类型 大小 float 4B double 8B decimal

字符串类型:

类型 大小 char 255B varchar 255B tinyblob 255B tinytext 255B blob 65 535B text 65 535B mediumblob 16 777 215B mediumtext 16 777 215B longblob 4 294 967 295B longtext 4 294 967 295B

日期类型:

类型 大小 范围 格式 date 3 1000-01-01/9999-12-31 YYYY-MM-DD time 3 -838:59:59/838:59:59 HH:MM:SS year 1 1901/2155 YYYY datetime 8 1000-01-01-00:00:00/9999-12-31-23:59:59 YYYY-MM-DD HH:MM:SS timestamp 8 1970-01-01-00:00:00/2037年某时 YYYYMMDDHHMMSS

操作语句:

库:

添加

create database [if not exists] 库名;

查询

show databases;   use库名;select database();show create database库名[\G]

修改

alter database 库名;

删除

drop database [if exists] 库名;

表:

建立

use 库名; create table 表名(字段名1 字段类型 字段约束, 字段名2 字段类型 字段约束...);create table 库名.表名(字段名1 字段类型 字段约束, 字段名2 字段类型 字段约束...);

添加

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

查询

describe 表名;简单show create table 表名 \G详细show tables;查询库所有表

修改

update 表名 set c=新值... where 条件;alter table 表名 rename 表名;修改表名

删除

delete from 表名;删除表数据drop table 表名;删除表

字段:

添加

alter table 表名 add 字段名 type [约束] [first|after old_字段名];

查询

select * from 表名;

修改

alter table 表名 modify 字段名 type;修改类型alter table 表名 modify 字段_name type first|after 字段名;修改位置

删除

alter table 表名 drop 字段名;

约束语句:

主键约束:

创建

create table 表名(字段名 类型 primary);create table 表名(字段名1 类型,字段名2 类型,字段名3 类型,primary key(字段名1,字段名2));

添加

alter table 表名 add primary key(字段名1,字段名2);

修改

alter table 表名 modify 字段名 类型 primary key;

删除

alter table 表名 drop primary key;

外键约束:

添加

alter table 表名1 add [constraint 外键名] foreign key(外键字段名) references 表名2(引用字段名);

查询

show create table 表名;

删除

alter table 表名 drop foreign key 外键名;

默认值约束:

添加

alter table 表名 alter 字段名 set default 默认值;create table 表名(字段名 类型 not null default 默认值);

删除

alter table表名 alter 字段名 drop default;     

唯一性约束:

创建

create table 表名(字段名1 类型 not null unique,字段名2 类型 not null, unique(字段名2));

添加

alter table 表名 add unique(字段1,字段2…);

删除

alter table 表名 drop index 字段名;

自添加约束:

创建

create table 表名(字段名 类型 primary key auto_increment);

添加

alter table 表名 modify字段名 类型 not null auto_increment;

删除

alter table 表名 modify字段名 类型 not null

数据操作语句:

select(查询):

全部

select * from 表名;

查询任意字段(按列表序输出)

select 字段列表 from表名; 

条件

select 字段列表 from 表名 where 条件表达式;
条件 符号或关键字 比较 =、<、<=、>、>=、!=、!<、!>、<> 指定范围 between and、not between and 指定集合 in、not in 匹配字符 like、not like(%任意字符、_一个字符) 是否为空值 is null、is not null 多个查询条件 and、or

分组

select字段列表from 表名 group by 字段列表 [having 条件表达式] [with rollup];

排序

select字段列表from 表名 order by 字段列表 [asc | desc];

限制

select字段列表from 表名 limit [初始位置(从0开始计数)], 查询记录数量;

避免重复

select distinct(字段名),字段列表 from 表名;

总条数

select [字段名] count(*) from 表名;

平均数

select [字段名] avg(*) [as ‘平均组名’] from 表名;

求和值

select [字段名] sum(*) [as ‘求和组名’] from 表名;

最大最小值

select [字段名] max[min](*) [as ‘极值组名’] from 表名;

内连接查询

select 字段列表 from 表名1  [inner] join 表名2  on 表名1.字段名=表名2.字段名;select a.字段,b.字段 from 表名1 as a inner join 表名2 as b on a.字段=b.字段;select 字段列表 from1,表2  where1.字段=表2.字段;select a.字段,b.字段 from 表名1 as a,表名2 as b where a.字段=b.字段;

insert(添加):

insert into 表名[(字段列表)] values(字段值列表),(字段值列表)…;insert into 表名1(字段列表1) select字段列表2 from 表名2 where 条件表达式;

update(修改):

update 表名 set 字段1=值1,…字段n=值n  where 条件表达式;

删除(delete):

delete from 表名 [where 条件表达式];

其他命令:

查询当前时间:

select now();

查询搜索引擎:

show engines;

执行文本文件命令:

source filename\. filenameshell>mysql [库名] < text_file
原创粉丝点击