当前位置:网站首页>六、MySQL之数据定义语言(一)
六、MySQL之数据定义语言(一)
2022-06-22 02:52:00 【xiaoweiwei99】

文章目录
??前言
这一节介绍MySQL中的DDL(数据定义语言),内容主要包括数据定义语言之库的管理、表的管理、数据类型以及常见的约束(下节单独介绍)等内容。
?? 库的管理
创建(create)、修改(alter)、删除(drop)
- 库的创建(create)
语法
create database 【if not exists】库名【character set 字符集名】;
- 库的修改(alter)
语法
alter database 库名 character set 字符集名;可以更改库的字符集:
ALTER DATEBASE books CHARACTER SET gbk;
- 库的删除
语法
drop database 【if exists】 库名;
??表的管理
创建(create)、修改(alter)、删除(drop)
- 表的创建(create)??
语法
create table 【if not exists】表名(
字段名 字段类型【约束】,
字段名 字段类型【约束】,
…
字段名 字段类型【约束】
)
- 表的修改
①添加列
alter table 表名 add column 列名 类型【first | after 字段名】;
②修改列的类型或约束
alter table 表名 modify column 列名 新类型【新约束】;
③修改列名
alter table 表名 change column 旧列名 新列名 类型;
④删除列
alter table 表名 drop column 列名;
⑤修改表名
alter table 表名 rename 【to】新表名;
- 表的删除
语法
drop table 【if exists】表名;
- 表的复制
①复制表的结构
create table 表名 like 旧表;
②复制表的结构+数据
create table 表名
select 查询列表 from 旧表【where 筛选】;
??数据类型
- 数值型
①整型
类型
字节
tinyint
1
smallint
2
mediumint
3
int/integer
4
bigint
8
特点
①都可以设置无符号和有符号,默认有符号,通过unsigned设置无符号
②如果超出了范围,会报out or range 异常,插入临界值
③长度可以不指定,默认会有一个长度
长度代表显示的最大宽度,如果不够则左边用0填充,但需要搭配zerofill ,并且默认变为无符号整型
②浮点型
定点数
decimal(M,D)
浮点数
float(M,D) double(M,D)
特点
①M代表整数部位+小数部位的个数,D代表小数部位
②如果超出范围,则报out or range 异常,并且插入临界值
③M和D都可以省略,但对于定点数,M默认为10,D默认为0
④如果精度要求较高,则优先考虑使用定点数
- 字符型
char 、varchar、binary、varbinary、enum、set、text、blob
== 常用两个==:
char:固定长度的字符,写法为char(M),最大长度不能超过M,其中M可以省略,默认为1
varchar:可变长度的字符,写法为varchar(M),最大长度不能超过M,其中M不可以省略
3.日期型
year年
date日期
time时间
datetime日期+时间:字节8
timestamp日期+时间 :字节4 比较容易受时区、语法模式、版本的影响,更能反映当前时区的真实时间
??总结
这一节主要介绍了MySQL中的数据定义语言,其中常见的约束这一部分内容较多,将在下一节单独展开介绍,欢迎小伙伴们关注并学习!

边栏推荐
- 圖數據庫ONgDB Release v-1.0.2
- Day21qt mouse event 2021-11-01
- Official release of ideal L9: retail price of 459800 yuan will be delivered before the end of August
- [8. One dimensional prefix and]
- xpm_memory_tdpram原语的完整使用实例
- 【5. 高精度减法】
- [5. high precision subtraction]
- Force buckle 239 Sliding window Max
- 【一起上水硕系列】Day Two
- June25,2022 PMP Exam clearance manual-4
猜你喜欢

Architecture and practice of vivo container cluster monitoring system

Ioerror: no translation files found for default language zh cn Solutions for

自适应批作业调度器:为 Flink 批作业自动推导并行度
![[7. high precision division]](/img/d9/060cf961db7414b2900ba76b5d8a52.png)
[7. high precision division]
![[2. merge sort]](/img/60/5e87cffabd91af0155ae681f5bf0ba.png)
[2. merge sort]

Zhixiang Jintai rushes to the scientific innovation board: the annual revenue is 39.19 million, the loss is more than 300million, and the proposed fund-raising is 4billion
![[go language] we should learn the go language in this way ~ a comprehensive learning tutorial on the whole network](/img/91/324d04dee0a191725a0b8751375005.jpg)
[go language] we should learn the go language in this way ~ a comprehensive learning tutorial on the whole network

图数据库ONgDB Release v-1.0.2

自动化工具-监测文件的变化

Database daily question - day 19: top ranked travelers
随机推荐
ACL 2022 | multilingual knowledge map reasoning based on self supervised graph alignment
使用 Neo4j 沙箱学习 Neo4j 图数据科学 GDS
最新發布:Neo4j 圖數據科學 GDS 2.0 和 AuraDS GA
Write your own kubernetes controller
Sword finger offer 28 Symmetric binary tree
并查集dsu
[8. One dimensional prefix and]
Microblog closes publishing multiple part-time fraud information illegal accounts: how to crack down on data fraud
银联支付 返回商户 Nignx post请求405
Sword finger offer 57 Next node of binary tree
web框架概述与程序开发
Write the processing framework for playing
With the acceleration of industry wide digital transformation, what kind of storage will be more popular?
Force buckle 160 Intersecting linked list
Conference chat room - development documents
Day17QtQLcdNumber2021-10-22
华阳智能冲刺深交所:拟募资4亿 复星惟盈是股东
Moorish voting
【9. 子矩阵和】
交通标志分类