当前位置:Gxlcms >
数据库问题 >
MySQL----主键(PRIMARY KEY)和自增(AUTO INCREMENT)
MySQL----主键(PRIMARY KEY)和自增(AUTO INCREMENT)
时间:2021-07-01 10:21:17
帮助过:3人阅读
测试主键
CREATE TABLE test_primary_key(
id INT UNSIGNED
PRIMARY KEY,
username VARCHAR(
20)
);
INSERT test_primary_key(id,username)
VALUES(
1,
‘NAME1‘);
INSERT test_primary_key(id,username)
VALUES(
1,
‘name2‘);
INSERT test_primary_key(username)
VALUES(
‘name3‘);
CREATE TABLE test_primary_key1(
id INT UNSIGNED
KEY,
username VARCHAR(
20)
);
CREATE TABLE test_primary_key2(
id INT UNSIGNED,
username VARCHAR(
20),
PRIMARY KEY(id)
);
-- CREATE TABLE test_primary_key3(
-- id INT UNSIGNED PRIMARY KEY,
-- courseId INT UNSIGNED PRIMARY KEY,
-- username VARCHAR(20),
-- email VARCHAR(50)
-- );
-- 复合主键
CREATE TABLE test_primary_key3(
id INT UNSIGNED,
courseId VARCHAR(
20),
username VARCHAR(
20),
email VARCHAR(
50),
PRIMARY KEY(id,courseId)
---<<---相当于通过id 和 courseId 都可以定位到要查询的记录
);
-- 1-a
INSERT test_primary_key3(id,courseId,username,email)
VALUES(
1,
1,
‘xiaoming‘,
‘8@qq.com‘);
INSERT test_primary_key3(id,courseId,username,email)
VALUES(
2,
1,
‘xiaohong‘,
‘71946@qq.com‘);
INSERT test_primary_key3(id,courseId,username,email)
VALUES(
3,
1,
‘张三‘,
‘3946@qq.com‘);
INSERT test_primary_key3(id,courseId,username,email)
VALUES(
4,
1,
‘李四‘,
‘17@qq.com‘);
-- 测试AUTO_INCREMENT
CREATE TABLE test_auto_increment(
id INT UNSIGNED
KEY AUTO_INCREMENT,
username VARCHAR(
20)
);
-- id INT UNSIGNED KEY AUTO_INCREMENT 中有AUTO_INCREMENT时一定要有KEY, 否则会报错.
INSERT test_auto_increment(username)
VALUES(
‘A‘);
INSERT test_auto_increment(username)
VALUES(
‘B‘);
INSERT test_auto_increment(username)
VALUES(
‘C‘);
INSERT test_auto_increment(id,username)
VALUES(
NULL,
‘E‘);
INSERT test_auto_increment(id,username)
VALUES(
DEFAULT,
‘F‘);
INSERT test_auto_increment(id,username) VALUES(‘‘,‘M‘);--报错, 不能为‘‘;
INSERT test_auto_increment(id,username) VALUES(15,‘G‘);--此刻id从15开始编号
mysql> select * from test_primary_key3;
+----+----------+----------+-------------+
| id | courseId | username | email |
+----+----------+----------+-------------+
| 1 | 1 | xiaoming | 8@qq.com |
| 2 | 1 | ?? | 3946@qq.com |
| 4 | 1 | 李四 | 17@qq.com |
+----+----------+----------+-------------+
3 rows in set (0.00 sec)
mysql> CREATE TABLE test_auto_increment(
-> id INT UNSIGNED KEY AUTO_INCREMENT,
-> username VARCHAR(20)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> DESC test_auto_increment;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(20) | YES | | NULL | |
+----------+------------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
INSERT test_auto_increment(username) VALUES(‘A‘);
INSERT test_auto_increment(username) VALUES(‘B‘);
INSERT test_auto_increment(username) VALUES(‘C‘);
INSERT test_auto_increment(id,username) VALUES(NULL,‘E‘);
INSERT test_auto_increment(id,username) VALUES(DEFAULT,‘F‘);
INSERT test_auto_increment(id,username) VALUES(15,‘G‘);
mysql> SELECT * FROM test_auto_increment;
+----+----------+
| id | username |
+----+----------+
| 1 | A |
| 2 | B |
| 3 | C |
| 4 | E |
| 5 | F |
| 15 | G |
+----+----------+
6 rows in set (0.00 sec)
MySQL----主键(PRIMARY KEY)和自增(AUTO INCREMENT)
标签:efault 效果 desc select ack username creat 测试 自增