2016-03-14 165 views
1

我得到这个错误SQL错误消息142,级别15,状态2,行0的 '表' 约束的定义不正确的语法

消息142,级别15,状态2,0行
'TABLE'约束定义的语法不正确。

我无法弄清楚,我用尽了时间......只是使用SQL的上半部分我仍然需要编辑其余部分,但是我需要解决此错误......或者有关什么信息我做错了...我也有问题确定NULL和不为空

Create table Service 
(
    ServiceCode varchar(15) not null 
     constraint PK_Service primary key clustered, 
    Description varchar(100) not null, 
    CostPerHour smallmoney not null 
) 


create table SupplyCategory 
(
    SupplyCategoryCode varchar(5) not null 
     constraint PK_SupplyCategory primary key clustered, 
    Description varchar(100)not null, 
    StorageRoom varchar(5)not null 
) 

create table Supply 
(
    SupplyCode varchar(8)not null 
     constraint PK_Supply primary key clustered, 
    Description varchar(100)not null, 
    SupplyCategoryCode varchar(5) not null 
     constraint FK_SupplyToSupplyCategory references SupplyCategory(SupplyCategoryCode) 
) 

create table StaffType 
(
    StaffTypeCode int identity(1,1)not null 
     constraint PK_StaffType primary key clustered, 
    Description varchar(100)not null, 
    Wage smallmoney not null 
     constraint CK_Wage check(Wage >=0) 
     constraint DF_Wage default 20 
) 

create table Staff 
(
    StaffID int not null 
     constraint PK_Staff primary key clustered, 
    FirstName varchar(50) not null, 
    LastName varchar(50) not null, 
    TrainingCredits smallint not null, 
    StaffTypeCode int not null 
     constraint FK_StaffToStaffType references StaffType(StaffTypecode) 
) 

create table Client 
(
    ClientID int identity(1,1) not null 
     constraint PK_Client primary key clustered, 
    FirstName varchar(50) not null, 
    LastName varchar(50) not null, 
    Phone varchar(14) null 
     constraint CK_Phone check(Phone like '([0-9][0-9][0-9]) [0-9][0-9][0-9]-[0-9][0-9][0-9]') 
) 

create table Job 
(
    JobNumber int identity(1,1) not null 
     constraint PK_Job primary key clustered, 
    Date datetime not null, 
    Address varchar(100) not null, 
    City varchar(50) not null, 
    Province char(2) not null 
     constraint CK_Province check(Province like '[^A-Z][^A-Z]'), 
    PostalCode char(7) null 
     constraint CK_PostalCode Check(PostalCode like '[A-Z][0-9][A-Z] [0-9][A-Z][0-9]'), 
    SubTotal money not null, 
    GST money not null, 
    Total money not null 
     constraint CK_TotalRecivedMustBeAfterSubTotal check(Total > SubTotal), 

    ClientID int not null 
     constraint FK_JobToClient references Client(ClientID), 
    StaffID int not null 
     constraint FK_JobToStaff references Staff(StaffID) 
) 

create table JobService 
(
    JobNumber int not null 
     constraint FK_JobServiceToJob references Job(JobNumber), 
    ServiceCode varchar(15) not null 
     constraint FK_JobServiceToService references Service(ServiceCode), 
    Notes varchar(200) not null, 
    Hours int not null, 
    ActualCostPerHour smallmoney not null, 
    ExtCost smallmoney not null, 
    constraint PK_JobService primary key clustered (JobNumber,ServiceCode) 
) 

create table Training 
(
    TrainingID varchar(20) not null 
     constraint PK_Training primary key clustered, 
    Description varchar(100) not null, 
    Credits tinyint 
     constraint CK_Credits check(Credits <=6) 
     constraint DF_Credits default 3 
) 

create table StaffTraining 
(
    StaffID int not null 
     constraint FK_StaffTrainingToStaff references Staff(StaffID), 
    TrainingID varchar(20) not null 
     constraint FK_StaffTrainingToTraining references Training(TrainingID),  
     CompletionDate datetime not null, 
    constraint PK_StaffTraining primary key clustered(StaffID,TrainingID) 
) 

