Basic operation of MySQL database : Basic operation of MySQL database
Create data table
After the database is successfully created, you can create a data table in the created database. Before creating the table, use it to USE 数据库名称
switch to the database that needs to be operated, and you can also SELECT database();
query the current database. When everything is ready, you can create a data table. The syntax for creating a data table is as follows:
CREATE TABLE 表名(
字段名1 数据类型,
字段名2 数据类型,
....
字段名n 数据类型
);
Create a stu table in the text database according to the above format, and three fields will be created in the stu table.
Field Name | type of data |
---|---|
stu_id | INT(10) |
stu_name | VARCHAR(50) |
stu_age | INT(10) |
They are the student’s number, name and age.
Use SHOW TABLES;
can query whether the data table is created successfully.
It can be seen from the execution result that there is a data table called stu under the database text.
View data sheet
After the database is created, you can SHOW CREATE TABLE
view the data table through the statement, the syntax format is as follows:
SHOW CREATE TABLE 表名;
Use this statement to query the previously created stu table.
The execution result shows that the three fields created when the data table stu was created at the time, including their database type and size, and the character encoding information of the table can also be viewed, but the displayed format is very confusing. If there are too many fields, it looks very difficult. . You can add a "\G" after the statement for formatting.
They are a bit neater, and there is another query result is a use DESCRIBE
statement, the syntax format is as follows:
DESCRIBE 表名;
You can also use his shorthand and the DESC
result is the same.
Modify the database
After the data table is created, the table name, table fields, data type, etc. may be modified.
1. Modify the table name
The syntax format for modifying the table name is as follows:
ALTER TABLE 原表名 RENAME [TO] 新表名;
The "to" can be written or not, and generally not written. Modify the table name of the stu table to student. Query the current table name first:
execute the modification statement, and the modified table name.
2. Modify the field
The syntax format of the modified field is as follows:
ALTER TABLE 表名 CHANGE 原字段名 新字段名 新数据类型;
Modify the stu_age field in the student table to stu_sex and the data type to VARCHAR(10).
stu_age is gone, replaced by stu_sex.
3. Modify the data type of the field
The grammatical format for modifying the field data type in the table is as follows:
ALTER TABLE 表名 MODIFY 字段名 数据类型;
Change the data type of the stu_sex field in the student table to CHAR.
The data type of stu_sex becomes char.
4. Add fields
As the requirements expand, fields may need to be added to the table. The syntax format for adding fields is as follows:
ALTER TABLE 表名 ADD 新字段名 数据类型;
Add the stu_hobby field to the student table, the data type is VARCHAR(50).
5. Delete the field
If there is a requirement for addition, there will be a requirement for deletion. The syntax format of the deleted field is as follows:
ALTER TABLE 表名 DROP 字段名;
Delete the stu_hobby that was just added from the table.
stu_hobby was successfully deleted.
6, modify the arrangement position of the field
When the table is created, the order of the fields has been determined. If you need to modify the position of the fields in the table, you can ALTER TABLE
complete it with a statement. The syntax format is as follows:
ALTER TABLE 表名 MODIFY 字段名1 数据类型 FIRST|AFTER 字段名2;
Field name 1 indicates the field whose position needs to be modified, FIRST is an optional parameter, which means that field 1 is changed to the first field of the table, and AFTER field name 2 means that field 1 is inserted after field 2.
Put stu_name in the student table after stu_sex.
The order in the original table is:
after modification:
Delete database
Deleting a data table is to delete a data table from the database and delete the data stored in the table at the same time. The syntax format of deleting a data table is as follows:
DROP TABLE 表名;
Delete the student table.
The database is empty and the student table is successfully deleted.