MySql 相关
MySql 相关
Spark一、 DDL
1. 数据库操作
(1)查询
查询所有数据库
1
show databases;
查询当前数据库
1
select database();
(2)创建
创建数据库
1
create database [if not exists] 数据库名 [default charset 字符集] [collate 排序规则];
(3)删除
删除数据库
1
drop database [if exists] 数据库名
(4)使用
使用数据库
1
use 数据库名;
2. 表操作
(1)查询 show
| desc
查询当前数据库所有表
1
show tables;
查询表结构
1
2
3desc 表名;
describe 表名;
show columns from 表名;查询指定表的建表语句
1
show create table 表名;
(2)创建 create table
| create index
建表语句
1
create table 表名 (列名1 数据类型 [约束] [comment 注释], 列名2 数据类型 [约束] [comment 注释], ...);
创建索引
1
create [unique|fulltext|spatial] index 索引名称 on 表名(字段名称[(长度)] [asc|desc])
(3)修改 alter table
添加字段
1
2alter table 表名
add [column] 字段名 数据类型 [约束] [comment 注释];添加索引
1
2alter table 表名
add [unique|fulltext|spatial] index 索引名称(字段名称[(长度)] [asc|desc]);添加
check
约束(域完整性)1
2alter table 表名
add constraint 约束名称 check (条件);添加唯一约束(实体完整性)
1
2
3# 唯一约束
alter table 表名
add constraint [约束名称(若不设置,则以字段名称作为约束名称)] unique (字段名称);添加主键约束和外键约束(参照完整性)
1
2
3
4
5
6
7
8
9# 主键约束
alter table 表名
add constraint [约束名称(在 MySql 即使设置该参数也会被忽略)] primary key (字段名称);
# 外键约束
alter table 表名
add constraint 约束名称 foreign key(字段名称)
references 被引用表名 (被引用字段名称);
# 注意:必须先为被引用字段创建索引,通常是主键索引修改数据类型
1
2alter table
modify 字段名 新数据类型 [约束] [comment 注释];修改字段名和数据类型
1
2alter table
change 旧字段名 新字段名 新数据类型 [约束] [comment 注释];修改表名
1
2
3alter table 旧表名 rename to 新表名;
# 或者
rename table 旧表名 to 新表名;
(4)删除 drop table
| truncate table
| alter table drop
| drop index
删除表
1
drop table [if exists] 表名;
删除,并重建指定表
1
truncate table 表名;
删除字段
1
2alter table 表名
drop 字段名;删除约束
1
2
3
4
5
6
7
8
9
10
11alter table 表名
drop constraint 约束名;
# 删除主键约束有三种写法,分别是:
alter table 表名
drop primary key;
# 或者 (不推荐)
alter table 表名
drop constraint `primary`;
# 或者 (不推荐)
drop index `primary` on 表名;删除索引
1
2
3
4alter table 表名
drop index 索引名;
# 或者
drop index 索引名 on 表名;
(5)常用的数据类型
① 数值类型
类型 | 大小 | 范围(有符号) | 范围(无符号) | 用途 |
---|---|---|---|---|
tinyint | 1 Bytes | (-128,127) | (0,255) | 小整数值 |
smallint | 2 Bytes | (-32 768,32 767) | (0,65 535) | 大整数值 |
mediumint | 3 Bytes | (-8 388 608,8 388 607) | (0,16 777 215) | 大整数值 |
int integer | 4 Bytes | (-2 147 483 648,2 147 483 647) | (0,4 294 967 295) | 大整数值 |
bigint | 8 Bytes | (-9,223,372,036,854,775,808,9 223 372 036 854 775 807) | (0,18 446 744 073 709 551 615) | 极大整数值 |
float | 4 Bytes | (-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) | 0,(1.175 494 351 E-38,3.402 823 466 E+38) | 单精度 浮点数值 |
double | 8 Bytes | (-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) | 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) | 双精度 浮点数值 |
decimal | 对decimal(M,D) ,如果M>D,为M+2否则为D+2 | 依赖于M和D的值 | 依赖于M和D的值 | 小数值 |
② 日期类型
类型 | 大小(Bytes) | 范围 | 格式 | 用途 |
---|---|---|---|---|
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 | 年份值 |
datatime | 8 | ‘1000-01-01 00:00:00’ 到 ‘9999-12-31 23:59:59’ | YYYY-MM-DD hh:mm:ss | 混合日期和时间值 |
timestamp | 4 | ‘1970-01-01 00:00:01’ UTC 到 ‘2038-01-19 03:14:07’ UTC结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07 | YYYY-MM-DD hh:mm:ss | 混合日期和时间值,时间戳 |
③ 字符串类型
char(n) 和 varchar(n) 中括号中 n 代表字符的个数,并不代表字节个数,比如 CHAR(30) 就可以存储 30 个字符。
类型 | 大小 | 用途 |
---|---|---|
char | 0-255 bytes | 定长字符串 |
varchar | 0-65535 bytes | 变长字符串 |
tinyblob | 0-255 bytes | 不超过 255 个字符的二进制字符串 |
tinytext | 0-255 bytes | 短文本字符串 |
blob | 0-65 535 bytes | 二进制形式的长文本数据 |
text | 0-65 535 bytes | 长文本数据 |
mediumblob | 0-16 777 215 bytes | 二进制形式的中等长度文本数据 |
mdeiumtext | 0-16 777 215 bytes | 中等长度文本数据 |
longblob | 0-4 294 967 295 bytes | 二进制形式的极大文本数据 |
longtext | 0-4 294 967 295 bytes | 极大文本数据 |
3. 视图操作
二、DML
三、 DQL
四、 DCL
评论
匿名评论隐私政策
✅ 你无需删除空行,直接评论以获取最佳展示效果