Constraints on the elementary operation of table data table

1, create a data table
MySQL> the CREATE TABLE tb_grade
-> (
-> the INT ID (20 is),
-> name CHAR (20 is),
-> Grade FLOAT
->)
->;
Query the OK, 0 rows affected

In order to verify the data table is successfully created, you need to use the SHOW TABLES statement to see
MySQL> SHOW TABLES;
+ ------------------ +
| Tables_in_itcast |
+ ------ + ------------
| tb_grade |
+ ------------------ +
1 in the SET Row

2, using SHOW CREATE TABLE table to view the data (see the data sheet of the method) MySQL> SHOW CREATE TABLE tb_grade;
+ ---------- + -------------- -------------------------------------------------- -------------------------------------------------- + --------------------------------------
| the Table | the Create the Table |
+ ---- ------ + ------------------------------------------- -------------------------------------------------- -------------------------------------------------- + ---------
| tb_grade | tb_grade` the CREATE TABLE `(
` id` int (. 11) the DEFAULT NULL,
`name` VARCHAR (20 is) the DEFAULT NULL,
` a float grade` the DEFAULT NULL
) ENGINE = MyISAM the DEFAULT CHARSET = gbk |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

Use DESCRIBE statement see the data sheet (Method II) (which may be abbreviated as DESC)
MySQL> DESCRIBE tb_grade;
+ ------------- + + ------- ------ ----- + --------- + ------- + +
| Field, | Type | Null | Key | the Default | Extra |
+ ------- + --- + ------ + ----- + ---------- --------- + ------- +
| the above mentioned id | int (11) | YES | | NULL | |
| name | VARCHAR (20) | YES | | NULL | |
| Grade | float | YES | | NULL | |
+ ------- + ---------- + ------ + ----- + --- --------- + ------- +
3 rows in the SET

NULL: indicate whether the column can store NULL values.
Key: Indicates whether the column has been indexed.
Default: indicates that the column has a default value.
Extra: indicates additional information relevant to a given column are acquired.

3, modify table (ALTER TABLE old table name RENAME [TO] a new table;)
MySQL> the ALTER TABLE tb_grade the RENAME the TO Grade;
Query the OK, 0 rows affected

To examine whether to amend the table name correctly, use the SHOW TABLES statement again to see all tables in the database
MySQL> SHOW TABLES;
+ ------------------ +
| Tables_in_itcast |
+ - + ----------------
| Grade |
+ ------------------ +
1 in the SET Row

4, changes to field names (ALTER TABLE table name CHANGE old field name new field name for the new data type;) MySQL> the ALTER TABLE Grade CHANGE name username VARCHAR (20 is);
Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

查看后:
mysql> DESC grade;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set


5, the data type field modification (ALTER TABLE table MODIFY Field Name Data Type;) MySQL> the ALTER TABLE MODIFY ID Grade the INT (20 is);
Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

6, add fields (ALTER TABLE table name ADD new Field Name Data Type [constraints] [FIRST | AFTER existing field name])
MySQL> the ALTER TABLE Grade ADD Age the INT (10);
Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

查看后:
mysql> DESC grade;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
| age | int(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set

7, delete the field (ALTER TABLE table DROP field name;)
MySQL> the ALTER TABLE DROP Age Grade; Query the OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

查看后:
mysql> DESC grade;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set

8, the arrangement position of the modified field (ALTER TABLE table MODIFY Field Name Data Type 1 FIRST | AFTER 2 field name)

(1)
mysql> ALTER TABLE grade MODIFY username VARCHAR(20) FIRST;Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
查看后:
mysql> DESC grade;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(20) | YES | | NULL | |
| id | int(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set

(2)
mysql> ALTER TABLE grade MODIFY id INT(20) AFTER grade;Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
查看后:
mysql> DESC grade;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
| id | int(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set

9, delete data table (DROP TABLE table name;)
MySQL> the DROP TABLE Grade;
Query the OK, 0 rows affected


10, a table constraint
constraints described
PRIMARY KEY constraints primary key uniquely identifies a record corresponding
FOREIGN KEY foreign key constraint
NOT NULL non-empty constraint
UNIQUE uniqueness constraint
DEFAULT default constraint values, the default value of a field


Refers to a multiple-field primary key is the primary key (PRIMARY KEY (field names 1, 2 field names, field names ...... n)) of the plurality of field combination
MySQL> the CREATE TABLE exam_1
->
-> (
-> stu_id the INT,
-> the INT COURSE_ID,
-> Grade FLOAT,
-> a PRIMARY KEY (stu_id, COURSE_ID)
->);
Query the OK, 0 rows affected

mysql> DESC exam_1;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | 0 | |
| course_id | int(11) | NO | PRI | 0 | |
| grade | float | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+
3 rows in set

Use AUTO_INCREMENT setting table field value is automatically increased (Field Name Data Type AUTO_INCREMENT;)
MySQL> the CREATE TABLE exam_2
-> (
-> ID the INT a PRIMARY KEY AUTO_INCREMENT,
-> stu_id the INT UNIQUE,
-> Grade FLOAT the DEFAULT 0
->);
Query the OK , 0 rows affected


mysql> DESC exam_2;
+--------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(11) | YES | UNI | NULL | |
| grade | float | YES | | 0 | |
+--------+---------+------+-----+---------+----------------+
3 rows in set

mysql> CREATE TABLE exam_3
-> (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> stu_id INT UNIQUE,
-> grade FLOAT
-> );
Query OK, 0 rows affected

mysql> DESC exam_3;
+--------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(11) | YES | UNI | NULL | |
| grade | float | YES | | NULL | |
+--------+---------+------+-----+---------+----------------+
3 rows in set

Guess you like

Origin www.cnblogs.com/p1035/p/11741243.html