mysql 创建主键问题
1个回答
展开全部
mysql>
create
table
lxl
(
a
char(1),
b
char(1)
);
Query
OK,
0
rows
affected
(0.10
sec)
mysql>
insert
into
lxl
VALUES
('a',
'b');
Query
OK,
1
row
affected
(0.00
sec)
mysql>
insert
into
lxl
VALUES
('c',
'd');
Query
OK,
1
row
affected
(0.00
sec)
mysql>
alter
table
lxl
add
id
int
primary
key;
ERROR
1062
(23000):
Duplicate
entry
'0'
for
key
'PRIMARY'
mysql>
alter
table
lxl
add
id
int
AUTO_INCREMENT
primary
key;
Query
OK,
2
rows
affected
(0.12
sec)
Records:
2
Duplicates:
0
Warnings:
0
mysql>
select
*
from
lxl;
+------+------+----+
|
a
|
b
|
id
|
+------+------+----+
|
a
|
b
|
1
|
|
c
|
d
|
2
|
+------+------+----+
2
rows
in
set
(0.01
sec)
create
table
lxl
(
a
char(1),
b
char(1)
);
Query
OK,
0
rows
affected
(0.10
sec)
mysql>
insert
into
lxl
VALUES
('a',
'b');
Query
OK,
1
row
affected
(0.00
sec)
mysql>
insert
into
lxl
VALUES
('c',
'd');
Query
OK,
1
row
affected
(0.00
sec)
mysql>
alter
table
lxl
add
id
int
primary
key;
ERROR
1062
(23000):
Duplicate
entry
'0'
for
key
'PRIMARY'
mysql>
alter
table
lxl
add
id
int
AUTO_INCREMENT
primary
key;
Query
OK,
2
rows
affected
(0.12
sec)
Records:
2
Duplicates:
0
Warnings:
0
mysql>
select
*
from
lxl;
+------+------+----+
|
a
|
b
|
id
|
+------+------+----+
|
a
|
b
|
1
|
|
c
|
d
|
2
|
+------+------+----+
2
rows
in
set
(0.01
sec)
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询