1. Create a data table
1> call to the database command: USE itcast;
After entering the correct command results
Database changed ;
2> create a data table:. The CREATE TABLE student_tb
-> (
-> ID the INT (10),
-> name CHAR (20 is),
-> Age the INT (2),
-> Sex CHAR (. 5)
->);
command after entering the correct results
Query OK, 0 rows affected
2. Check the data table is successfully created: SHOW TABLES;
Results after entering the correct command
+ ------------------ +
| Tables_in_itcast |
+ ------------------ +
| student_tb |
+ ------------------ +
1 in the SET Row
3. View the data sheet information has been created: SHOW CREATE TABLE student_tb;
After entering the correct command results
+ ------------ + ------------------------------ -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- +
| the Table | the Create the Table |
+ ------------ + ----------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- - +
| student_tb | student_tb` the CREATE TABLE `(
` id` int (10) the DEFAULT NULL,
`char name` (20 is) the DEFAULT NULL,
` age` int (2) the DEFAULT NULL,
`sex` char(5) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set
See Table 4. The data information (specific information table): DESC student_tb;
After entering the correct command results
+ ---------- + ------- + ------ + ----- + --------- + - + ------
| Field, | Type | Null | Key | the Default | Extra |
+ ------- + ------ + --- + ---------- - + --------- + ------- +
| the above mentioned id | int (10) | YES | | NULL | |
| name | char (20) | YES | | NULL | |
| Age | int (2) | YES | | NULL | |
| Sex | char (5) | YES | | NULL | |
+ ------- + --- + ---------- + --------- + + ----- --- ------- +
4 rows in the SET
5. Modify the table name: ALTER TABLE student_tb RENAME TO student1923_tb;
After entering the correct command results
Query OK, 0 rows affected
6. modify data field names: ALTER TABLE student1923_tb CHANGE name username CHAR (20);
Results after entering the correct command
Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
7. Modify field data type: ALTER TABLE student1923_tb MODIFY username VARCHAR (20);
Results after entering the correct command
Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
View the data sheet information (specific information table) whether the amendment is successful: DESC student1923_tb;
After entering the correct command results
+ ------------- + ---------- + ------ + ----- + ----- + ------- + ----
| Field, | Type | Null | Key | the Default | Extra |
+ ---------- + ------------ - + ------ + ----- + --------- + ------- +
| the above mentioned id | int (10) | YES | | NULL | |
| username | VARCHAR (20 is) | YES | | NULL | |
| Age | int (2) | YES | | NULL | |
| Sex | char (. 5) | YES | | NULL | |
+ ---------- ------------- + ----- + ------ + --------- + ------- + +
4 rows in the SET
8. Add field:
1> added to the last: ALTER TABLE student1923_tb ADD address CHAR (20);
Results after entering the correct command
Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
View the data sheet information (specific information table) whether the amendment is successful: DESC student1923_tb;
After entering the correct command results
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
| sex | char(5) | YES | | NULL | |
| address | char(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set
2> were added to intermediate: ALTER TABLE student1923_tb ADD mail CHAR (30) AFTER sex;
Results after entering the correct command
Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
View the data sheet information (specific information table) whether the amendment is successful: DESC student1923_tb;
After entering the correct command results
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(10) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
| sex | char(5) | YES | | NULL | |
| mail | char(30) | YES | | NULL | |
| address | char(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
6 rows in set
3> is added to the frontmost: ALTER TABLE student1923_tb ADD grade CHAR (30) FIRST;
Results after entering the correct command
Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
View the data sheet information (specific information table) whether the amendment is successful: DESC student1923_tb;
After entering the correct command results
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| grade | char(30) | YES | | NULL | |
| id | int(10) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
| sex | char(5) | YES | | NULL | |
| mail | char(30) | YES | | NULL | |
| address | char(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
7 rows in set
9. Delete field: ALTER TABLE student1923_tb DROP mail;
Results after entering the correct command
Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
View the data sheet information (specific information table) whether the amendment is successful: DESC student1923_tb;
After entering the correct command results
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| grade | char(30) | YES | | NULL | |
| id | int(10) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
| sex | char(5) | YES | | NULL | |
| address | char(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
6 rows in set
10. The modified arrangement position field:
1> The first modification field: ALTER TABLE student1923_tb MODIFY username VARCHAR (20) FIRST;
Results after entering the correct command
Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
View the data sheet information (specific information table) whether the amendment is successful: DESC student1923_tb;
After entering the correct command results
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(20) | YES | | NULL | |
| grade | char(30) | YES | | NULL | |
| id | int(10) | YES | | NULL | |
| age | int(2) | YES | | NULL | |
| sex | char(5) | YES | | NULL | |
| address | char(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
6 rows in set
After 2> to modify the field ...: ALTER TABLE student1923_tb MODIFY username VARCHAR (20) AFTER grade;
Results after entering the correct command
Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
11. Delete Data table: DROP TABLE student1923_tb;
Results after entering the correct command
Query OK, 0 rows affected
12. Check whether to delete the data sheet success: SHOW TABLES;
After entering the correct command results
Empty set
13. The word key constraint:
1> single-field: the CREATE TABLE exam1
-> (
-> the INT ID (20 is),
-> name CHAR (20 is) a PRIMARY KEY
->);
Results after entering the correct command
Query OK, 0 rows affected
View the data sheet information (specific information table) whether the amendment is successful: DESC exam1;
After entering the correct command results
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| name | char(20) | NO | PRI | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set
2>.多字段:CREATE TABLE exam2
-> (
-> id INT(20),
-> name CHAR(20),
-> PRIMARY KEY(id,name)
-> );
Results after entering the correct command
Query OK, 0 rows affected
View the data sheet information (specific information table) whether the amendment is successful: DESC exam2;
After entering the correct command results
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(20) | NO | PRI | 0 | |
| name | char(20) | NO | PRI | | |
+-------+----------+------+-----+---------+-------+
2 rows in set
14. The non-null constraint: the CREATE TABLE exam3
-> (
-> the INT ID (20 is),
-> name CHAR (20 is) the NOT NULL
->);
Results after entering the correct command
Query OK, 0 rows affected
View the data sheet information (specific information table) whether the amendment is successful: DESC exam3;
After entering the correct command results
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| name | char(20) | NO | | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set
15. The only constraint: the CREATE TABLE exam4
-> (
-> the INT ID (20 is),
-> name CHAR (20 is) UNIQUE
->);
After entering the correct command results
Query OK, 0 rows affected
View the data sheet information (specific information table) whether the amendment is successful: DESC exam4;
After entering the correct command results
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| name | char(20) | YES | UNI | NULL | |
+-------+----------+------+-----+---------+-------+
2 rows in set