当前位置:网站首页>六、MySQL之数据定义语言(一)
六、MySQL之数据定义语言(一)
2022-07-03 09:27:00 【sherry 96】

前言
这一节介绍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不可以省略日期型
year年
date日期
time时间
datetime日期+时间:字节8
timestamp日期+时间 :字节4 比较容易受时区、语法模式、版本的影响,更能反映当前时区的真实时间
总结
这一节主要介绍了MySQL中的数据定义语言,其中常见的约束这一部分内容较多,将在下一节单独展开介绍,欢迎小伙伴们关注并学习!

边栏推荐
- Data classification: support vector machine
- 熵值法求权重
- Leetcode - 1172 plate stack (Design - list + small top pile + stack))
- Codeup: word replacement
- 20220609其他:多数元素
- [graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
- CV learning notes - image filter
- Numpy Foundation
- 波士顿房价预测(TensorFlow2.9实践)
- Out of the box high color background system
猜你喜欢

重写波士顿房价预测任务(使用飞桨paddlepaddle)

Anaconda安装包 报错packagesNotFoundError: The following packages are not available from current channels:

CV learning notes - edge extraction

CV learning notes alexnet

2.2 DP: Value Iteration & Gambler‘s Problem

Implementation of "quick start electronic" window dragging

GAOFAN Weibo app

CV learning notes - BP neural network training example (including detailed calculation process and formula derivation)

Tensorflow—Neural Style Transfer

Hands on deep learning pytorch version exercise solution - 2.6 probability
随机推荐
What did I read in order to understand the to do list
Mise en œuvre d'OpenCV + dlib pour changer le visage de Mona Lisa
QT creator uses OpenCV Pro add
20220605数学:两数相除
Anaconda installation package reported an error packagesnotfounderror: the following packages are not available from current channels:
『快速入门electron』之实现窗口拖拽
[graduation season] the picture is rich, and frugality is easy; Never forget chaos and danger in peace.
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow
Stroke prediction: Bayesian
20220608其他:逆波兰表达式求值
20220531 Mathematics: Happy numbers
20220609 other: most elements
2-program logic
An open source OA office automation system
Simple real-time gesture recognition based on OpenCV (including code)
Leetcode - 705 design hash set (Design)
Inverse code of string (Jilin University postgraduate entrance examination question)
20220601 Mathematics: zero after factorial
Connect Alibaba cloud servers in the form of key pairs
Standard library header file