SQL table creation

1. Introduction to knowledge points:

Table creation, modification and deletion:

  • 1.1 Create a table directly:
  • CREATE TABLE

    [IF NOT EXISTS] tb_name -- Create if it does not exist, skip it if it exists

    (column_name1 data_type1 -- column name and type are required

      [ PRIMARY KEY -- optional constraint, primary key

       | FOREIGN KEY -- foreign key, referencing key values ​​of other tables

       | AUTO_INCREMENT -- auto-increment ID

       | COMMENT comment -- column comment (comment)

       | DEFAULT default_value -- default value

       | UNIQUE -- a unique constraint that does not allow two records to have the same column value

       | NOT NULL -- the column is not null

      ], ...

    ) [CHARACTER SET charset] -- character set encoding

    [COLLATE collate_value] -- rules for column sorting and comparison (whether case sensitive, etc.)

  • 1.2 Create a table by copying the table structure from another table: CREATE TABLE tb_name LIKE tb_name_old

  • 1.3 Create a table from the query results of another table: CREATE TABLE tb_name AS SELECT * FROM tb_name_old WHERE options

  • 2.1 Modification table: ALTER TABLE 表名 修改选项 . Collection of options:

1

2

3

4

5

6

7

8

{ ADD COLUMN <column name> <type> -- add column

 | CHANGE COLUMN <old column name> <new column name> <new column type> -- modify column name or type

 | ALTER COLUMN <column name> { SET DEFAULT <default value> | DROP DEFAULT } -- modify/delete the default value of the column

 | MODIFY COLUMN <column name> <type> -- modify column type

 | DROP COLUMN <column name> -- delete a column

 | RENAME TO <new table name> -- modify the table name

 | CHARACTER SET <character set name> -- modify the character set

 | COLLATE <collation rule name> } -- Modify collation rules (used for comparison and sorting)

  • 3.1 Delete the table: DROP TABLE [IF EXISTS] table name 1 [, table name 2].

Detailed analysis:

  • Auto increment ID: AUTO_INCREMENT;
  • Set the primary key: PRIMARY KEY;
  • Unique constraint: UNIQUE
  • Not-null constraint: NOT NULL
  • Set default value: DEFAULT 0
  • Current timestamp: CURRENT_TIMESTAMP
  • Comment/Note: COMMENT
  • If the table has already been created, return normally: IF NOT EXISTS

2. Case

(1) There is a user information table, which contains the information of users who have registered on the platform for many years. With the continuous growth of the Niuke platform, the number of users is increasing rapidly. The user splits out a new table.

The original user information table:

Filed Type Null Key Default Extra Comment
id int(11) NO AT (NULL) auto_increment auto-increment ID
uid int(11) NO UNI (NULL) User ID
nick_name varchar(64) YES (NULL) Nick name
achievement int(11) YES 0 achievement value
level int(11) YES (NULL) user level
job varchar(32) YES (NULL) career direction
register_time datetime YES CURRENT_TIMESTAMP Registration time

As a data analyst, please create a high-quality user information table user_info_vip , the table structure is consistent with the user information table.

--T1:
CREATE TABLE IF NOT EXISTS user_info_vip
(id int PRIMARY KEY AUTO_INCREMENT COMMENT '自增ID',
 uid int UNIQUE NOT NULL COMMENT '用户ID',
 nick_name	varchar(64) COMMENT '昵称',
 achievement int DEFAULT 0 COMMENT '成就值',
 `level` int COMMENT '用户等级',
 job varchar(32) COMMENT '职业方向',
 register_time datetime DEFAULT CURRENT_TIMESTAMP COMMENT '注册时间')
 CHARACTER SET utf8 COLLATE utf8_general_ci;

--T2:
create table user_info_vip(
id int(11) not null primary key auto_increment comment '自增ID',
uid int(11) not null unique key comment '用户ID',
nick_name varchar(64) comment '昵称',
achievement int(11) default 0 comment '成就值',
level int(11) comment '用户等级',
job varchar(32) comment '职业方向',
register_time datetime default current_timestamp comment '注册时间'
) default charset=utf8

The output you should return is shown in the following table. Please write the table creation statement to record all the restrictions and instructions in the table into the table.

Filed Type Null Key Default Extra Comment
id int(11) NO AT auto_increment auto-increment ID
uid int(11) NO UNI User ID
nick_name varchar(64) YES Nick name
achievement int(11) YES 0 achievement value
level int(11) YES user level
job varchar(32) YES career direction
register_time datetime YES CURRENT_TIMESTAMP Registration time

Remarks:
1. The background will use the SHOW FULL FIELDS FROM user_info_vip statement to compare the output results

2. If the table has been created by other analysts, just return it normally.

Guess you like

Origin blog.csdn.net/weixin_48272780/article/details/128328964