当前位置:网站首页>MySQL data table operation DDL & data type
MySQL data table operation DDL & data type
2022-07-05 12:12:00 【ziyi813】
MySQL Basic operation - DDL
1、DDL explain
DDL(Data Definition Language) Data definition language
- Common operations on Database
- Common operations on table structure
- Modify table structure
Database -> Table
To the database 、 Table operations , No data involved
2、DDL Database operation
function | SQL |
---|---|
View all databases | show databases; |
Create database | create database [if not exists] mydb1 [charset=utf8] |
Switch 、 Select database | use mydb1; |
Delete database | drop database [if exists] mydb1; |
Modify database code | alter database mydb1 character set utf8; |
3、DDL Data table operation
Create a table format
create table [if not exists] Table name (
Field name 1 type [( Width )] [ constraint condition ] [comment ‘ Field description ’],
Field name 2 type [( Width )] [ constraint condition ] [comment ‘ Field description ’]
)[ Some settings of the watch ];
Creating a table is to build an empty table , Specify the name of this table , This table has several columns , What is the name of each column , And the storage data type of each column .
use mydb1;
create table if not exists student (
sid int,
name varchar(20),
age int,
birth date,
address varchar(20)
);
data type
When defining field columns, you need to specify the data type and type size .
value type
integer , floating-point
type | size | Range ( A signed ) | Range ( Unsigned ) | purpose |
---|---|---|---|---|
TINYINT | 1 byte | (-128,127) | (0,255) | Small integer value |
SMALLINT | 2byte | (-32768, 32767) | (0,65535) | Whole size The number |
MEDIUMINT | 3byte | (-8388608, 8388607) | (0, 16777215) | Large integer value |
INT perhaps INTGER | 4byte | (-2147483648, 2147483647) | (0, 4294967295) | Large integer value |
BIGINT | 8byte | (-9223372036854775808, 9223372036854775808) | (0, 18446744073709551615) | Maximal integer The number |
DOUBLE | 8byte | (-17976931348623157E+308, 17976931348623157E+308) | (0,22250738585072014E-308, 17976931348623157E+308) | Double precision floating point value |
DECIMAL | Depend on M and D Value | Depend on M and D Value | Small value | |
String type
type | size | purpose |
---|---|---|
char | 0-255bytes | Fixed length fixed character string |
varchar | 0-65536 bytes | Variable length delimiter string |
tinyblog | 0-255bytes | No more than 255 Binary string of characters |
tinytext | 0-255bytes | Text string |
blog | 0-65535 bytes | Long text data in binary form |
text | 0-65535 bytes | Long text data |
mediumblog | 0-16777215 bytes | Medium length text data in binary form |
mediumtext | 0-16777215 bytest | Medium length text data |
longblog | 0-4294967295 bytes | Maximum text data in binary form |
longtext | 0-4294967295 bytes | Large text data |
Date and time type
type | size bytes | Range | Format | purpose |
---|---|---|---|---|
DATE | 3 bytes | 1000-01-01 9999-12-31 | YYYY-MM-DD | Date value |
TIME | 3 bytes | -838:59:59 / 838:59:59 | HH:MM:SS | Time value Or duration |
YEAR | 1 bytes | 1901/2155 | yyyy | The year is worth |
DATETIME | 8 bytes | 1000-01-01 00:00:00 / 9999-12-31 23:59:59 | YYYY-MM-DD HH:MM:SS | Mix date and time values |
TIMESTAMP | 4 bytes | 1970-01-01 00:00:00 /2038 The end time is the 2147483647 second , Beijing time. 2038-1-19 11:14:07 , GMT 2038 year 1 month 19 Early morning 03:14:07 | YYYYMMDD HH:MM:SS | Mix date and time values , Time stamp |
4、 Common operations on table structure
function | SQL |
---|---|
View all table names of the current database | show tables; |
View the creation statement of the specified table | show create table Table name ; |
View table structure | desc Table name ; |
Delete table | drop table Table name ; |
Modify the table structure format
Modify the table to add columns
Grammar format :
alter table Table name add Name type ( length ) [ constraint ]
Example :
– by student Add a new field to the table as : Is don't dept The type is varchar(20)
ALTER TABLE student ADD dept
VARCHAR(20)
Change column name and type
Grammar format
alter table Table name change Old column names New column names type ( length ) [ constraint ];
Example :
– by student Tabular dept Replace field with department varchar(30)
ALTER TABLE student change dept
department VARCHAR(30);
Modify the order of fields in the table
alter table table_name modify Property name data type first|after Property name 2;
-- Example 1, take isdel Move field to first place
alter table table_name modify isdel tinyint(2) first;
-- Example 2, take isdel Move field to id After column
alter table table_name modify isdel tinyint(2) after id;
Modify table delete column
Grammar format :
alter table Table name drop Name ;
Example :
– Delete student Of table name department Column
ALTER TABLE student DROP department;
Modify the name of the table
Grammar format :
rename table Table name to The new name of the table ;
Example :
– Will table student Change its name to stu
RENAME TABLE student to stu;
边栏推荐
- 【TFLite, ONNX, CoreML, TensorRT Export】
- Thoughts and suggestions on the construction of intelligent management and control system platform for safe production in petrochemical enterprises
- 网络五连鞭
- MySQL splits strings for conditional queries
- ACID事务理论
- Pytorch linear regression
- Intern position selection and simplified career development planning in Internet companies
- Why do you always fail in automated tests?
- Simple production of wechat applet cloud development authorization login
- 一类恒等式的应用(范德蒙德卷积与超几何函数)
猜你喜欢
Why do you always fail in automated tests?
Understand kotlin from the perspective of an architect
强化学习-学习笔记3 | 策略学习
报错ModuleNotFoundError: No module named ‘cv2.aruco‘
Matlab label2idx function (convert the label matrix into a cell array with linear index)
Select drop-down box realizes three-level linkage of provinces and cities in China
Redis cluster (master-slave) brain crack and solution
Redis集群的重定向
嵌入式软件架构设计-消息交互
Two minutes will take you to quickly master the project structure, resources, dependencies and localization of flutter
随机推荐
2022年国内云管平台厂商哪家好?为什么?
yolov5目標檢測神經網絡——損失函數計算原理
Video networkState 属性
Yolov 5 Target Detection Neural Network - Loss Function Calculation Principle
Pytorch weight decay and dropout
一款新型的智能家居WiFi选择方案——SimpleWiFi在无线智能家居中的应用
想问问,如何选择券商?在线开户是很安全么?
【使用TensorRT通过ONNX部署Pytorch项目】
Network five whip
Troubleshooting of high memory usage of redis in a production environment
【load dataset】
pytorch-多层感知机MLP
Check the debug port information in rancher and do idea remote JVM debug
[mainstream nivida graphics card deep learning / reinforcement learning /ai computing power summary]
Redis集群的重定向
手机 CPU 架构类型了解
调查显示传统数据安全工具在60%情况下无法抵御勒索软件攻击
Two minutes will take you to quickly master the project structure, resources, dependencies and localization of flutter
图像超分实验:SRCNN/FSRCNN
[yolov3 loss function]