1. Introduction to the experiment
1.1 Experiment content
In this experiment, we will learn and practice how to modify, delete, and rename database tables and their contents.
1.2 Experimental knowledge points
- database operations
- Data table manipulation
- Update and delete data
1.3 Experimental environment
The experimental environment used in the course is Ubuntu Linux 14.04 64-bit version. The program will be used in the experiment:
- Mysql 5.5.50
- Xfce Terminal
2. Development preparation
Note: If you entered this section directly from the previous section to study, please delete the database created in the previous section first
mysql_shiyan
, and delete the statement asDROP DATABASE mysql_shiyan;
.
Before officially starting the experimental content, you need to download the relevant code.
This code can create two new databases named test_01
and mysql_shiyan
, and mysql_shiyan
create four tables (department, employee, project, table_1) in the database, and then insert data into them.
The specific operation is as follows, first enter the command to enter the /home/shiyanlou/Desktop
directory:
cd /home/shiyanlou/Desktop
Then enter the command to download the code:
git clone https://github.com/shiyanlou/SQL5
After the download is complete, enter the command to start the MySQL service and log in with the root user:
#Open MySQL service sudo service mysql start #Log in as root user mysql -u root
There is 1 file in the downloaded SQL5 directory MySQL-05.sql
(the SQL5 directory is on the desktop, you can use gedit to view and edit the files in it.)
To load the data in the file, you need to enter the command in the MySQL console to complete the experimental preparation:
source /home/shiyanlou/Desktop/SQL5/MySQL-05.sql
3. Experimental steps
3.1 Modifications to the database
Use the command SHOW DATABASES;
to see MySQL-05.sql
the two databases generated by running the file just now:
test_01
Now we run the command to delete the database named :
DROP DATABASE test_01;
Now use the command again SHOW DATABASES;
to find that the test_01
database has been successfully deleted:
3.2 Modifications to a table
3.2.1 Renaming a table
RENAME TABLE original name TO new name; ALTER TABLE original name RENAME new name; ALTER TABLE original name RENAME TO new name;
The statement to rename a table has many forms. The following three forms have the same effect:
table_1
The name tried to modify using the command is table_2
:
3.2.2 Delete a table
The statement to delete a table is similar to the statement to delete the database just used. The format is as follows:
DROP TABLE table name;
For example, we table_2
delete the table:
3.3 Modification of a column (that is, modification of the table structure)
Modifying the table structure is the difficulty of the experiment in this section. Sometimes some small mistakes will cause irreversible consequences, so please operate carefully. Also note that it is not necessary to avoid changing the table structure as much as possible.
3.3.1 Add a column
The format of the statement to add a column to the table is:
ALTER TABLE table name ADD COLUMN column name data type constraints; or: ALTER TABLE table name ADD column name data type constraint;
Now that the employee table has id、name、age、salary、phone、in_dpt
these 6 columns, we try to join height
(height) a column and specify a DEFAULT constraint:
It can be found that the newly added column is placed at the far right of this table by default. If you want to insert the added column at the specified position, you need to use the AFTER keyword at the end of the statement ("AFTER column 1" means that the new column is placed after "Column 1").
For example, we add a new column weight
(weight) and place it after age
(age):
The above effect is to add the new column after a certain position. If you want to put it in the position of the first column, use FIRST
keywords, such as the statement:
ALTER TABLE employee ADD test INT(10) DEFAULT 11 FIRST;
The effect is as follows:
3.3.2 Delete a column
The format of deleting a column in a table is very similar to the statement format of adding a column just used, except that the keyword is ADD
changed DROP
, and there is no need for data type, constraint or location information after the statement. Specific sentence format:
ALTER TABLE table name DROP COLUMN column name; or: ALTER TABLE table_name DROP column_name;
test
Let's delete what we just added :
3.3.3 Renaming a column
This statement can actually not only be used to rename a column, to be precise, it is to modify a column (CHANGE):
ALTER TABLE table name CHANGE original column name new column name data type constraint;
Note: The "data type" after this renaming statement cannot be omitted, otherwise the renaming will fail.
When the original column name is the same as the new column name, specifying a new data type or constraint can be used to modify the data type or constraint. It should be noted that modifying the data type may cause data loss, so use it with caution.
We use this statement to rename the "height" column to Hanyu Pinyin "shengao", the effect is as follows:
3.3.4 Changing the data type
To modify the data type of a column, in addition to using the CHANGE statement just now, you can also use this MODIFY statement:
ALTER TABLE table name MODIFY column name new data type;
Again, care must be taken when modifying data types, as this may result in data loss. Please think carefully before attempting to modify the data type.
3.4 Modify the content of the table
3.4.1 Modify a value in the table
Most of the time, what we need to modify is not the entire database or the entire table, but one or several data in the table, which requires us to use the following command to achieve precise modification:
UPDATE table name SET column 1=value 1, column 2=value 2 WHERE condition;
For example, we want to change Tom's age to 21 and salary to 3000:
Note: There must be a WHERE condition, otherwise there will be consequences you don't want to see
3.4.2 Delete a row of records
To delete a row of data in the table, the WHERE condition must also be added, otherwise the entire column of data will be deleted. Delete statement:
DELETE FROM table name WHERE condition;
Let's try to delete Tom's data: