当前位置:网站首页>MySQL DDL (data definition language)
MySQL DDL (data definition language)
2022-07-21 20:02:00 【Jiahe Jianing Papa】
Catalog
- One 、 brief introduction
- Two 、 database
- 3、 ... and 、 data type
- Four 、 Data sheet
- 5、 ... and 、 Primary key and index
- 6、 ... and 、 View
- Study plan :
One 、 brief introduction
DDL(Data Definition Language) Data definition language , Scope of application : For some objects in the database ( for example ,database,TABLE) Conduct management , Such as Create,ALTER and Drop,DDL The operation is implicitly submitted , You can't rollback!
Two 、 database
Operate the database , First you have to log in .
mysql -u user name -p User password
[email protected]:/# mysql -u alian -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 64
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
2.1、 Display database
Be careful not to lose that s
show databases;
2.2、 Create database
Simple writing , Adopt the default settings of the database
CREATE DATABASE Database name ;
Recommend writing , Set up Base character set and Database collation , The name of the database cannot be changed
CREATE DATABASE IF NOT EXISTS Database name
DEFAULT CHARACTER SET utf8mb4
DEFAULT COLLATE utf8mb4_general_ci;
2.3、 Select database
USE Database name
2.4、 Delete database
DROP database Database name
3、 ... and 、 data type
3.1、 value type
type | size ( bytes) | Range ( A signed ) | Range ( Unsigned ) | purpose |
---|---|---|---|---|
TINYINT | 1 | (-128,127) | (0,255) | Small integer value |
SMALLINT | 2 | (-32 768,32 767) | (0,65 535) | Large integer value |
MEDIUMINT | 3 | (-8 388 608,8 388 607) | (0,16 777 215) | Large integer value |
INT or INTEGER | 4 | (-2 147 483 648,2 147 483 647) | (0,4 294 967 295) | Large integer value |
BIGINT | 8 | (-9,223,372,036,854,775,808,9 223 372 036 854 775 807) | (0,18 446 744 073 709 551 615) | Maximum integer value |
FLOAT | 4 | (-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) | 0,(1.175 494 351 E-38,3.402 823 466 E+38) | Single precision floating point values |
DOUBLE | 8 | (-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) | 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) | Double precision floating point value |
DECIMAL | Yes DECIMAL(M,D) , If M>D, by M+2 Otherwise D+2 | Depend on M and D Value | Depend on M and D Value | Small value |
3.2、 Date and time type
type | size ( bytes) | Range | Format | purpose |
---|---|---|---|---|
DATE | 3 | 1000-01-01/9999-12-31 | YYYY-MM-DD | Date value |
TIME | 3 | ‘-838:59:59’/‘838:59:59’ | HH:MM:SS | Time value or duration |
YEAR | 1 | 1901/2155 | YYYY | The year is worth |
DATETIME | 8 | 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 | 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 Japan In the morning 03:14:07 | YYYYMMDD HHMMSS | Mix date and time values , Time stamp |
3.3、 String type
type | size ( bytes) | purpose |
---|---|---|
CHAR | 0-255 | Fixed length string |
VARCHAR | 0-65535 | Variable length string |
TINYBLOB | 0-255 | No more than 255 Binary string of characters |
TINYTEXT | 0-255 | Text string |
BLOB | 0-65 535 | Long text data in binary form |
TEXT | 0-65 535 | Long text data |
MEDIUMBLOB | 0-16 777 215 | Medium length text data in binary form |
MEDIUMTEXT | 0-16 777 215 | Medium length text data |
LONGBLOB | 0-4 294 967 295 | Maximum text data in binary form |
LONGTEXT | 0-4 294 967 295 | Large text data |
3.4、 About int( length )
- If a field has the unsigned and fill zero attributes , So no matter what value this field stores , The length of the value will be consistent with the set display width , For example, insert 1, Is shown as 00000000001, The left side is mended 10 Zero until the length reaches 11 position
- Setting the display width of a field does not limit the range of values stored in the field , If you set int(6), But it can still store 99999999
- The set character width is only valid when the numerical length does not meet the width , Setting up int(6), Insert 1 when , Insufficient length 6, So add... On the left 5 A zero , If you insert 888888888, The display width doesn't work
Four 、 Data sheet
4.1、 Create table
Create the correct posture instructions for the table
CREATE TABLE `user` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT ' Primary key ',
`user_id` INT UNSIGNED NOT NULL DEFAULT '1000' COMMENT ' Student number ',
`user_name` VARCHAR(10) NOT NULL DEFAULT '' COMMENT ' The student's name ',
`gender` CHAR(1) NOT NULL DEFAULT '1' COMMENT ' Gender ',
`birthday` DATE NOT NULL DEFAULT '1970-01-01' COMMENT ' Birthday ',
`home_address` VARCHAR(100) NOT NULL DEFAULT '' COMMENT ' Home address ',
`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ' Creation time ',
`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT ' Update time ',
PRIMARY KEY (`id`),
UNIQUE KEY `idx_user_id` (`user_id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT=' Student information sheet ';
- mysql Create a table and try to keep a self growing id, And set to UNSIGNED
- The naming of fields should be meaningful , Combined with programming language, the hump mode can be changed to Underline Segmentation will automatically map
- The type of field must be reasonable , And set the appropriate length , It is suggested to add field description COMMENT
- Set the default value for any field , Try to avoid a value of NULL The situation of
- create_time and update_time General business table will have , One is the time of creation , Then it won't change , One is as the data changes , Time will also be shown , Very practical
- Set the index according to the situation , Naming conventions for indexes ( primary key :pk_ namely primary key; unique index :uk_ namely unique key; General index :idx_ namely index For short )
- The business table engine is generally InnoDB, Because it supports transactions
- The character set of the table is set to utf8mb4, Sort rule utf8mb4_general_ci
- Finally, remember to add the notes of the table COMMENT
4.2、 Display table
show TABLEs;
4.3、 View table structure
desc Table name of the database ;
4.4、 Show the statement that created the table
show create TABLE Table name of the database ;
4.5、 Modify table
4.5.1、 Modify table name or comment
ALTER TABLE Original database table name rename to New database table name ;
ALTER TABLE Database table name COMMENT=' This is the note of the table ';
4.5.2、 Add fields
ALTER TABLE Database table name add The name of the field to add data type constraint condition Field description after Field name ;
# For example student Add one at the end of the table age Field
ALTER TABLE student add `age` TINYINT UNSIGNED NOT NULL DEFAULT '0' COMMENT ' Age ';
# For example student In the table gender Add a... After the field age Field
ALTER TABLE student add `age` TINYINT UNSIGNED NOT NULL DEFAULT '0' COMMENT ' Age ' after gender;
# If it's in student In the table gender Add a age Field
ALTER TABLE student add `age` TINYINT UNSIGNED NOT NULL DEFAULT '0' COMMENT ' Age ' before gender;
4.5.3、 Modify field name
ALTER TABLE Database table name CHANGE Old field name new field name New field type ;
# If we put student In the table age Change the field to user_age, The default value is changed to 1( In fact, it is to create a field definition and rewrite )
ALTER TABLE student change age user_age TINYINT UNSIGNED NOT NULL DEFAULT '1' COMMENT ' Age ';
4.5.4、 Delete field
ALTER TABLE Database table name DROP Field name to delete ;
# For example, we delete user_age Field ( Because of the attribute of birthday )
ALTER TABLE student DROP user_age;
4.5.5、 Set the default value
## Set mode value
ALTER TABLE Database table name ALTER Field name SET DEFAULT The value to set ;
## hold student Tabular gender Set the default value to 0
ALTER TABLE student ALTER gender SET DEFAULT 0;
4.5.6、 Delete default ( Don't suggest )
## Delete mode value
ALTER TABLE Database table name ALTER Field name DROP DEFAULT;
# For example, delete student Tabular gender The default value of
ALTER TABLE student ALTER gender DROP DEFAULT;
4.6、 Clear the table
TRUNCATE TABLE Table name of the database ;
4.7、 Delete table
DROP TABLE Table name of the database ;
4.8、 Modify the self growth value
# Modify the self increasing value
ALTER TABLE Table name of the database AUTO_INCREMENT= The self increasing value to be set
4.9、 Modify the storage engine of the table
# Modify the storage engine of the table (MyISAM perhaps INNODB)
ALTER TABLE Table name of the database ENGINE= Storage engine name
ALTER TABLE student ENGINE=INNODB;
5、 ... and 、 Primary key and index
5.1、 Add primary key
ALTER TABLE Table name of the database ADD PRIMARY KEY( Field name );
5.2、 Add a composite primary key
ALTER TABLE Table name of the database ADD PRIMARY KEY( Field name 1, Field name 2,...);
5.3、 Delete primary key
ALTER TABLE Table name of the database DROP PRIMARY KEY;
exception : If it is a self growing primary key, you need to remove the self growing setting
# use MODIFY Delete self growth attribute , Be careful MODIFY The primary key attribute cannot be removed
ALTER TABLE student MODIFY id INT UNSIGNED NOT NULL COMMENT ' Primary key ';
# Delete primary key
ALTER TABLE student DROP PRIMARY KEY;
5.4、 Create unique index
CREATE UNIQUE INDEX The index name ON The name of the table ( Table field )
5.5、 Add unique index
ALTER TABLE Table name of the database ADD UNIQUE The index name ( Field name );
# Such as to student Of user_id Add index to field , Index name is idx_user_id
ALTER TABLE student ADD UNIQUE uk_user_id(user_id);
5.6、 Create a normal index
# here length Indicates the first digits of the field , That is, short index
CREATE INDEX The index name ON Table name of the database ( Field name (length))
5.7、 Add a normal index
ALTER TABLE Table name of the database add INDEX The index name ( Field name );
# Such as to student Of user_name Add index to field , Index name is idx_user_name
ALTER TABLE student ADD INDEX idx_user_name(user_name);
5.8、 View the index of the table
SHOW INDEX FROM Table name of the database ;
5.9、 Delete index
# Delete index
ALTER TABLE Table name of the database DROP UNIQUE The index name ;
# For example, delete student On the table user_name Index on field
ALTER TABLE student DROP INDEX idx_user_name;
perhaps
# perhaps
DROP INDEX The index name on Table name of the database ;
# For example, delete student On the table user_name Index on field
DROP INDEX idx_user_name on student;
6、 ... and 、 View
6.1、 View creation
CREATE VIEW View name ( Field 1, Field 2,..., Field n)
AS
SELECT Field 1, Field 2,..., Field n FROM surface
WITH CHECK OPTION;
For example, we put the number and name of the student table in a view
# Create a view with student numbers and names
CREATE VIEW v_student(user_code,user_name)
AS
SELECT user_code,user_name FROM tb_student
WITH CHECK OPTION;
The following is an example of creating a multi table view
# Put the numbers and names of the student table and class table into one view
CREATE VIEW v_class_student(class_code,class_name,user_code,user_name)
AS
SELECT c.class_code,c.class_name,s.user_code,s.user_name FROM tb_class c, tb_student s WHERE c.class_code=s.class_code
WITH CHECK OPTION;
6.2、 View query
# Query view
select * from View name ;
# For example, we created the student view before
SELECT user_code,user_name FROM v_student;
6.3、 View change
# Change the view
create or replace view View name as Query statement ;
6.4、 View delete
# Delete view
drop view if exists View name ;
# Delete student view
drop view if exists v_student;
Study plan :
- CentOS 8 in Docker install MySQL8
- MySQL And DCL( Data control language )
- MySQL And DDL( Data definition language )
- MySQL And DML( Data manipulation language )
- MySQL And DQL( Data query language )- Common keywords
- MySQL And DQL( Data query language )- Link query
- MySQL And DQL( Data query language )- Common function
- MySQL Three paradigms of database design
边栏推荐
猜你喜欢
3D数学之三角公式
Momenta“飞轮式L4”接受夜间长尾场景「像素级」挑战,表现堪比老司机
图表即代码:以代码化的方式构建新一代图形库 —— Feakin
逐步理解深度信念网络
Uniapp develops apps to solve uni Pagescrollto does not take effect
uniapp开发app解决uni.pageScrollTo不生效
My understanding of UI automated testing
如何成功拿到蚂蚁,京东,小米,腾讯等大厂的offer
全志A40i开发板硬件说明书——100%国产+工业级方案(中)
426. Transform binary search tree into sorted two-way linked list
随机推荐
Mes réflexions sur les tests automatisés d'assurance - chômage
Written test compulsory training day 18
STM32 - positioning module atgm336h, data analysis, longitude and latitude extraction
odoo神操作后台调用路由接口
Written test compulsory training day 19
微信小程序支付
Unity ECS 测试Demo
Ens reading notes
Why write unit tests? How to write unit tests?
Learning notes introduction to explain
项目debug|no String-argument constructor/factory method to deserialize from String val
Momenta "flywheel L4" accepted the challenge of "pixel level" in the night long tail scene, and its performance was comparable to that of an old driver
NXP i.MX 8m Mini development board specifications, quad core arm cortex-a53 + arm cortex-m4
10行代码实现微信小程序支付功能,使用小程序云开发实现小程序支付功能(含源码
2022DASCTF MAY web
426. 将二叉搜索树转化为排序的双向链表
Wechat applet payment
NR modulation 4-AM
CentOS 8中Docker安装MySQL8
NXP i.MX 8M Mini 开发板规格参数,四核ARM Cortex-A53 + ARM Cortex-M4