mySQL modify data table

table of Contents

mySQl modify data table

1 Modify the table name

2 modified data type field

3 Modify the field name

4 Add field

4.1 add no integrity constraint condition of the field

4.2 Add fields of an integrity constraint conditions

4.3 Adding a field of the first column of the table

Add a 4.4 field after a specified column of the table

5 Delete field

6 modified arrangement position of the field

6.1 modify the field for the first field of the table

6.2 to modify the specified field following list of

7 changes to the table storage engine

Table 8 delete foreign key constraint


mySQl modify data table

It refers to a modified table structure in the database to modify the existing data table. MySQL use the ALTER TABLE statement to modify a table. Operations are commonly used to modify the table: modify the table name, modify the field names or field types of data, adding and deleting fields, the arrangement position of the field to modify, change table storage engine, remove the foreign key table constraints.

1 Modify the table name

MySQL is achieved by modifying the table name of the ALTER TABLE statement, the specific syntax rules are as follows:

ALTER TABLE <旧表名> RENAME [TO] <新表名>;

Where TO is optional and not used or not affect the results.

[Examples]: the data table tb_dept1 renamed tb_deptment1;

mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_dept1          |
| tb_emp1           |
| tb_emp5           |
+-------------------+
3 rows in set (0.00 sec)
mysql> alter table tb_dept1 rename tb_deptment1;
Query OK, 0 rows affected (0.22 sec)
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_deptment1      |
| tb_emp1           |
| tb_emp5           |
+-------------------+
3 rows in set (0.00 sec)

2 modified data type field

Modify the field data type, the data type field is converted into another data type.

Modification syntax rule field type data in MySQL follows:

ALTER TABLE <表名> MODIFY <字段名> <数据类型>

Where the name of the table "table name" simply more to modify the data type of the field, "field name" refers to field needs to be modified, "data type" refers to the new type of modified data field.

[Examples] The data type in the data table tb_deptl name field modified by the VARCHAR VARCHAR (22) (30).

Before performing the operation to modify the table name, view the DESC tb_dept table structure, the following results:

mysql> desc tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(22) | NO   |     | NULL    |       |
| location | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.06 sec)
mysql> alter table tb_dept1 modify name varchar(30);
Query OK, 0 rows affected (0.36 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(30) | YES  |     | NULL    |       |
| location | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.89 sec)

3 Modify the field name

Modify MySQL table field name syntax rules are as follows:

ALTER TABLE <表名> CHANGE <旧字段名> <新字段名> <新数据类型>;

Wherein, "old field name" field name refers to before modification; "new field name" refers to the modified field name; "new data type" refers to the data type of the modified data need not be modified if the type field may be a new data type can be provided with the same as the original, but the data type can not be empty.

[Examples] The location data field name in the table to tb_dept1 loc, data type remains unchanged, SQL statements, as follows:

ALTER TABLE tb_deptl CHANGE location loc VARCHAR(50);
mysql> desc tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(30) | YES  |     | NULL    |       |
| location | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.04 sec)
mysql> alter table tb_dept1 change location loc varchar(50);
Query OK, 0 rows affected (0.38 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc tb_dept1;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(30) | YES  |     | NULL    |       |
| loc   | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.02 sec)

4 Add field

As business needs change, you may need to add a new field in the table already existing. A full field including field names, data types, integrity constraints. Add field syntax is as follows:

ALTER TABLE<表名> ADD <新字段名> <数据类型> [约束条件] [FIRST|AFTER 已存在字段名];

We need to add a new field called the name of the field;

"FIRST" is optional, its role is newly added field set to the first field in the table;

"AFTER" is optional, and its role is to add new fields added to the back of "existing field name" specified.

4.1 add no integrity constraint condition of the field

[Examples] INT type field managerId added without integrity constraints (manager number) in the data table tb_deptl,

SQL statement is as follows:

