当前位置:网站首页>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;
边栏推荐
猜你喜欢
Two minutes will take you to quickly master the project structure, resources, dependencies and localization of flutter

Multi table operation - sub query
![[mainstream nivida graphics card deep learning / reinforcement learning /ai computing power summary]](/img/1a/dd7453bc5afc6458334ea08aed7998.png)
[mainstream nivida graphics card deep learning / reinforcement learning /ai computing power summary]

Principle of persistence mechanism of redis

pytorch-权重衰退(weight decay)和丢弃法(dropout)

codeforces每日5题(均1700)-第五天

Sentinel sentinel mechanism of master automatic election in redis master-slave

【TFLite, ONNX, CoreML, TensorRT Export】

【Win11 多用户同时登录远程桌面配置方法】

谜语1
随机推荐
Halcon template matching actual code (I)
JS for loop number exception
【yolov3损失函数】
leetcode:1200. Minimum absolute difference
图像超分实验:SRCNN/FSRCNN
Halcon 模板匹配实战代码(一)
Simply solve the problem that the node in the redis cluster cannot read data (error) moved
redis主从中的Master自动选举之Sentinel哨兵机制
Understand kotlin from the perspective of an architect
【pytorch 修改预训练模型:实测加载预训练模型与模型随机初始化差别不大】
Pytorch linear regression
报错ModuleNotFoundError: No module named ‘cv2.aruco‘
II. Data type
信息服务器怎么恢复,服务器数据恢复怎么弄[通俗易懂]
查看rancher中debug端口信息,并做IDEA Remote Jvm Debug
调查显示传统数据安全工具在60%情况下无法抵御勒索软件攻击
强化学习-学习笔记3 | 策略学习
byte2String、string2Byte
Xi IO flow
多表操作-自关联查询