PRIMARY KEY 主键
AUTO_INCREMENT 自增长
FOREIGN KEY 外键
NOT NULL 非空
UNIQUE KEY 唯一
DEFAULT 默认值
主键:唯一标识符,身份证号和人的关系就相当于主键和记录的关系
一般加到无意义的字段上例如:编号字段
定义主键字段的要求:不能重复、被标志成主键的字段自动非空
分为单字段主键和多字段主键
--测试主键
CREATE TABLE IF NOT EXISTS user1(
id INT PRIMARY KEY,
username VARCHAR(20)
);
Query OK, 0 rows affected (0.09 sec)
DESC user1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| username | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
2 rows in set (0.04 sec)
--查看创建表的定义
SHOW CREATE TABLE user1;
+-------+----------------------------------------------------------------------------
---------------------------------------------------------+
| Table | Create Table
|
+-------+----------------------------------------------------------------------------
---------------------------------------------------------+
| user1 | CREATE TABLE `user1` (
`id` int(11) NOT NULL,
`username` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------
---------------------------------------------------------+
1 row in set (0.00 sec)
--主键值的唯一性
INSERT user1 VALUES(2,'QUEEN');
Query OK, 1 row affected (0.01 sec)
INSERT user1 VALUES(1,'KING');
Query OK, 1 row affected (0.00 sec)
INSERT user1 VALUES(1,'KING');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
--查看记录
SELECT * FROM user1;
+----+----------+
| id | username |
+----+----------+
| 1 | KING |
| 2 | QUEEN |
+----+----------+
2 rows in set (0.00 sec)
--查看id为1的记录
SELECT * FROM user1 WHERE id=1;
+----+----------+
| id | username |
+----+----------+
| 1 | KING |
+----+----------+
1 row in set (0.01 sec)
--设置两个主键
mysql> CREATE TABLE IF NOT EXISTS userb(
-> id INT,
-> username VARCHAR(20),
-> card CHAR(18),
-> PRIMARY KEY (id,card)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> DESC userb;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | 0 | |
| username | varchar(20) | YES | | NULL | |
| card | char(18) | NO | PRI | | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.05 sec)
--插入值(复合主键时,两个主键的值完全相同的记录不能插入)
mysql> INSERT userb VALUES(1,'KING','111');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT userb VALUES(1,'QUEEN','112');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT userb VALUES(1,'PRINCESS','112');
ERROR 1062 (23000): Duplicate entry '1-112' for key 'PRIMARY'
mysql> SELECT * FROM userb;
+----+----------+------+
| id | username | card |
+----+----------+------+
| 1 | KING | 111 |
| 1 | QUEEN | 112 |
+----+----------+------+
2 rows in set (0.00 sec)
一个表中只能有一个自增长字段并且要配合主键使用,标识成自增长的字段一定是主键,但是主键不一定被标志成自增长
自增长只对整数整数列有效果,像字符串就没有意义了
默认从1开始每次加1
mysql> CREATE TABLE IF NOT EXISTS userc(
id SMALLINT KEY AUTO_INCREMENT,
username VARCHAR(20)
);
Query OK, 0 rows affected (0.02 sec)
mysql> DESC userc;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | smallint(6) | NO | PRI | NULL | auto_increment |
| username | varchar(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
---自动加1
mysql> INSERT userc VALUES(1,'KING');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT userc(username) VALUES('QUEEN');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM userc;
+----+----------+
| id | username |
+----+----------+
| 1 | KING |
| 2 | QUEEN |
+----+----------+
2 rows in set (0.00 sec)
--插入其他不连续的id值
mysql> INSERT userc VALUES(111,'KING1');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM userc;
+-----+----------+
| id | username |
+-----+----------+
| 1 | KING |
| 2 | QUEEN |
| 111 | KING1 |
+-----+----------+
3 rows in set (0.00 sec)
--再次插入值
mysql> INSERT userc(username) VALUES('QUEEN1');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM userc;
+-----+----------+
| id | username |
+-----+----------+
| 1 | KING |
| 2 | QUEEN |
| 111 | KING1 |
| 112 | QUEEN1 |
+-----+----------+
4 rows in set (0.00 sec)
--查询细节
mysql> SHOW CREATE TABLE userc;
+-------+-------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+-------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------+
| userc | CREATE TABLE `userc` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`username` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=113 DEFAULT CHARSET=utf8 |
+-------+-------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
--在自增长值中插入NULL 和 DEFAULT 记录
mysql> INSERT userc VALUES(NULL,'ABC');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT userc VALUES(DEFAULT,'ABC');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM userc;
+-----+----------+
| id | username |
+-----+----------+
| 1 | KING |
| 2 | QUEEN |
| 111 | KING1 |
| 112 | QUEEN1 |
| 113 | ABC |
| 114 | ABC |
+-----+----------+
6 rows in set (0.00 sec)
---修改初始自增长值
mysql> CREATE TABLE IF NOT EXISTS userd(
id SMALLINT KEY AUTO_INCREMENT,
username VARCHAR(20)
)AUTO_INCREMENT=100;
Query OK, 0 rows affected (0.02 sec)
mysql> SHOW CREATE TABLE userd;
+-------+-------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+-------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------+
| userd | CREATE TABLE `userd` (
`id` smallint(6) NOT NULL AUTO_INCREMENT,
`username` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=100 DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> INSERT userd(username) VALUES('QUEEN1');
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM userd;
+-----+----------+
| id | username |
+-----+----------+
| 100 | QUEEN1 |
+-----+----------+
1 row in set (0.00 sec)
--修改自增长的值
ALTER TABLE userd AUTO_INCREMENT =500;
--测试非空(代表字段一定有值而且不能为空值)
mysql> CREATE TABLE IF NOT EXISTS userf(
-> id INT UNSIGNED KEY AUTO_INCREMENT,
-> username VARCHAR(20) NOT NULL,
-> password CHAR(32) NOT NULL,
-> age TINYINT UNSIGNED
-> );
Query OK, 0 rows affected (0.05 sec)
mysql> INSERT userf(username,password) VALUES('KING','KING');
Query OK, 1 row affected (0.01 sec)
mysql> INSERT userf(username,password,age) VALUES('KING1','KING1',12);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT *FROM userf;
+----+----------+----------+------+
| id | username | password | age |
+----+----------+----------+------+
| 1 | KING | KING | NULL |
| 2 | KING1 | KING1 | 12 |
+----+----------+----------+------+
2 rows in set (0.00 sec)
--插入空值报错
mysql> INSERT userf(username,password) VALUES(NULL,NULL);
ERROR 1048 (23000): Column 'username' cannot be null
头像可以设置一个默认的图片
CREATE TABLE IF NOT EXISTS userg(
id INT UNSIGNED KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL,
password CHAR(32) NOT NULL,
age TINYINT UNSIGNED DEFAULT 18,
addr VARCHAR(50) NOT NULL DEFAULT '北京',
sex ENUM('男','女','保密') NOT NULL DEFAULT '男'
);
INSERT userg(username,password) VALUES('KING','KING');
mysql> SELECT * FROM userg;
+----+----------+----------+------+------+-----+
| id | username | password | age | addr | sex |
+----+----------+----------+------+------+-----+
| 1 | KING | KING | 18 | 北京 | 男 |
+----+----------+----------+------+------+-----+
--测试其他默认值
INSERT userg VALUES(3,'QUEEN','QUEEN',29,'上海','保密');
INSERT userg VALUES(4,'QUEEN','QUEEN',DEFAULT,DEFAULT,'保密');
字段不能重复的时候使用
CREATE TABLE IF NOT EXISTS userj(
id TINYINT UNSIGNED KEY AUTO_INCREMENT,
username VARCHAR(20) NOT NULL UNIQUE,
card CHAR(18) UNIQUE
);
mysql> DESC userj;
+----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(20) | NO | UNI | NULL | |
| card | char(18) | YES | UNI | NULL | |
+----------+---------------------+------+-----+---------+----------------+
3 rows in set (0.02 sec)
mysql> SHOW CREATE TABLE userj;
+-------+-------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+-------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------+
| userj | CREATE TABLE `userj` (
`id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(20) NOT NULL,
`card` char(18) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `card` (`card`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> INSERT userj(username) VALUES('A');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT userj(username) VALUES('A1');
Query OK, 1 row affected (0.00 sec)
--NULL值不算重复
mysql> SELECT * FROM userj;
+----+----------+------+
| id | username | card |
+----+----------+------+
| 1 | A | NULL |
| 2 | A1 | NULL |
+----+----------+------+
2 rows in set (0.00 sec)
--报错username是个唯一性字段
mysql> INSERT userj(username) VALUES('A1');
ERROR 1062 (23000): Duplicate entry 'A1' for key 'username'
mysql> INSERT user9(username,card) VALUES('B','111');
Query OK, 1 row affected (0.00 sec)
--NULL值不算重复都成功了
mysql> INSERT userj(username,card) VALUES('B1',NULL);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT userj(username,card) VALUES('B2',NULL);
Query OK, 1 row affected (0.00 sec)
CREATE TABLE [IF NOT EXISTS] tbl_name(
字段名称 字段类型 [UNSIGNED|ZEROFILL] [NULL|NOTNULL] [DEFAULT默认值] [[PRIMARY] KEY| UNIQUE[KEY]] [AUTO_INCREMENT一定和主键联合使用] 要按照这个顺序去写或者去省略
)ENGINE=INNODB, CHARSET=utf8, AUTO_INCREMENT=100;