Add primary Key
Mysql> ALTER TABLE info add primary key (ID);
Query OK, 0 rows affected (0.07 sec)
records:0 duplicates:0 warnings:0
Mysql> desc Info; #查看主键
+--------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+-------+
| ID | Int (11) | NO | PRI | NULL | | #显示出主键
| name | char (6) | YES | | NULL | |
| Score | Decimal (5,2) | YES | | NULL | |
| Age | Int (4) | YES | | NULL | |
| hobbly | Int (4) | YES | | NULL | |
+--------+--------------+------+-----+---------+-------+
5 rows in Set (0.00 sec)
Add foreign key
ALTER TABLE info add constraint fk_id foreign key (hobbly) REFERENCES hy (ID);
Mysql> Show index from INFO\G; #查看外键
1. Row
Table:info
non_unique:0
Key_name:primary
Seq_in_index:1
Column_name:id
Collation:a
Cardinality:3
Sub_part:null
Packed:null
Null:
Index_type:btree
Comment:
Index_comment:
2. Row
Table:info
Non_unique:1
key_name:fk_id #外键添加成功
Seq_in_index:1
column_name:hobbly
Collation:a
Cardinality:3
Sub_part:null
Packed:null
Null:yes
Index_type:btree
Comment:
Index_comment:
2 rows in Set (0.00 sec)
Table changes fields at the same time (different table column contents)
Update info set age=23,num=44 where id=2;
Mysql> SELECT * from info;
+----+--------+-------+-----------+------+------+
| ID | name | Score | hobbly | Age | num |
+----+--------+-------+-----------+------+------+
| 1 | ZL | 88 | Reading | 33 | NULL |
| 2 | John Doe | 68 | Internet access | 23 | 44 | #成功修改
| 3 | Wangsi | 68 | Watch TV | NULL | NULL |
+----+--------+-------+-----------+------+------+
3 Rows in Set (0.00 sec)
Add multiple fields at the same time
Mysql> ALTER TABLE info add column age int,add column num int;
Query OK, 0 rows affected (0.05 sec)
records:0 duplicates:0 warnings:0
Mysql> desc Info; #查看表结构
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| ID | Int (11) | NO | PRI | NULL | |
| name | CHAR (4) | YES | | NULL | |
| Score | char (6) | YES | | NULL | |
| hobbly | char (10) | YES | | NULL | |
| Age | Int (11) | YES | | NULL | |
| num | Int (11) | YES | | NULL | |
+--------+----------+------+-----+---------+-------+
Delete multiple fields at the same time
mysql> ALTER TABLE info drop column age,drop column num;
Query OK, 0 rows affected (0.05 sec)
records:0 duplicates:0 warnings:0
Mysql> SELECT * from info;
+----+--------+-------+-----------+
| ID | name | Score | hobbly |
+----+--------+-------+-----------+
| 1 | ZL | 88 | Reading |
| 2 | John Doe | 68 | Internet access |
| 3 | Wangsi | 68 | Watch TV |
+----+--------+-------+-----------+
Delete multiple record strips (multiple lines) at the same time
mysql> Delete from info where ID in (3,4);
Query OK, 2 rows Affected (0.00 sec)
Mysql> SELECT * from info;
+----+--------+-------+--------+
| ID | name | Score | hobbly |
+----+--------+-------+--------+
| 1 | ZL | 88 | Reading |
| 2 | John Doe | 68 | Internet access |
| 5 | KL | 88 | Swimming |
+----+--------+-------+--------+
View more than one specified record (row) at a time
Mysql> SELECT * FROM info where ID in (3,4);
+----+--------+-------+-----------+
| ID | name | Score | hobbly |
+----+--------+-------+-----------+
| 3 | Wangsi | 68 | Watch TV |
| 4 | ll | 67 | Reading |
+----+--------+-------+-----------+
MySQL database Getting Started command extension