Control statements of MySQL data tables

Bowen outline:

  • First, the constraints related statements
    • The primary key constraint
    • Non-empty constraint
    • The only setting values
    • Set the default value of the column
    • Set from value-added
  • Second, using the ALTER command
    • Modifying the length of the data value column
    • Modify field name
    • Insert a new field to the table
    • When you add a field to add a constraint
    • Add a foreign key
    • Remove the foreign key
    • Remove Columns
    • Modify the order of the columns
    • Delete table

First, the constraints related statements

1, the primary key constraint (primary key constraint requires data primary key column only, and does not allow empty)

#创建库
mysql> create database test01; 
#切换至新库
mysql> use test01;
#创建一个带有主键约束的表
mysql> create table t1(
    -> id int(10),
    -> name varchar(10) primary key,
    -> sex varchar(5),
    -> info varchar(200)
    -> );

Control statements of MySQL data tables

Determine whether the creation of the primary key (the column has a PRI words):

Control statements of MySQL data tables
The method described above, is defined in the definition of the primary key column at the same time, let's write about the specified primary key after you have defined all columns:

mysql> create table t2(
    -> id int(10),
    -> name varchar(10),
    -> sex varchar(5),
    -> primary key(id)
    -> );

Control statements of MySQL data tables

To confirm whether there is a primary key:

Control statements of MySQL data tables

2, non-empty constraint (no column is blank)

mysql> create table t3(
    -> id int(6) not null,
    -> name varchar(10)
    -> );

View table information to confirm:

Control statements of MySQL data tables

3, set up a unique value (do not allow duplicate data, may be empty, but only one empty, otherwise it will be treated as a duplicate)

mysql> create table t7(
    -> id int not null unique,
    -> name varchar(20)
    -> );

See its table structure:

Control statements of MySQL data tables

You can see it is to identify the primary key, but has not specified when it was created is the primary key, but the property of the column to meet the basic requirements of a primary key, such as unique, can not be empty.

4, set the default value for the column (if the column is empty, writing the default)

mysql> create table t4(
    -> id int(2) not null,
    -> name varchar(20),
    -> project varchar(20) default 'mysql'
    -> );

Look for confirmation:

Control statements of MySQL data tables

5, since the set value (id column generally used, must be set to an auto-increment primary key)

NOTE: mysql allows only the initial value, provided from the allowed value, that is, can be set to a value of 5, and then successively incremented, such as: 5,6,7 ..... but not provided its an increment number 2, for example: 5,7,9 ......

mysql> create table t5(
    -> id int not null primary key auto_increment,
    -> name varchar(20)
    -> );

View table structure to confirm:

Control statements of MySQL data tables

The effect of increasing the self-test:

mysql> insert into t5(name) values('zhangsan'),('lisi');
mysql> select * from t5;
+----+----------+
| id | name     |
+----+----------+
|  1 | zhangsan |
|  2 | lisi     |
+----+----------+

As can be seen from the above test, only two values ​​of the name is inserted and not inserted into the id value, but when the view table data, id already has a value, indicating self-energizing effect.

Set start value increment

#定义初始值为5
mysql> create table t6(                                  
    -> id int primary key auto_increment,
    -> name varchar(20)
    -> ) auto_increment=5;
#插入数据进行测试
mysql> insert into t6(name) values('zhangsan'),('lisi'); 

Verify its own value:
Control statements of MySQL data tables

Second, using the ALTER command

1, the length of the modified data value column

mysql> desc t1;            <!--查看t1表的结构-->
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(10)      | YES  |     | NULL    |       |
| name  | varchar(10)  | NO   | PRI | NULL    |       |
| sex   | varchar(5)   | YES  |     | NULL    |       |
| info  | varchar(200) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table t1 modify name varchar(20);      <!--修改其name字段的长度为20-->

Confirmation table modified structure:

Control statements of MySQL data tables

2, changes to field names (column names in the modification, it is also possible to modify the type of new data field names and data length)

mysql> desc t1;         <!--查看其info列-->
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id    | int(10)      | YES  |     | NULL    |       |
| name  | varchar(20)  | NO   | PRI | NULL    |       |
| sex   | varchar(5)   | YES  |     | NULL    |       |
| info  | varchar(200) | YES  |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+

mysql> alter table t1 change info infofo char(20);    <!--修改其info列的名字及数据类型-->

Verification modified result:

Control statements of MySQL data tables

3, insert a new field to the table

1) Insert a new column in the last one:

mysql> desc t3;             <!--确认t3列当前的字段-->
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(6)      | NO   |     | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> alter table t3 add tel int(13);         <!--插入一个tel列-->

After viewing Insert Column:

Control statements of MySQL data tables
2) Insert a new column at the beginning of the table:

mysql> alter table t3 add sex char(1) first;

3) insert a new column after the column specified:

mysql> alter table t3 add loc varchar(255) after name;

4, a field is added when adding constraints

mysql> alter table t3 add hobyy varchar(255) default 'work';

5, adding a foreign key

Before this operation, there is a need to find their own primary key table (t1 me name column is the primary key of the table).

Now the structure of the table t1 is as follows:
Control statements of MySQL data tables
the structure of the table t3 as follows:
Control statements of MySQL data tables

Now add the table name column is a foreign key t3 (where constraints t3_t1_name custom name) column name table t1:

mysql> alter table t3 add constraint t3_t1_name foreign key(name) references t1(name);

View changes t3 table:

Control statements of MySQL data tables

6, delete the foreign key

The foreign key is added above delete, t3_t1_name is the name of the foreign key.

mysql> alter table t3 drop foreign key t3_t1_name;
mysql> alter table t3 drop  key t3_t1_name;

7, delete columns

mysql> alter table t3 drop tel;                <!--删除t3表的tel列-->

Note: If you want to delete affiliated columns and other columns in the table, you need to delete a relationship, then delete the column. Otherwise, when later created a column of the same name, it will automatically establish a relationship.

8, to modify the order of columns

<!--将name字段移动到表的第一列-->
mysql> alter table t3 modify name varchar(10) first;

9, delete the table

mysql> drop table t5;            <!--直接删除-->
Query OK, 0 rows affected (0.00 sec)

mysql> drop table t5;            <!--再次删除,由于已经删除了,所以表不存在,会报错-->
ERROR 1051 (42S02): Unknown table 'test01.t5'
mysql> drop table if exists t5;       <!--进行判断后删除,if exists表示如果存在就删除-->
Query OK, 0 rows affected, 1 warning (0.00 sec)
<!--可以看到上述返回的信息有1个warning事项,可以执行以下命令进行查看-->
mysql> show warnings;    <!--记录的信息时不知道test01库中的t5表-->
+-------+------+---------------------------+
| Level | Code | Message                   |
+-------+------+---------------------------+
| Note  | 1051 | Unknown table 'test01.t5' |
+-------+------+---------------------------+
1 row in set (0.00 sec)

Similarly, when the delete operation table, if there is relationship, you need to delete the relationship, and then drop the table.

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14154700/2455908