標題:表的基本操作
包含內容:
創建表
創建主鍵,外鍵,非空,唯一,默認約束
設置表列屬性自動增長
查看錶結構
修改表名,字段數據類型,字段名,添加刪除字段,修改字段位置
更改表的存儲引擎
刪除外鍵約束
刪除表
--創建主鍵約束索引
mysql> create table t1(
-> col varchar(8) primary key,
-> col2 int
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> desc t1
-> ;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| col | varchar(8) | NO | PRI | NULL | |
| col2 | int(11) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql>
mysql> drop table t1;
Query OK, 0 rows affected (0.03 sec)
--設置非空約束
mysql> create table t1(
-> col int not null
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col | int(11) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
mysql> drop table t1;
Query OK, 0 rows affected (0.00 sec)
mysql> create table t1(
-> col1 int primary key,
-> col2 varchar(8)
-> );
Query OK, 0 rows affected (0.03 sec)
--設置T2表的外鍵約束(與T1相關),key值顯示爲"MUL"
mysql> create table t2(
-> col1 int,
-> col2 int
-> ,
-> constraint fk_t2 foreign key(col1) references t1(col1)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc t2;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | int(11) | YES | MUL | NULL | |
| col2 | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
--設置唯一性約束
mysql> create table t3(
-> col1 int unique key
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc t3;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | int(11) | YES | UNI | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)
--設置默認值約束
mysql> create table t4(
-> col1 varchar(8) default 'xxxx',
-> col2 int
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t4(col2) values(1);
Query OK, 1 row affected (0.03 sec)
mysql> select * from t4;
+------+------+
| col1 | col2 |
+------+------+
| xxxx | 1 |
+------+------+
1 row in set (0.00 sec)
--設置自動增長列
mysql> create table t5(
-> col1 int primary key auto_increment,
-> col2 varchar(8)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc t5;
+-------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+----------------+
| col1 | int(11) | NO | PRI | NULL | auto_increment |
| col2 | varchar(8) | YES | | NULL | |
+-------+------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> insert into t5 values (2,'cc');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t5;
+------+------+
| col1 | col2 |
+------+------+
| 2 | cc |
+------+------+
1 row in set (0.00 sec)
mysql> insert into t5(col2) values('xxx');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t5;
+------+------+
| col1 | col2 |
+------+------+
| 2 | cc |
| 3 | xxx |
+------+------+
2 rows in set (0.00 sec)
mysql> show create table t1 \G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`col1` int(11) NOT NULL,
`col2` varchar(8) DEFAULT NULL,
PRIMARY KEY (`col1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> desc t5;
+-------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+----------------+
| col1 | int(11) | NO | PRI | NULL | auto_increment |
| col2 | varchar(8) | YES | | NULL | |
+-------+------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
--修改表名
mysql> alter table t5 rename to t55;
Query OK, 0 rows affected (0.04 sec)
mysql> desc t5;
ERROR 1146 (42S02): Table 'zbk_db.t5' doesn't exist
mysql> desc t55;
+-------+------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+----------------+
| col1 | int(11) | NO | PRI | NULL | auto_increment |
| col2 | varchar(8) | YES | | NULL | |
+-------+------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
--修改列的數據類型
mysql> alter table t55 modify col1 int(12);
Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0
--修改列的名稱
mysql> alter table t55 change col1 col11 int(11);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc t55;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| col11 | int(11) | NO | PRI | 0 | |
| col2 | varchar(8) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
--添加表的列
mysql> alter table t55 add col3 varchar(8);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc t55;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| col11 | int(11) | NO | PRI | 0 | |
| col2 | varchar(8) | YES | | NULL | |
| col3 | varchar(8) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
--添除表的列
mysql> alter table t55 drop column col3;
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc t55;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| col11 | int(11) | NO | PRI | 0 | |
| col2 | varchar(8) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
--修改表中列的位置
mysql> alter table t55 modify col2 first;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'first' at line 1
mysql> alter table t55 modify col2 varchar(8) first;
Query OK, 2 rows affected (0.03 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> desc t55;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| col2 | varchar(8) | YES | | NULL | |
| col11 | int(11) | NO | PRI | 0 | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> show create table t55 \G
*************************** 1. row ***************************
Table: t55
Create Table: CREATE TABLE `t55` (
`col2` varchar(8) DEFAULT NULL,
`col11` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`col11`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
--修改表的存儲引擎
mysql> alter table t55 engine=myisam;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> show create table t55 \G
*************************** 1. row ***************************
Table: t55
Create Table: CREATE TABLE `t55` (
`col2` varchar(8) DEFAULT NULL,
`col11` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`col11`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> desc t2;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | int(11) | YES | MUL | NULL | |
| col2 | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> desc t1;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| col1 | int(11) | NO | PRI | NULL | |
| col2 | varchar(8) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> show constraint from t1 \G
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'constraint from t1' at line 1
mysql> show index from t1 \G
*************************** 1. row ***************************
Table: t1
Non_unique: 0
Key_name: PRIMARY
Seq_in_index: 1
Column_name: col1
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null:
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.00 sec)
--查看約束索引名
mysql> show index from t2 \G
*************************** 1. row ***************************
Table: t2
Non_unique: 1
Key_name: fk_t2
Seq_in_index: 1
Column_name: col1
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.00 sec)
--刪除外鍵約束
mysql> alter table t2 drop foreign key fk_t2;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from t2 \G
*************************** 1. row ***************************
Table: t2
Non_unique: 1
Key_name: fk_t2
Seq_in_index: 1
Column_name: col1
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
1 row in set (0.00 sec)
--可以看到,雖然刪除了約束,但索引仍存在,需要再次刪除索引
mysql> show create table t2 \G
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`col1` int(11) DEFAULT NULL,
`col2` int(11) DEFAULT NULL,
KEY `fk_t2` (`col1`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
--刪除索引
mysql> alter table t2 drop index fk_t2;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table t2 \G
*************************** 1. row ***************************
Table: t2
Create Table: CREATE TABLE `t2` (
`col1` int(11) DEFAULT NULL,
`col2` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
mysql> desc t2
-> ;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| col1 | int(11) | YES | | NULL | |
| col2 | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
MYSQL基礎01 - 表的操作
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.