mysql> alter table tb_dept1 add managerId int(10);
Query OK, 0 rows affected (0.35 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id        | int(11)     | NO   | PRI | NULL    |       |
| name      | varchar(30) | YES  |     | NULL    |       |
| loc       | varchar(50) | YES  |     | NULL    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
4 rows in set (0.50 sec)

4.2 Add fields of an integrity constraint conditions

[Examples] add a not-null VARCHAR (12) in the data type field columnI tb_deptl table, SQL statements as follows:

mysql> alter table tb_dept1 add column1 varchar(12) not null;
Query OK, 0 rows affected (0.25 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id        | int(11)     | NO   | PRI | NULL    |       |
| name      | varchar(30) | YES  |     | NULL    |       |
| loc       | varchar(50) | YES  |     | NULL    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
| column1   | varchar(12) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.03 sec)

4.3 Adding a field of the first column of the table

Adding an int type field in the data table tb_dept1 column2, SQL statement is as follows:

mysql> alter table tb_dept1 add column2 int(11) first;
Query OK, 0 rows affected (1.44 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| column2   | int(11)     | YES  |     | NULL    |       |
| id        | int(11)     | NO   | PRI | NULL    |       |
| name      | varchar(30) | YES  |     | NULL    |       |
| loc       | varchar(50) | YES  |     | NULL    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
| column1   | varchar(12) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.64 sec)

Add a 4.4 field after a specified column of the table

[Examples] After adding a name column tb_dept1 column3 int type field in the data table, SQL statement is as follows:

mysql> alter table tb_dept1 add column3 int(11) after name;
Query OK, 0 rows affected (0.60 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| column2   | int(11)     | YES  |     | NULL    |       |
| id        | int(11)     | NO   | PRI | NULL    |       |
| name      | varchar(30) | YES  |     | NULL    |       |
| column3   | int(11)     | YES  |     | NULL    |       |
| loc       | varchar(50) | YES  |     | NULL    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
| column1   | varchar(12) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
7 rows in set (0.02 sec)

5 Delete field

Delete the field is a field in the data table is removed from the table, the syntax is as follows:

ALER TABLE <表名> DROP <字段名>

"Field Name" refers to the need column2 field from the table tb_dept1 table

[Example 5] Delete field data table column2 table tb_dept1

mysql> alter table tb_dept1 drop column2;
Query OK, 0 rows affected (0.66 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id        | int(11)     | NO   | PRI | NULL    |       |
| name      | varchar(30) | YES  |     | NULL    |       |
| column3   | int(11)     | YES  |     | NULL    |       |
| loc       | varchar(50) | YES  |     | NULL    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
| column1   | varchar(12) | NO   |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.32 sec)

6 modified arrangement position of the field

For a data table, in a time of creation, the fields in the order table have been determined.

But the structure is not completely the table can not be changed, the relative position of the table can be changed in the field through the ALTER TABLE. Syntax is as follows:

ALTER TABLE <表名> MODIFY <字段1> <数据类型> FIRST|AFTER <字段2>;

"Field 1" field simply more modified sites, "data type" means "Field 1" data type,

"FIRST" is optional, it refers to "Field 1" was changed to the first field in the table,

"Field an AFTER 2" refers to the "Field 1" into "Field 2" hereinafter.

6.1 modify the field for the first field of the table

[Examples] The field data table column1 tb_dept1 modifications in the first field of the table, SQL statements, as follows:

mysql> alter table tb_dept1 modify column1 varchar(12) first;
Query OK, 0 rows affected (1.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| column1   | varchar(12) | YES  |     | NULL    |       |
| id        | int(11)     | NO   | PRI | NULL    |       |
| name      | varchar(30) | YES  |     | NULL    |       |
| column3   | int(11)     | YES  |     | NULL    |       |
| loc       | varchar(50) | YES  |     | NULL    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.18 sec)

6.2 to modify the specified field following list of

[Examples] The field data table tb_dept1 column1 inserted behind the location field, SQL statement is as follows:

mysql> alter table tb_dept1 modify column1 varchar(12) after location;
Query OK, 0 rows affected (1.96 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept1;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id        | int(11)     | NO   | PRI | NULL    |       |
| name      | varchar(30) | YES  |     | NULL    |       |
| column3   | int(11)     | YES  |     | NULL    |       |
| location  | varchar(50) | YES  |     | NULL    |       |
| column1   | varchar(12) | YES  |     | NULL    |       |
| managerId | int(10)     | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.02 sec)

7 changes to the table storage engine

MySQL storage engine support

Engine name Support
FEDERATED no
MRG MYISAM Yes
MYISAM Yes
BLACKHOLE Yes
CSV Yes
MEMORY Yes
ARCHIVE Yes
InnoDB Yes
PERFORMANCE SCHEMA default

[Examples] The table data storage engine tb_emp1 modify MyISAM;

mysql> alter table tb_emp1 engine=MyISAM;
Query OK, 0 rows affected (0.18 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table tb_emp1\G;
*************************** 1. row ***************************
       Table: tb_emp1
Create Table: CREATE TABLE `tb_emp1` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(25) DEFAULT NULL,
  `deptId` int(11) DEFAULT NULL,
  `salary` float DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.01 sec)

ERROR:
No query specified

Table 8 delete foreign key constraint

For foreign keys defined in the database, you no longer need, you can delete it. Once you remove the foreign key, the primary table will be lifted and relationships between tables from,

MySQL to delete the foreign key syntax is as follows:

ALTER TABLE <表名> DROP FOREIGN KEY <外键约束名>

"Foreign key constraint name" refers to the following parameter definition table when the keyword CONSTRAINT

Released five original articles · won praise 4 · Views 252

Guess you like

Origin blog.csdn.net/sc_942344134/article/details/103977238