当前位置:网站首页>MySQL基础之写表(创建表)
MySQL基础之写表(创建表)
2022-04-23 20:39:00 【小简(JanYork)】
工具
市面上的SQL
可视化工具不少,我一般常用的主要就是这两个。
当然,IDEA也是集成了数据库可视化功能的。除了这些,还有DBeaver、SQLyog等等。
我比较喜欢DataGrip
,我就用这个演示了。不过这个的界面就没有Navicat
优美了,不过个人感觉功能强大许多。
写表
这里,我已经创建好了一个Demo
数据库。
我们先来了解一下创建表的语法。
创建表
USE Demo;
# 语法如下
# CREATE TABLE [IF NOT EXISTS] 表名(字段内容)
CREATE TABLE IF NOT EXISTS class(
Id INT(4) COMMENT 'ID号',
Name VARCHAR(10) COMMENT '姓名'
);
IF NOT EXISTS
可以省略不写。
CREATE TABLE class(
Id INT(4) COMMENT 'ID号',
Name VARCHAR(10) COMMENT '姓名'
);
记住**CREATE TABLE
**用于创建表。
创建字段
()
里面是表的字段,写入字段的格式如下。
# 字段名 数值类型 COMMENT '字段备注'
Id INT(4) COMMENT 'ID号',
Name VARCHAR(10) COMMENT '姓名'
- 如果数值类型要设置长度,我们可以在后面接上
()
,里面填入长度值。 COMMENT
关键词是用于创建字段的对应备注,备注必须在其后。
字段约束与属性设置
我们可以通过某些关键词为字段设置约束与某些属性。
Id INT(4) COMMENT 'ID号' PRIMARY KEY ,
id INT(4) COMMENT 'ID号' PRIMARY KEY AUTO_INCREMENT NOT NULL UNIQUE KEY
比如,PRIMARY KEY
关键词可以将字段设置为主键。
位置可变!对应语法格式即可,字段 数据类型 [ 字段属性 | 约束 ] [ 索引 ] [ 字段备注 ]
。
约束或属性 | 说明 |
---|---|
主键约束 | PRIMARY KEY |
外键约束 | FOREIGN KEY |
非空约束 | NOT NULL |
自动增长 | AUTO_INCREMENT |
唯一约束 | UNIQUE KEY |
默认约束 | DEFAULT |
对于这些约束,需要知道详细作用的话,请前往浏览器查看,嘿嘿!
常见数据类型
数值类型
类型 | 大小 | 用途 |
---|---|---|
TINYINT | 1 Bytes | 小整数值 |
SMALLINT | 2 Bytes | 大整数值 |
MEDIUMINT | 3 Bytes | 大整数值 |
INT或INTEGER | 4 Bytes | 大整数值 |
BIGINT | 8 Bytes | 极大整数值 |
FLOAT | 4 Bytes | 单精度 浮点数值 |
DOUBLE | 8 Bytes | 双精度 浮点数值 |
DECIMAL | 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 | 小数值 |
日期类型
类型 | 大小 ( bytes) | 格式 | 用途 |
---|---|---|---|
DATE | 3 | YYYY-MM-DD | 日期值 |
TIME | 3 | HH:MM:SS | 时间值或持续时间 |
YEAR | 1 | YYYY | 年份值 |
DATETIME | 8 | YYYY-MM-DD HH:MM:SS | 混合日期和时间值 |
TIMESTAMP | 4 | YYYYMMDD HHMMSS | 混合日期和时间值,时间戳 |
字符串类型
类型 | 大小 | 用途 |
---|---|---|
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 | 二进制形式的中等长度文本数据 |
MEDIUMTEXT | 0-16 777 215 bytes | 中等长度文本数据 |
LONGBLOB | 0-4 294 967 295 bytes | 二进制形式的极大文本数据 |
LONGTEXT | 0-4 294 967 295 bytes | 极大文本数据 |
版权声明
本文为[小简(JanYork)]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_60750453/article/details/124305131
边栏推荐
- Bash script learning -- for loop traversal
- 启牛学堂有用吗,推荐的证券账户是否安全
- Unity solves Z-fighting
- JS arrow function user and processing method of converting arrow function into ordinary function
- [graph theory brush question-4] force deduction 778 Swimming in a rising pool
- Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom
- Case of the third day of go language development fresh every day project - news release system II
- How to use PM2 management application? Come in and see
- Use of node template engine
- Come in and teach you how to solve the problem of port occupation
猜你喜欢
堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
【PTA】整除光棍
Tensorflow 2 basic operation dictionary
GO語言開發天天生鮮項目第三天 案例-新聞發布系統二
BMP JPEG 图片转换为矢量图像 ContourTrace
16MySQL之DCL 中 COMMIT和ROllBACK
Devaxpress report replay: complete the drawing of conventional two-dimensional report + histogram + pie chart
Matlab: psychtoolbox installation
随机推荐
JSX syntax rules
Leetcode 1346. Check whether integers and their multiples exist
LeetCode 116. 填充每个节点的下一个右侧节点指针
Easy to use nprogress progress bar
ArcGIS js api 4. X submergence analysis and water submergence analysis
CONDA environment management command
bounding box iou
go slice
Async function ------ ES6
LeetCode 232、用栈实现队列
6-5 字符串 - 2. 字符串复制(赋值) (10 分)C语言标准函数库中包括 strcpy 函数,用于字符串复制(赋值)。作为练习,我们自己编写一个功能与之相同的函数。
Identifier CV is not defined in opencv4_ CAP_ PROP_ FPS; CV_ CAP_ PROP_ FRAME_ COUNT; CV_ CAP_ PROP_ POS_ Frames problem
GO语言开发天天生鲜项目第三天 案例-新闻发布系统二
Analysis of the relationship between generalized Bim and CAD under the current background
Vscode download speed up
【SQL】字符串系列2:将一个字符串根据特定字符分拆成多行
C migration project record: modify namespace and folder name
Identification of bolt points in aerial photography based on perception
【PTA】整除光棍
Es error: request contains unrecognized parameter [ignore_throttled]