create table JobSupply 
(
    JobNumber int not null 
     constraint FK_JobSupplyToJob references Job(JobNumber), 
    SupplyCode varchar(8) not null 
     constraint FK_JobSupplyToSupply references Supply(SupplyCode), 
    Quantity smallint not null, 
    constraint PK_JobSupply primary key clustered (JobNumber,SupplyCode) 
) 

Delete JobService 
Delete Service 
Delete JobSupply 
Delete Supply 
Delete SupplyCategory 
Delete Job 
Delete Client 
Delete StaffTraining 
Delete Training 
Delete Staff 
Delete StaffType 


Alter table Client 
Add Email varchar(300) 
    constraint ck_Email check(Email like '%@%.%') 

Alter table Staff 
Add Available char(1) null 
      default 'Y' 
       constraint CK_Available check(Available in ('Y','N')) 

Alter table job 
Add constraint DF_Province default 'AB' 

CREATE nonclustered INDEX IX_JobNumber 
     ON JobService (JobNumber) 

CREATE nonclustered INDEX IX_ServiceCode 
     ON JobService (ServiceCode) 

CREATE nonclustered INDEX IX_ClientId 
     ON Job (ClientID) 

CREATE nonclustered INDEX IX_StaffID 
     ON Job (StaffID) 

CREATE nonclustered INDEX IX_JobNumber 
     ON JobSupply (JobNumber) 

CREATE nonclustered INDEX IX_SupplyCode 
     ON JobSupply (SupplyCode) 

CREATE nonclustered INDEX IX_SupplyCategoryCode 
     ON Supply (SupplyCategoryCode) 

CREATE nonclustered INDEX IX_StaffTypeCode 
     ON Staff (StaffTypeCode) 

CREATE nonclustered INDEX IX_StaffID 
     ON StaffTraining (StaffID) 

CREATE nonclustered INDEX IX_TrainingID 
     ON StaffTraining (TrainingID) 

Insert into StaffType (Description,Wage) 
values ('Painter',25) 
Insert into StaffType (Description,Wage) 
values ('Sander',20) 
Insert into StaffType (Description,Wage) 
values ('Builder',30) 
Insert into StaffType (Description,Wage) 
values ('Demolition',35) 
Insert into StaffType (Description,Wage) 
values ('Cleaning',15) 


Insert into Staff (StaffID,FirstName,LastName,TrainingCredits,StaffTypeCode) 
values (11111,'Jason','Painter',12,1) 
Insert into Staff (StaffID,FirstName,LastName,TrainingCredits,StaffTypeCode) 
values (22222,'Suzy','Cleaner',12,5) 
Insert into Staff (StaffID,FirstName,LastName,TrainingCredits,StaffTypeCode) 
values (33333,'Alex','Boom',13,4) 
Insert into Staff (StaffID,FirstName,LastName,TrainingCredits,StaffTypeCode) 
values (44444,'Adam','Scraper',15,2) 
Insert into Staff (StaffID,FirstName,LastName,TrainingCredits,StaffTypeCode) 
values (55555,'Bob','LaBuilder',21,3) 

Insert into Training (TrainingID, Description, Credits) 
values ('Paint101','Introduction to Painting',3) 
Insert into Training (TrainingID, Description, Credits) 
values ('Finishing123','Sanding and Finishing',6) 
Insert into Training (TrainingID, Description, Credits) 
values ('Cleaning224','Cleaning and Clearing',3) 
Insert into Training (TrainingID, Description, Credits) 
values ('Demolition101','Making Things go Boom!',4) 
Insert into Training (TrainingID, Description, Credits) 
values ('Safety104','Basic Safety Protocols',3) 
Insert into Training (TrainingID, Description, Credits) 
values ('Repairs105','Basic Repairs',3) 
Insert into Training (TrainingID, Description, Credits) 
values ('ClientConflict202','Dealing with Grumpy Clients',6) 
Insert into Training (TrainingID, Description, Credits) 
values ('Building321','Basic Building Concepts',6) 


Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (11111,'Paint101','Jan 1 2016') 
Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (11111,'Safety104','Jan 4 2016') 
Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (11111,'ClientConflict202','Jan 7 2016') 

Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (22222,'Cleaning224','Jan 1 2016') 
Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (22222,'Safety104','Jan 4 2016') 
Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (22222,'ClientConflict202','Jan 7 2016') 

Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (33333,'Demolition101','Jan 1 2016') 
Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (33333,'Safety104','Jan 4 2016') 
Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (33333,'ClientConflict202','Jan 7 2016') 

Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (44444,'Finishing123','Jan 1 2016') 
Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (44444,'Safety104','Jan 4 2016') 
Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (44444,'ClientConflict202','Jan 7 2016') 

Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (55555,'Building321','Jan 1 2016') 
Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (55555,'Safety104','Jan 4 2016') 
Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (55555,'ClientConflict202','Jan 7 2016') 
Insert into StaffTraining(StaffID, TrainingID, CompletionDate) 
values (55555,'Finishing123','Jan 7 2016') 

