MySQL 5 数据库基础语句总结
来源:互联网 发布:淘宝客服外包多少钱 编辑:程序博客网 时间:2024/06/06 06:46
近来开始接触MySQL,为了方便学习使用,将一些常用的基本语句做了总结。以“库-表-字段-数据”的结构框架为顺序,以“增改查删”为线索,总结了如下这些语句。
数据类型
整数型:
浮点型:
字符串类型:
日期类型:
操作语句:
库:
添加
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 条件表达式;
分组
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 字段列表 from 表1,表2 where 表1.字段=表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
阅读全文
0 0
- MySQL 5 数据库基础语句总结
- mysql基础语句总结
- mysql数据库基础总结
- mysql数据库基础总结
- Mysql数据库操作语句总结
- MySQL数据库基础详解 数据库语句
- mysql操作数据库总结sql语句总结
- MySQL数据库基础知识点总结
- MySQL数据库-----------基础总结1
- MySQL数据库基础学习(SQL语句)
- Android学习-mysql 数据库基础语句
- MySQL语句基础之数据库约束
- MySQL数据库基础语句操作整理
- 一些mysql的数据库基础语句
- 一些基础的MySQL数据库操作语句
- 基础sql语句总结(mysql)
- MySQL基础sql语句总结(一)
- 【MYSQL】基础SQL语句总结(一)
- (137)投射阴影
- 常用地址
- Open Live Writer 安装和博客账号配置 博客园 CSDN 开源中国
- 通过TraceView定位卡顿问题
- SQL表连接查询(inner join、full join、left join、right join)
- MySQL 5 数据库基础语句总结
- Dubbo配置
- mysql insert锁机制
- commons-pool2对象池分配回收策略
- Struts2框架搭建
- 暴力搜索 寻找鞍点
- SQL语言学习心得
- Redis之对象
- hdu1242 Rescue(BFS +优先队列 or BFS )