Article directory
1. Create the database
The data table belongs to the database. Before creating the database, you should use the use <数据库名称>
specified database in which the operation is performed.
1.1 Use create
statement to create data table
Examples are as follows
First create and select the database
create database company;
use company;
create table emp
(
id int,
name varchar(25),
sex tinyint,
salary float
);
2. View the data table structure
2.1 View the basic structure of the table
Use the describe/desc statement to view table field information.
for example
Enter the following code
desc company;
2.2 View the detailed structure of the table
show create table emp;
3. Modify the data table
After the data table is created, you can also modify the data table according to actual needs
3.1 Modify the data table name
The table name is uniquely determined in a database, and the database system distinguishes different tables by the table name
Data Sheet Update Code Format
alter table <旧表名> rename to <新表名>
E.g
Modify the emp table in the company database to emps
Enter the sql statement
show tables;
alter table emp rename to emps;
show tables;
3.2 Modify the field data type
Modifying the data type of a field is to convert the data type of the field to another data type. The syntax format for modifying the field data type in MySQL is as follows:
ALTER TABLE<表名>MODIFY<字段名><数据类型>;
The main parameters are introduced as follows.
Table name: Refers to the name of the table where the field whose data type is to be modified is located.
Field name: refers to the field that needs to be modified.
Data Type: Refers to the new data type of the modified field.
Example
View data table name type before operation
desc emps;
[External link image transfer failed, the source site may have anti-leech mechanism, it is recommended to save the image and upload it directly (img-PYGyzSsb-1644943645344) (C:/Users/19867/AppData/Roaming/Typora/typora-user-images/ image-20220215234709213.png)]
modify name
alter table emps modify name varchar(20);
3.3 Modify the field name of the data table
After the field name in the data table is determined, it is not static, and the field name can be modified as needed. The syntax for modifying table field names in MySQL is as follows:
ALTER TABLE<表名>CHANGE<旧字段名><新字段名><新数据类型>;
The main parameters are introduced as follows.
Table name: The data table where the field name to be modified is located. Old field name: refers to the field name before modification.
New field name: refers to the modified field name.
New data type: refers to the modified data type. If you do not need to modify the data type of the field, you can set the new data type to be the same as the original, but the data type cannot be empty.
Example
emps
Change the name in the data table tonewname
alter table emps change newname varchar(28);
Check out the datasheet again
desc emps;
3.4 Adding fields to the data table
After the data table is created, if the field information does not meet the requirements, you can add new fields to the data table as needed. The format is as follows:
ALTER TABLE<表名>ADD<新字段名><数据类型>[约束条件][FIRST /AFTER已经存在的字段名];
The main parameters are described as follows:
Table name: The name of the data table to which the new field is to be added.
New field name: The field name that needs to be added.
Constraints: Set the full constraints for the new field.
FIRST: An optional parameter whose role is to set the newly added field as the first field of the table.
AFTER: An optional parameter whose role is to add the newly added field to the specified "existing field name".
alter table emps add city varchar(20);
alter table emps add age int after sex;
3.5 Modify the sorting method of fields
For the data table that has been created, the user can modify the arrangement order of the fields according to actual needs."
In MySQL, the relative position of the fields in the table can be changed by ALTER TABLE. The syntax format is as follows:
ALTER TABLE<表名>MODIFY<字段1><数据类型>FIRST|AFTER<字段2>;
The main parameters are described as follows:
Field 1: refers to the field of the position to be modified.
Data Type: Refers to the data type of "Field 1".
FIRST: is an optional parameter, which means to modify "field 1" as the first field of the table.
AFTER Field 2: Refers to inserting "Field 1" after "Field 2".
for example
Modify the sex field of the data table emps to the first field
alter table emps modify sex int first;
4. Delete the data table
4.1 Delete unrelated tables
In MySQL, use DROP TABLE to delete one or more tables at a time that are not associated with other tables. The syntax is as follows:
DROP TABLE <数据表名称>;
for example
DROP TABLE emps;
4.2 Delete the main table associated with other tables
When there is a foreign key association between the data tables, if the parent table is directly deleted by Jiaguo, the result will show failure. The reason is that direct deletion will destroy the referential integrity of the table. If the addition must be deleted, you can delete the child table associated with it, and then delete the parent table, but this will delete the data in both tables at the same time. To delete the parent table separately, Gary just needs to cancel the foreign key constraint of the associated table, and then delete the parent table.
for example
Create two associated tables in the database
CREATE TABLE tb_1(
id
INT PRIMARY KEY,
name
VARCHAR (22)
);
CREATE TABLE tb_2(
id
INT
PRIMARY KEY,
name
VARCHAR(25),
age
INT,
CONSTRAINT fk tb dt FOREIGN KEY (id)REFERENCES tb_1(id));
View foreign key constraints for tb_2
show create table tb_2;
The execution result is shown in the figure. As can be seen from the result, a foreign key constraint named fk_tb_dt is created on the data table tb_2.
Now delete the parent table tb_1 directly, and enter the delete statement as follows:
DROP TABLE tb_1;
The execution result is shown in Figure 4-31. It can be seen that, as mentioned above, when there is a foreign key constraint, the parent table cannot be directly deleted. Next, release the foreign key constraint of the associated sub-table tb_2, the SQL statement is as follows:
ALTER TABLE tb 2 DROP FOREIGN KEY fk_tb_dt;
delete tb_1 again
DROP TABLE tb_1;