我的 SQL Auto_increment 不适用于固定列
My SQL Auto_increment not working for a fixed column
我正在尝试使用 MySQL Workbench 创建三个 table,其中两列需要使用固定的起始值自动递增。我查看了一些在线资源并想出了要使用的语句。
create database test;
use test;
/*table Project */
create table Project(
Pnumber INT NOT NULL AUTO_INCREMENT,
Pname varchar(30) NOT NULL,
Plocation enum ('QLD', 'VIC', 'NSW', 'SA') NOT NULL,
primary key(Pnumber)
);
ALTER TABLE Project AUTO_INCREMENT = 7777770;
/*table Department*/
Create table Department(
Dcode varchar(5) NOT NULL,
Dname varchar(30),
Dmg_ssn varchar(30),
primary key(Dcode)
);
/* Table Employee*/
create table Employee(
Ssn INT NOT NULL AUTO_INCREMENT,
Ename varchar(30) NOT NULL,
Bdate DATE,
Address varchar(30),
Dcode varchar(5) NOT NULL,
Driver_License varchar(30),
primary key(Ssn),
foreign key(Dcode) references Department(Dcode)
);
ALTER TABLE Employee AUTO_INCREMENT = 1000000;
/* Insert into Project*/
Insert into Project values ('7777770','Star', 'QLD');
Insert into Project values ('7777771','Innova', 'NSW');
Insert into Project values ('7777772','Andra', "QLD");
/* Insert into Department */
insert into Department values ('ABC12', 'Finances', 'RA12');
insert into Department values ('WXY10', 'Human Resources', 'RA12');
insert into Department values ('PBC32', 'S2', 'RB13');
/*Insert into Employee */
insert into Employee values ('0000001','Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456');
insert into Employee values ('0000002','Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947');
insert into Employee values ('0000003','Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655');
但是,在我创建table之后,只有tableProject
中的Pnumber列符合要求的格式,而Employee
中的Ssn列没有.
这是 SELECT 超过 Employee
table:
Ssn,Ename,Bdate,Address,Dcode,Driver_License
1,"Vladimir Rostov",2008-07-04,"19 Wilson St",ABC12,1023456
2,"Rory Reid",2002-02-10,"10 Mary St",WXY10,2365947
3,"Andy Murray",2001-05-11,"1280 Albert St",WXY10,5891655
知道我做错了什么吗??
您的 sql 脚本有一个 employee
table,而不是 student
,我假设这两个名称指的是同一个 table。我将使用 employee
名称。
因此,您将 employee
table 的起始值自动递增设置为 1000000:
ALTER TABLE Employee AUTO_INCREMENT = 1000000;
但是随后您使用插入语句将 1、2 和 3 显式插入此列,因为 '0000001'
转换为 1
。如果您明确地将一个值插入自动递增字段并且它高于给定字段中的最大值,那么 mysql 将按原样将该值插入自动递增字段。
如果您正在使用自动递增,那么您应该让它工作并且不要指定显式值:
/*Insert into Employee */
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456');
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947');
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655');
我正在尝试使用 MySQL Workbench 创建三个 table,其中两列需要使用固定的起始值自动递增。我查看了一些在线资源并想出了要使用的语句。
create database test;
use test;
/*table Project */
create table Project(
Pnumber INT NOT NULL AUTO_INCREMENT,
Pname varchar(30) NOT NULL,
Plocation enum ('QLD', 'VIC', 'NSW', 'SA') NOT NULL,
primary key(Pnumber)
);
ALTER TABLE Project AUTO_INCREMENT = 7777770;
/*table Department*/
Create table Department(
Dcode varchar(5) NOT NULL,
Dname varchar(30),
Dmg_ssn varchar(30),
primary key(Dcode)
);
/* Table Employee*/
create table Employee(
Ssn INT NOT NULL AUTO_INCREMENT,
Ename varchar(30) NOT NULL,
Bdate DATE,
Address varchar(30),
Dcode varchar(5) NOT NULL,
Driver_License varchar(30),
primary key(Ssn),
foreign key(Dcode) references Department(Dcode)
);
ALTER TABLE Employee AUTO_INCREMENT = 1000000;
/* Insert into Project*/
Insert into Project values ('7777770','Star', 'QLD');
Insert into Project values ('7777771','Innova', 'NSW');
Insert into Project values ('7777772','Andra', "QLD");
/* Insert into Department */
insert into Department values ('ABC12', 'Finances', 'RA12');
insert into Department values ('WXY10', 'Human Resources', 'RA12');
insert into Department values ('PBC32', 'S2', 'RB13');
/*Insert into Employee */
insert into Employee values ('0000001','Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456');
insert into Employee values ('0000002','Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947');
insert into Employee values ('0000003','Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655');
但是,在我创建table之后,只有tableProject
中的Pnumber列符合要求的格式,而Employee
中的Ssn列没有.
这是 SELECT 超过 Employee
table:
Ssn,Ename,Bdate,Address,Dcode,Driver_License
1,"Vladimir Rostov",2008-07-04,"19 Wilson St",ABC12,1023456
2,"Rory Reid",2002-02-10,"10 Mary St",WXY10,2365947
3,"Andy Murray",2001-05-11,"1280 Albert St",WXY10,5891655
知道我做错了什么吗??
您的 sql 脚本有一个 employee
table,而不是 student
,我假设这两个名称指的是同一个 table。我将使用 employee
名称。
因此,您将 employee
table 的起始值自动递增设置为 1000000:
ALTER TABLE Employee AUTO_INCREMENT = 1000000;
但是随后您使用插入语句将 1、2 和 3 显式插入此列,因为 '0000001'
转换为 1
。如果您明确地将一个值插入自动递增字段并且它高于给定字段中的最大值,那么 mysql 将按原样将该值插入自动递增字段。
如果您正在使用自动递增,那么您应该让它工作并且不要指定显式值:
/*Insert into Employee */
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456');
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947');
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655');