Fourth, modify table statement

 1, create a file named student of the table: CREATE TABLE student_tb

mysql> USE itcast;
Database changed

mysql> CREATE TABLE student_tb
-> (
-> id INT(20),
-> name CHAR(20),
-> age INT(2),
-> sex CHAR(4)
-> );
Query OK, 0 rows affected

2, displays all the information to create the table: SHOW TABLES

mysql> SHOW TABLES;
+------------------+
| Tables_in_itcast |
+------------------+
| student_tb |
| tb_grade |
+------------------+
2 rows in set

3, display student created table: SHOW CREATE TABLE student_tb

mysql> SHOW CREATE TABLE student_tb;
+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student_tb | CREATE TABLE `student_tb` (
`id` int(20) DEFAULT NULL,
`name` char(20) DEFAULT NULL,
`age` int(2) DEFAULT NULL,
`sex` char(4) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

4, details of viewing student table structure built: DESC student_tb

mysql> DESC student_tb;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| name | char(20) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
| sex | char(4) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
4 rows in set

5, delete student_tb_1923 field student table: ALTER TABLE student_tb RENAME TO student_tb_1923

mysql> ALTER TABLE student_tb RENAME TO student_tb_1923;
Query OK, 0 rows affected

mysql> SHOW TABLES;
+------------------+
| Tables_in_itcast |
+------------------+
| student_tb_1923 |
| tb_grade |
+------------------+
2 rows in set

6, delete student_id INT student table (20): ALTER TABLE student_tb_1923 CHANGE id student_id 

INT(20)

mysql> ALTER TABLE student_tb_1923 CHANGE id student_id
INT(20);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

7, see the student table: DESC student_tb_1923

mysql> DESC student_tb_1923;
+------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+-------+
| student_id | int(20) | YES | | NULL | |
| name | char(20) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
| sex | char(4) | YES | | NULL | |
+------------+----------+------+-----+---------+-------+
4 rows in set

8, Student modify the table, after the name plus tel: ALTER TABLE student_tb_1923 ADD tel INT (11) AFTER name

mysql> ALTER TABLE student_tb_1923 ADD tel INT (11) AFTER name;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

9. After modifying the table look table: DESC student_tb_1923

mysql> DESC student_tb_1923;
+------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+-------+
| student_id | int(20) | YES | | NULL | |
| name | char(20) | YES | | NULL | |
| tel | int(11) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
| sex | char(4) | YES | | NULL | |
+------------+----------+------+-----+---------+-------+
5 rows in set

10, tel deleted using the delete statement: ALTER TABLE student_tb_1923 DROP tel

mysql> ALTER TABLE student_tb_1923 DROP tel;
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

11, the above statement appears to indicate deleted successfully, after deleting can look at the table: DESC student_tb_1923

mysql> DESC student_tb_1923;
+------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+-------+
| student_id | int(20) | YES | | NULL | |
| name | char(20) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
| sex | char(4) | YES | | NULL | |
+------------+----------+------+-----+---------+-------+
4 rows in set

These are created using Mysql table, modify the table name, modify the fields, delete fields, see the table, add fields and other statements about it as listed above!

Guess you like

Origin www.cnblogs.com/shh1431533328/p/11737987.html