当前位置:网站首页>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
边栏推荐
- Devexpress 14.1 installation record
- Modeling based on catiav6
- Some basic knowledge of devexpress report development
- 6-5 string - 2 String copy (assignment) (10 points) the C language standard function library includes the strcpy function for string copy (assignment). As an exercise, we write a function with the sam
- Communication between RING3 and ring0
- LeetCode 1351、统计有序矩阵中的负数
- 上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
- BMP JPEG picture to vector image contourtrace
- Leetcode 994, rotten orange
- Bracket matching -- [implementation of one-dimensional array]
猜你喜欢
随机推荐
Some basic configurations in interlij idea
BMP JPEG picture to vector image contourtrace
Unity ECS dots notes
Matlab matrix index problem
Queue template code
Bracket matching -- [implementation of one-dimensional array]
【PTA】L1-002 打印沙漏
Easy to use nprogress progress bar
Shanghai responded that "flour official website is an illegal website": neglect of operation and maintenance has been "hacked", and the police have filed a case
bounding box iou
DOS command of Intranet penetration
Analysis of the relationship between generalized Bim and CAD under the current background
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
Vscode download speed up
star
The ODB model calculates the data and outputs it to excel
Scrapy教程 - (2)寫一個簡單爬蟲
Summary and effect analysis of methods for calculating binocular parallax
Learn to C language fourth day
【SQL】字符串系列2:将一个字符串根据特定字符分拆成多行





![[PTA] l1-002 printing hourglass](/img/9e/dc715f7debf7edb7a40e9ecfa69cef.png)