Insert into Client (FirstName, LastName, Phone) 
values ('Peggy', 'Sue','(780) 111-1111') 
Insert into Client (FirstName, LastName, Phone) 
values ('Maggie', 'May','(780) 222-2222') 
Insert into Client (FirstName, LastName, Phone) 
values ('Billy', 'Jean','(780) 111-1111') 
Insert into Client (FirstName, LastName, Phone) 
values ('Bobby', 'McGee','(780) 111-1111') 
Insert into Client (FirstName, LastName, Phone) 
values ('Tom', 'Dooly','(780) 111-1111') 
Insert into Client (FirstName, LastName, Phone) 
values ('Mary', 'Jane','(780) 111-1111') 
Insert into Client (FirstName, LastName, Phone) 
values ('Jimmy', 'Mack','(780) 111-1111') 
Insert into Client (FirstName, LastName, Phone) 
values ('Eleanor', 'Rigby','(780) 111-1111') 

Insert Into Service (ServiceCode,Description,CostPerHour) 
values ('Prime1','Priming',20) 
Insert Into Service (ServiceCode,Description,CostPerHour) 
values ('Painting1','Painting',20) 
Insert Into Service (ServiceCode,Description,CostPerHour) 
values ('Construction10','Basic Reconstruction',40) 
Insert Into Service (ServiceCode,Description,CostPerHour) 
values ('Construction20','Advanced Construction',60) 
Insert Into Service (ServiceCode,Description,CostPerHour) 
values ('Demolition','Demolition of Property',30) 
Insert Into Service (ServiceCode,Description,CostPerHour) 
values ('Sanding1','Surface Sanding',20) 
Insert Into Service (ServiceCode,Description,CostPerHour) 
values ('Cleaning1','Basic Cleaning',20) 
Insert Into Service (ServiceCode,Description,CostPerHour) 
values ('Garbage1','Removal of Graffit Refuse',25) 




Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('Jan 1 2016','12345 Anywhere Street','Edmonton', 'AB','T3D 1S5',120,26,126,11111,1) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (1,'Prime1','Prime fence beside the house',3,20,60) 
Insert Into JobService (JobNumber, ServiceCode, Notes,hours, ActualCostPerHour, extcost) 
values (1,'Painting1','Prime fence beside the house',3,20,60) 

Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('Jan 2 2016','12345 First Street','Edmonton', 'AB','T3A 1A5',160,8,168,11111,1) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (2,'Sanding1','Sand the wall',5,20,100) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (2,'Painting1','Paint the wall',3,20,60) 

Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('Jan 3 2016','12345 Second Street','Edmonton', 'AB','T3B 1B5',20,1,21,11111,1) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (3,'Painting1','Paint the door',1,20,20) 

Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('Jan 3 2016','12345 Third Street','Edmonton', 'AB','T3C 1C5',40,2,42,22222,2) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (4,'Cleaning1','Clean the wall',2,20,40) 


Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('Jan 4 2016','12345 Fourth Street','Edmonton', 'AB','T3D 1D5',80,4,84,22222,2) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (5,'Cleaning1','Clean the fence',4,20,80) 


Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('February 5 2016','12345 Fifth Street','Edmonton', 'AB','T3E 1E5',40,2,42,11111,3) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (6,'Painting1','Paint the sign',2,20,40) 


Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('Jan 6 2016','12345 Sixth Street','Edmonton', 'AB','T3F 1F5',60,3,63,33333,5) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (7,'Demolition','Take down the sign',2,30,60) 


Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('Jan 7 2016','12345 Seventh Street','Edmonton', 'AB','T3G 1G5',110,5.5,115.5,33333,1) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (8,'Demolition','Clean the wall',2,30,60) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (8,'Garbage1','Clean the wall',2,25,50) 

Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('February 8 2016','12345 Eighth Street','Edmonton', 'AB','T3H 1H5',40,2,42,44444,6) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (9,'Sanding1','Sand the blue fence',2,20,40) 


Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('February 27 2016','12345 Ninth Street','Edmonton', 'AB','T3I 1I5',20,1,21,44444,7) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (10,'Sanding1','Sand the table',1,20,20) 


Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('February 28 2016','12345 Tenth Street','Edmonton', 'AB','T3J 1J5',200,10,210,55555,8) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (11,'Construction10','Rebiuld Table',5,40,200) 


Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('March 1 2016','12345 Eleventh Street','Edmonton', 'AB','T3K 1K5',600,30,630,55555,3) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (12,'Construction20','Build new shed',10,60,600) 


