Basic operation statement MySQL database and tables (DDL)

Transfer:   https://blog.csdn.net/weixin_44420511/article/details/91884834

· Fan Li, a senior development engineer:  https://gitchat.csdn.net/column/5c0e149eedba1b683458fd5f?utm_source=feed1812275

I think Hong Meng operating system:  https://blog.csdn.net/caoz/article/details/92802521

Large sites backstage stability technology strategy:  https://gitbook.cn/gitchat/activity/5c304f18d88495255020bc22?utm_source=feed1904184

Technical realm of two hundred thirty-four:  https://blog.csdn.net/xiexiaojing/article/details/93148922

Single table MySQL data not more than 5 million line: is the value of experience, gold or iron rule? Https://blog.csdn.net/LiangGzone/article/details/93144337 

Micro-services transformation and performance optimization in payment scenario is based on:  https://blog.csdn.net/g6U8W7p06dCO99fQ3/article/details/93261859

 

 

1, to create the database:

CREATE DATABASE database name; 
eg.CREATE DATABASE test_ddl;

2, create a table

CREATE TABLE table name (
Column Name Data Type Constraints,
...
);

 the CREATE TABLE table_ddl EG (.
 ID the INT (10) the AUTO_INCREMENT a PRIMARY KEY,
 Test_Content VARCHAR (20 is) the NOT NULL
);

. 3, Table replication
(1) copy the table structure (no data)

CREATE TABLE new table name (SELECT * FROM old table name WHERE 1 = 2);

TABLE copy_table_ddl eg.CREATE (
the SELECT * 
the FROM table_ddl 
the WHERE = 2. 1);

(2) copy the table data structure, and

CREATE TABLE new table name (SELECT * FROM old table name);

eg.CREATE TABLE copy_table_ddl2(
SELECT * 
FROM table_ddl 
);

4.修改表(ALTER)

(1) modify the table name: (RENAME)

ALTER TABLE name RENAME new table the old table name;

TABLE table_ddl the RENAME table_ddl_rename eg.ALTER; 

(2) modify the column name (CHANGE)

ALTER TABLE table name CHANGE old column name for the new column name of the original data type of the original constraints;

TABLE table_ddl_rename eg.ALTER 
the CHANGE Test_Content Content VARCHAR (20 is) the NOT NULL; 

(. 3) modify the data types and constraints (the MODIFY)

ALTER TABLE table MODIFY column names new constraint new data types;

eg.ALTER TABLE table_ddl_rename MODIFY content VARCHAR(100) NULL;

(4)删除列(DROP)

ALTER TABLE table DROP column names;

eg.ALTER TABLE table_ddl_rename DROP COLUMN new_columm;

(5)添加列(ADD)

ALTER TABLE table name ADD COLUMN Column Name Data Type Constraints;

TABLE table_ddl_rename the COLUMN new_columm the ADD eg.ALTER INT (20) UNIQUE; 

(6) Delete the primary key, unique key constraint (first delete the index, then delete the constraint)
# because there is only one primary key, so you can delete without index:

Delete the primary key:
the ALTER TABLE table name PRIMARY KEY DROP;
        
eg.ALTER copy_table_ddl TABLE DROP PRIMARY KEY;

To delete a unique key constraint indexes:
the ALTER TABLE table DROP INDEX index name;

table_ddl_rename the INDEX new_columm the DROP TABLE eg.ALTER;    

5. The other statements:

DESC table name; # lookup table structure
SHOW INDEX FROM table name; # lookup table index

6. constraints:

(1) primary key: a PRIMARY KEY
(2) unique key: UNIQUE
(. 3) non-empty: the NOT NULL
(. 4) increment: the AUTO_INCREMENT
(. 5) Default value: the DEFAULT
(. 6) Foreign Key: foregin KEY 

. . .
--------------------- 
Author: Wu does not envy cents 
Source: CSDN 
Original: https: //blog.csdn.net/weixin_44420511/article/details/91884834 
copyright Disclaimer: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/qq_36688928/article/details/93330814