Database data table control statement

1. Statements related to constraints

1. Primary key constraint (primary key constraint requires that the data of the primary key column is unique, not allowed to be empty)

#创建库
mysql> create database test1;
#进入到库中
mysql> use test1;
#创建一个带有主键约束的表
mysql> create table tab1(
    -> id int(10),
    -> name varchar(10) primary key,
    -> sex varchar(5),
    -> info varchar(200)
    -> );
Query OK, 0 rows affected (0.01 sec)

Determine whether the created primary key (whether the column has the word PRI): The
Database data table control statement
above is to define the primary key while defining the column. Let's write to specify the primary key after defining all the columns:

mysql> create table tab2(
    -> id int(10),
    -> name varchar(10),
    -> sex varchar(10),
    -> primary key(id)                       # 括号里定义主键的列
    -> );

Database data table control statement
2. Non-null constraint (the value of the column is not allowed to be empty)

mysql> create table tab3(
    -> id int(6) not null,                # not null :不允许为空
    -> name varchar(10)
    -> );

Database data table control statement
3. The uniqueness of the set value (duplicate data is not allowed, it can be empty, but there can only be one, otherwise it will be considered as a duplicate)

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

Database data table control statement
You can see that it is identified as the primary key, but it was not specified as the primary key when it was created, but the attributes of this column basically meet the requirements of the primary key, such as unique, can not be empty .
4. Set the default value of the column (if the column is empty, write the default value)

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

Database data table control statement
5. Set self-increment value (usually used for id column, self-increment column must be set as the primary key)
Note: mysql only allows to set the initial value, not to set self-increment value, that is to say, you can set the first value to 5, Then increase in turn, such as: 5, 6, 7, ..... but you can not set it to increment 2 times at a time, such as: 5, 7, 9 ...

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

Database data table control statement
Test its self-increasing effect:

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

As can be seen from the above test, only two values ​​of name are inserted, and no value of id is inserted, but when viewing the table data, id already has a value, indicating that self-increment takes effect and
sets the self-increment starting value

mysql> create table tab7(
    -> id int primary key auto_increment,
    -> name varchar(20)
    -> )auto_increment=6;
#插入数据验证
mysql> insert into tab7(name) values('zyz'),('lisi'),('zhangsan');

mysql> select * from tab7;
+----+----------+
| id | name     |
+----+----------+
|  6 | zyz      |
|  7 | lisi     |
|  8 | zhangsan |
+----+----------+

Second, the use of ALTER instruction

1. Modify the data length of the column value

mysql> desc tab1;
+-------+--------------+------+-----+---------+-------+
| 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    |       |
+-------+--------------+------+-----+---------+-------+
#修改name 字符长度为20
mysql> alter table tab1 modify name varchar(20);

Database data table control statement
2. Modify the field name (while modifying the field name, you can also modify the data type and data length of its new field name)

mysql> desc tab1;
+-------+--------------+------+-----+---------+-------+
| 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 tab1 change info info2 char(20);

Database data table control statement
3. Insert a new field into the table
1) Insert a new column in the last column:

mysql> desc tab3;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(6)      | NO   |     | NULL    |       |
| name  | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
#插入一个  zuihou  列
mysql> alter table tab3 add zuihou int(10);

Database data table control statement
2) Insert a new column at the beginning of the table:

mysql> alter table tab3 add sex char(10) first;

3) Insert a new column after the specified column:

mysql> alter table tab3 add local varchar(255) after name;

4. Add constraints when adding fields

mysql> alter table tab3 add hello varchar(20) default 'work';

5.
Before adding a foreign key to perform this operation, you need to find a table with a primary key (the name column of the tab1 table here is the primary key)
tab1 table structure:
Database data table control statement
tab3 table structure:
Database data table control statement
now add the name column of the t3 table to the t1 table Foreign key in the name column (where t3_t1_name is a custom constraint name):

mysql> alter table tab3 add constraint tab3_tab1_name foreign key(name) references tab1(name);

View the changes in the tab3 table:
Database data table control statement
6. Delete the foreign key
to delete the foreign key added above, tab3_tab1_name is the name of the foreign key.

mysql> alter table tab3 drop foreign key tab3_tab1_name;
mysql> alter table tab3 drop key tab3_tab1_name;

——————————

Speaking of foreign keys, here is to talk about what is a foreign key.
Querying related information is all about speaking some terms. The main function of foreign keys is to maintain the consistency and integrity of data. I heard that I was confused, indicating that I did not understand.
Here I also explain from the words of a big brother, you can see the small table below
+ ------- + ref + ------- +
| sub | ------> | main |
+ ------- + + ------- +

Reference (ref) the value of a column in the main table from a column in the table (sub).

For example, the student table has a student number (sid), and the student column (stu) in the score table refers to the student number of the student table. At this time, for the stu of the score table. The sid of the student table is the foreign key.

The secondary table is also called a foreign key table, the primary table is also called a primary key table, a foreign table, and the columns are also called fields.

So when designing. Add a foreign key to Table 1, this foreign key is the student number field in Table 2. Then Table 1 is the main table, and Table 2 is the child table.

Your master-slave relationship is upside down. In your diagram, Table 1 is indeed the main table. Table 2 is a child table, but it is not called adding a foreign key to Table 1, but adding a foreign key to Table 2. The student number field in Table 2 is called a foreign key, which is the primary key of the student number field in Table 1.

You can say this: the student number field of Table 1 is the foreign key of Table 2

———————— End of foreign key explanation ——————
7, delete column

mysql> alter table tab3 drop zuihou;

Note: If the column to be deleted is related to a column in another table, you need to delete the relationship before deleting the column. Otherwise, when a column with the same name is created later, it will automatically establish a relationship.
8. Modify the order of the columns

mysql> alter table tab3 modify name varchar(10) first;

9. Delete the table

#直接删除
mysql> drop table tab6;
Query OK, 0 rows affected (0.01 sec)
#再次删除这个表,可以看到已经提示说没有这个表了,说明已经删除
mysql> drop table tab6;
ERROR 1051 (42S02): Unknown table 'test1.tab6'
#进行判断后删除,if exists表示如果存在就删除
mysql> drop table if exists tab6;
Query OK, 0 rows affected, 1 warning (0.00 sec)
#可以看到上述返回的信息有1个warning事项,可以执行以下命令进行查看
mysql> show warnings;               # 返回的信息显示不知道test1库中的tab6表
+-------+------+----------------------------+
| Level | Code | Message                    |
+-------+------+----------------------------+
| Note  | 1051 | Unknown table 'test1.tab6' |
+-------+------+----------------------------+
1 row in set (0.00 sec)

Similarly, when performing a delete table operation, if there is an association relationship, you need to delete the association relationship first, and then delete the table.
Similarly, when performing a delete table operation, if there is an association relationship, you need to delete the association relationship, then delete the table
. When performing the delete table operation, if there is an association relationship, you need to delete the association relationship before deleting the table

Guess you like

Origin blog.51cto.com/14227204/2489543