Insert into Job (Date, Address, City,Province,PostalCode,SubTotal,GST,Total,StaffID,ClientID) 
values ('March 3 2016','12345 Twelth Street','Edmonton', 'AB','T3L 1L5',120,6,126,11111,8) 
Insert Into JobService (JobNumber, ServiceCode, Notes, Hours,ActualCostPerHour, extcost) 
values (13,'Cleaning1','Clean the whole house!',12,10,120) 


Insert Into SupplyCategory (SupplyCategoryCode, Description, StorageRoom) 
values ('P5','5 Gallon Paint Products','A101') 
Insert Into SupplyCategory (SupplyCategoryCode, Description, StorageRoom) 
values ('P10','10 Gallon Paint Products','A101') 
Insert Into SupplyCategory (SupplyCategoryCode, Description, StorageRoom) 
values ('P20','20 Gallon Paint Products','A105') 
Insert Into SupplyCategory (SupplyCategoryCode, Description, StorageRoom) 
values ('S101','Sanding Supplies','S101') 
Insert Into SupplyCategory (SupplyCategoryCode, Description, StorageRoom) 
values ('B123','Demolition Equipment','DM212') 
Insert Into SupplyCategory (SupplyCategoryCode, Description, StorageRoom) 
values ('CL10','All Cleaning Supplies','B232') 
Insert Into SupplyCategory (SupplyCategoryCode, Description, StorageRoom) 
values ('B100','Building and Construction Materials','B202') 
Insert Into SupplyCategory (SupplyCategoryCode, Description, StorageRoom) 
values ('PR100','Primers','A211') 
Insert Into SupplyCategory (SupplyCategoryCode, Description, StorageRoom) 
values ('W100','Water','W211') 


Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('PR104','Grey Primer','PR100') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('RP5','5 Gallon RedPaint','P5') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('BP10','10 Gallon Blue Paint','P10') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('GP20','20 Gallon Green Paint','P20') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('SP10','Sanding Paper','S101') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('PR105','White Primer','PR100') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('PR106','Black Primer','PR100') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('D100','Large Hammer','B123') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('DS100','Drywall Saw','B123') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('L100','Ladder','B100') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('N100','Nails','B100') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('C100','Soap','CL10') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('S100','Sponge','CL10') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('B100','Wire Brush','CL10') 
Insert Into Supply (SupplyCode,Description, SupplyCategoryCode) 
values ('T100','Towels','CL10') 


Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(1,'BP10',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(1,'PR104',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(2,'PR104',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(2,'SP10',4) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(3,'RP5',1) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(4,'C100',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(4,'S100',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(5,'C100',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(5,'S100',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(6,'RP5',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(7,'DS100',1) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(8,'D100',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(9,'SP10',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(10,'SP10',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(11,'D100',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(11,'N100',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(12,'L100',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(12,'D100',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(12,'N100',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(13,'C100',2) 
Insert into JobSupply (JobNumber,SupplyCode,Quantity) 
values(13,'S100',2) 


--Write the following queries: 
--a. Select the first name, last name (as one column) and phone number from client with client id 4. (1 mark) 
select FirstName + ' ' + LastName, phone from Client 
where ClientID = 4 

--b. For all clients, select the client first name, last name and the number of jobs they have. (3 marks) 
select FirstName + ' ' + LastName, count(ClientID)'job amount' from Client 
inner join Job 
on job.ClientID = Client.ClientID 
group by FirstName + ' ' + LastName 

--c. Select the average CostPerHour of the services.(1 mark) 
Select AVG(CostPerHour) from Service 

--d. Create a list of all the client and staff first names and last names in descending order by last name.(2 marks) 
select FirstName + ' ' + LastName from Staff 
UNION 
select FirstName + ' ' + LastName from Client 
Order by LastName DESC 
--e. Select the supply code and description for all the supplies that have been used on more than 2 jobs.(4 marks) 
select Supply.SupplyCode,Description, count(JobNumber)'Amount of jobs used in' from Supply 
inner join JobSupply 
on Supply.SupplyCode = JobSupply.SupplyCode 
group by Supply.SupplyCode 
having JobNumber >2 

--f. Select jobnumber, date, full address, sub, GST, total, staff name (one column), client name (one column) and phone number for job number 2. (3 marks) 
select JobNumber,Date,Address,SubTotal,GST,Total,FirstName + ' ' + LastName 'Name',Phone from Job 
inner join Staff 
on Job.StaffID = Staff.StaffID 
inner join Client 
on Job.ClientID = Client.ClientID 
group by FirstName + ' ' + LastName 
having JobNumber = 2 

--g. Select all the supply descriptions whose category code starts with “P”.  (2 marks) 
select Description from Supply 
where SupplyCategoryCode like 'P%' 

--h. Select the supply code and description of the supplies that have never been used on a job.(2 marks) 
select Supply.SupplyCode,Description, Count(JobNumber)from Supply 
inner join JobSupply 
on Supply.SupplyCode = JobSupply.SupplyCode 
group by Supply.SupplyCode 
having Count(JobNumber) = 0 

--i. Select the first name, last name of the staff member who has taken the most number of training courses. (4 marks) 
Select FirstName + ' ' + Lastname 'StaffName', Max(TrainingID) from Staff 
inner join StaffTraining 
on staff.StaffID = StaffTraining.StaffID 
group by FirstName + ' ' + Lastname 

--j. Hard work has its rewards! On the last day of each month the staff member who worked on the job with the highest total for that month wins a $25 Tim Hortons gift card! This query will be executed on the last day of each month. Select the first name and the last name (one column) of the staff member who won the gift card for the current month.(4 marks) 
select FirstName + ' ' + LastName 'Staff Name', Max(Total)from Staff 
inner join Job 
on Staff.StaffID = Job.StaffID 
having Max(Total) = Datediff(m,0,(Getdate())) 

--k. How many supplies are there in each supply category? Select the supplycategorycode, description and the count.(3 marks) 
select distinct SupplyCategoryCode, Description, count(*) from Supply 

Update Service 
SET CostPerHour = CostPerHour*1.1 
where Description = 'Painting' 

delete SupplyCategory 
where Description = ' ' 


Select * from JobService 
Select * from Service 
Select * from JobSupply 
Select * from Supply 
Select * from SupplyCategory 
Select * from Job 
Select * from Client 
Select * from StaffTraining 
Select * from Training 
Select * from Staff 
Select * from StaffType 
+1

***其中***在这一切的SQL那个错误发生?您可以创建哪些表格?它在哪里弹出? –

+0

它不会告诉我,但我假设在创建表 –

+0

好吧,经过更详细的调查后,我发现Job表上的DEFAULT约束会产生您在问题中描述的错误。你还应该声明约束的列名称是这样的: '改变表格作业 添加约束DF_Province默认'AB'FOR省' –

回答

1

您是否试图删除代码顶部的表?它不应该是:

降JobService

删除服务

降JobSupply

...

+0

虽然'delete'在语法上是正确的,但它仅删除表中的行,而不是表本身。 –

+0

我认为,但我的教授这样做,所以我离开了它....我应该尝试过......我会想 –

相关问题