创建数据库staff_management;
使用staff_management;
创建表Employee ( eID int(100)非空IDENTITY(1,1)主键, eName varchar(255)NOT NULL, 作业文字NOT NULL, Salary int(100)NOT NULL, 通讯(100), hDate日期NOT NULL, dID int(10)NOT NULL,
constraint emp_pk primary key (eID)
); alter table员工IDENTITY(1,1)PRIMARY KEY = 1001;
变更表Employee 在eName之后添加Mgr int(100)列;
插入Employee(eName,Mgr,Job,Salary,Comm,hDate,dID) 值(“肯亚当斯”,1004,“推销员”,70000、20000,“ 2008-04-12”,1), (“鲁琼斯”,1004,“推销员”,65000、15000,“ 2010-01-18”,1), (“ Dhal Sim”,1006,“ Accountant”,88000,NULL,“ 2001-03-07”,2), (“ Ellen Honda”,1006,“ Manager”,118000,NULL,“ 2001-03-17”,1), (“ Mike Bal”,1006,“ Receptionist”,68000,NULL,“ 2006-06-21”,3), (“ Martin Bison”,NULL,“ CEO”,210000,NULL,“ 2010-07-12”,3), (“神力”,1004,“推销员”,86000、18000,“ 2014-09-18”,1), (“ Zang Ross”,1004,“ Salesman”,65000、10000,“ 2017-02-02”,1), (“ Sagar Kahn”,1004,“ Salesman”,70000,15000,“ 2016-03-01”,1);
变更表Employee 在删除集NULL上的更新级联上添加约束emp_mgr_fk外键(Mgr)引用Employee(eID);
创建表部 ( dID int(10)NOT NULL唯一IDENTITY(1,1)PRIMARY KEY, dName varchar(255)不为null, 地址文字, 电话文字,
constraint d_pk primary key (dID)
);
变更表Employee 添加约束emp_d_fk 外键(dID)引用部门(dID);
创建表等级 ( gID char(10)不为null唯一, MinSal int(100), MaxSal int(100), Leavee int(10),
constraint g_pk primary key (gID)
);
插入INTO等级(gID,MinSal,MaxSal,Leavee) 值('A',NULL,60000、20), ('B',60000,80000,20), ('C',80000,100000,20), ('D',100000,120000,25), (“ E”,120000,NULL,30);
从成绩中选择*;
插入部门(dName,地址,电话) 值(“销售”,“悉尼”,“ 0425 198 053”), (“帐户”,“墨尔本”,“ 0429 198 955”), (“管理员”,“墨尔本”,“ 0428 198 758”), (“营销”,“悉尼”,“ 0427 198 757”);
从部门选择*;
我的代码有问题
第156条消息,第15级,状态1,第18行 关键字“ IDENTITY”附近的语法不正确。 消息156,第15层,状态1,第21行 关键字“ column”附近的语法不正确。
答案 0 :(得分:1)
SQL Server 的语法为否 auto_increment
-您需要使用INT IDENTITY
列。而且:在T-SQL中,INT
是INT
是INT
-无法定义“精度”):
代替此:
create table Employee
(
eID int(100) NOT NULL auto_increment,
使用此:
CREATE TABLE dbo.Employee
(
eID INT NOT NULL IDENTITY(1,1),
答案 1 :(得分:0)
此问题发布在SQL Server之下,SQL Server是Microsoft而不是MySQL,但是您的语法似乎适用于MySQL。
如果您正在运行MSSQL,则下面的代码将可用,但是当您尝试为同一表中的列创建外键时,您需要查看外键。通常,引入键时,它与另一个表有关。例如,等级FK引用了员工PK。
create database staff_management;
use staff_management;
create table Employee
(
eID int NOT NULL IDENTITY(1,1) PRIMARY KEY,
eName varchar(255) NOT NULL,
Job text NOT NULL,
Salary int NOT NULL,
Comm int,
hDate date NOT NULL,
dID int NOT NULL,
);
alter table Employee
add Mgr int;
insert into Employee(eName,Mgr, Job, Salary, Comm, hDate, dID)
values ('ken Adams', 1004, 'Salesman', 70000, 20000, '2008-04-12', 1),
('Ru Jones', 1004, 'Salesman', 65000, 15000, '2010-01-18', 1),
('Dhal Sim', 1006, 'Accountant', 88000, NULL, '2001-03-07', 2),
('Ellen Honda', 1006, 'Manager', 118000, NULL, '2001-03-17', 1),
('Mike Bal', 1006, 'Receptionist', 68000, NULL, '2006-06-21', 3),
('Martin Bison',NULL, 'CEO', 210000, NULL, '2010-07-12', 3),
('Shen Li', 1004, 'Salesman', 86000, 18000, '2014-09-18', 1),
('Zang Ross', 1004, 'Salesman', 65000, 10000, '2017-02-02', 1),
('Sagar Kahn', 1004, 'Salesman', 70000, 15000, '2016-03-01', 1);
--alter table Employee
--add constraint emp_mgr_fk
-- foreign key (Mgr) references Employee(eID)
-- on update cascade on delete set NULL;
create table Department
(
dID int NOT NULL IDENTITY(1,1) PRIMARY KEY,
dName varchar(255) not null,
Address text,
phone text,
);
--alter table Employee
--add constraint emp_d_fk
--foreign key (dID) references Department(dID);
create table Grade
(
gID char(10) not null unique,
MinSal int,
MaxSal int,
Leavee int,
constraint g_pk primary key (gID)
);
INSERT INTO Grade (gID, MinSal, MaxSal, Leavee)
VALUES ('A', NULL, 60000, 20),
('B', 60000, 80000, 20),
('C', 80000, 100000, 20),
('D', 100000, 120000, 25),
('E', 120000, NULL, 30);
select * from Grade;
insert into Department (dName, Address, phone)
values ('Sales', 'Sydney', '0425 198 053'),
('Accounts', 'Melbourne', '0429 198 955'),
('Admin', 'Melbourne', '0428 198 758'),
('Marketing', 'Sydney', '0427 198 757');
select * from Department;