目录
一、DDL语句
(Data Definition Language)
数据库定义语言→针对数据库和数据表的操作
(一)数据库DDL语句
- 创建数据库 : create database database_name
- 删除数据库: drop database database_name
- 查看数据库:show database
(二)数据表的DDL语句
- 创建表
create table table_name( 字段名 字段数据类型 not null auto_increment主键自动增长, id int not null auto_increment, name varchar(20), like varchar(20), primary key (id当前表中主键名) foreign key (当前表中的外键名) references(被引用表名)(被引用的表的主键名) )
- 删除表:drop table table_name
- 修改表名:alter table
- 重命名表:rename table name to new_name
(三)字段的DDL语句
- 新增字段:add column_name data_type [not null] [default default_value]
- 变更字段:change name new_name data_type [not null] [default default_value]
- 更新字段:modify name new_data_type [not null] [default default_value]
- 删除字段:drop column column_name
- 删除唯一约束:drop unique constraint_name
(四)删除与添加外键
- 添加外键约束:
alter table table_name( add constraint constraint_name约束名 foreign key(当前表中外键名) references referenced_table_name被引用主表名(被引用表主键) )
- 删除外键约束:drop foreugn key constraint_name
二、DML语句
(Data Manipulation Language)
数据库操纵语言→针对于数据库表中的数据进行操作
- insert新增记录: insert into可略 table_name字段 values值
- update可以对表中一行、多行甚至所有记录进行修改:
update table_name set 字段名=字段值,字段名=字段值 where+条件
- delete删除表记录:delete from table_name where+条件
[不存在where+条件,则修改全部内容!]
三、DQL语句
数据库查询语言→针对于数据库表中的数据进行查询
(一)数据查询
select 字段 from tableName表名 where+条件 group by 字段 having 分组完过滤`条件表达式 order by 字段 asc/desc
asc默认升序排序,desc降序排序
- 选择所有列:select * from 表名 +where...可单独查询
- 查询部分列:select 列名 from 表名
- 列标题:select 英文列名 AS可略写为空格 中文列名 from 表名
- 过滤重复行:select distinct 字段名 from 表名
- 选择列表为表达式:select 字段名1,字段名2,ceil(字段名2 * 1.2) from 表名
- 限制查询结果数量:select * from 表名 order by 字段名 desc/asc limit(offset,rows)
offset偏移量(可略),rows指定返回记录行最大数目
(二)运算符
基本运算符 | +加 -减 *乘 \除返回商 %返回余数 |
基本比较运算符 | =等于 <=>安全等于可以比较null >大于 >=大于等于 <小于 <=小于等于 |
<> 或 != 不等于 |
|
IS NULL 判断是否为null值 IS NOT NULL 判断是否不为null值 |
|
逻辑运算符 | AND 或 && 并且 OR 或 || 或者 |
NOT 或 ! (非) null→null,取反 0→1,非0→0 XOR (异或) null→null, 非null(都0\都非0,null→null0; 0+非零,null→null1) |
(三) 模糊查询
select 字段名 from 表名 +where...字段名 like\in\between数值and数值
in:成员条件运算符
%匹配0~n个任意字符
_匹配任意一个字符
[ ]用于指定一个字符合集