当前位置:网站首页>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
边栏推荐
- go struct
- 內網滲透之DOS命令
- Es index (document name) fuzzy query method (database name fuzzy query method)
- XXXI` Prototype ` displays prototype properties and`__ proto__` Implicit prototype properties
- Learn to C language fourth day
- Go zero framework database avoidance Guide
- Unity animation creates sequence frame code and generates animationclip
- Go language development Daily Fresh Project Day 3 Case - Press Release System II
- Devaxpress report replay: complete the drawing of conventional two-dimensional report + histogram + pie chart
- Experience of mathematical modeling in 18 year research competition
猜你喜欢
A useless confession artifact
[matlab 2016 use mex command to find editor visual studio 2019]
Latest investigation and progress of building intelligence based on sati
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
[PTA] l1-002 printing hourglass
【PTA】L1-002 打印沙漏
Development of Matlab GUI bridge auxiliary Designer (functional introduction)
Matlab matrix index problem
The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
C migration project record: modify namespace and folder name
随机推荐
【PTA】L1-002 打印沙漏
黑客的入侵方式你知道几种?
BMP JPEG 图片转换为矢量图像 ContourTrace
Installation and use of NVM
go-zero框架数据库方面避坑指南
LeetCode 116. Populate the next right node pointer for each node
The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
Plato farm is one of the four largest online IEOS in metauniverse, and the transaction on the chain is quite high
Easy to use nprogress progress bar
How to use PM2 management application? Come in and see
学会打字后的思考
一. js的深拷贝和浅拷贝
Vulnhub DC: 1 penetration notes
Create vs project with MATLAB
Unity ECS dots notes
BMP JPEG picture to vector image contourtrace
bounding box iou
Unity asset import settings
LeetCode 1351、统计有序矩阵中的负数
Solution: NPM err! code ELIFECYCLE npm ERR! errno 1