You are on page 1of 18

Bug Catcher 1 CREATE TABLE Sales.

TravelTrip ( TripID int NOT NULL, Complaint bit NOT NULL, CONSTRAINT PK_TravelTrip_TripID PRIMARY KEY CLUSTERED TripID (ASC) )

Answer: The specified Column List for the Primary Key must be inside the parentheses.
CONSTRAINT PK_TravelTrip_TripID PRIMARY KEY CLUSTERED(TripID ASC)

Bug Catcher 2 CREATE TABLE Sales.TravelTrip ( TripID int PRIMARY KEY CLUSTERED, TripName varchar(max) CONSTRAINT UC_TripName UNIQUE, Complaint bit NOT NULL)

Answer: Datatypes over 900 bytes can not be Unique Constraints.


CREATE TABLE Sales.TravelTrip ( TripID int PRIMARY KEY CLUSTERED, TripName varchar(100) CONSTRAINT UC_TripName UNIQUE, Complaint bit NOT NULL)

Bug Catcher 3 CREATE TABLE Sales.TravelTrip ( TripID int NOT NULL, Complaint bit NOT NULL, CONSTRAINT PK_TravelTrip_TripID PRIMARY KEY CLUSTERED )

Answer: You need to specify Column List for the Primary Key.

CONSTRAINT PK_TravelTrip_TripID PRIMARY KEY CLUSTERED(TripID)

Bug Catcher 4

CONSTRAINT pk_Cust_CustID PRIMARY KEY (CustID), CONSTRAINT ck_Cust_Gender (Gender='M' OR Gender='F')

Answer: Check constraint needs CHECK keyword.

CONSTRAINT pk_Cust_CustID PRIMARY KEY (CustID), CONSTRAINT ck_Cust_Gender CHECK (Gender='M' OR Gender='F')

Bug Catcher 5

CONSTRAINT pk_Cust_CustID PRIMARY KEY (CustID), CONSTRAINT ck_Cust_Gender CHECK (Gender= M OR Gender= F )

Answer: Need single quotes, should be 'M' OR 'F'.

CONSTRAINT pk_Cust_CustID PRIMARY KEY (CustID), CONSTRAINT ck_Cust_Gender CHECK (Gender='M' OR Gender='F')

Bug Catcher 6 CREATE TABLE Sales.TravelTrip ( TripID int NOT NULL, StartDate datetime NOT NULL, EndDate datetime NOT NULL, CONSTRAINT PK_TravelTrip_TripID PRIMARY KEY CLUSTERED (TripID) ), CONSTRAINT ck_TravelTrip_EndDate CHECK (EndDate>=StartDate) GO Answer: Put both constraints

inside CREATE TABLE parens.


CONSTRAINT PK_TravelTrip_TripID PRIMARY KEY CLUSTERED (TripID), CONSTRAINT ck_TravelTrip_EndDate CHECK (EndDate>=StartDate) )

Bug Catcher 7 CREATE TABLE Sales.TravelTrip ( TripID int NOT NULL, StartDate datetime NOT NULL, EndDate datetime NOT NULL, Complaint bit NOT NULL DEFAULT, CONSTRAINT PK_TravelTrip_TripID PRIMARY KEY CLUSTERED (TripID) , CONSTRAINT ck_TravelTrip_EndDate CHECK (EndDate>=StartDate) ) GO

Answer: DEFAULT requires a default value.


Complaint bit NOT NULL DEFAULT (0),

Bug Catcher 8 CREATE TABLE Sales.TravelTrip ( TripID int NOT NULL, StartDate datetime NOT NULL, EndDate datetime NOT NULL, Complaint bit NOT NULL DEFAULT(0), CONSTRAINT PK_TravelTrip_TripID PRIMARY KEY CLUSTERED (TripID) , CONSTRAINT ck_TravelTrip_EndDate CHECK (EndDate>+StartDate) ) GO

Answer: Should be >=


CHECK (EndDate>=StartDate)

Bug Catcher 9 CREATE TABLE Sales.TravelTrip ( TripID int NOT NULL, StartDate datetime NOT NULL, EndDate datetime NOT NULL, Complaint bit NOT NULL, DEFAULT(0), CONSTRAINT PK_TravelTrip_TripID PRIMARY KEY CLUSTERED (TripID) , CONSTRAINT ck_TravelTrip_EndDate CHECK (EndDate>=StartDate) ) GO

Answer: When using a constraint inline with a column you dont need a comma.

Complaint bit NOT NULL DEFAULT (0),

CREATE TABLE Sales.Travel Bug Catcher 10 (TripID int NOT NULL, CustomerID int NOT NULL, LocationID int NOT NULL, StartDate datetime NOT NULL, EndDate datetime NOT NULL, Rating tinyint NOT NULL, Complaint bit NOT NULL DEFAULT (0), CONSTRAINT PK_Travel_TripID PRIMARY KEY CLUSTERED (TripID ASC), CONSTRAINT ck_Travel_EndDate CHECK (EndDate>StartDate), CONSTRAINT ck_Travel_Rating CHECK (Rating<=100) CONSTRAINT ck_Travel_Complaint CHECK ((Complaint=0) OR (Complaint=1 AND Rating <=90)) )

Answer: All constraints need to be separated by commas.


CONSTRAINT ck_Travel_Rating CHECK (Rating<=100),

--Logical Bug

Bug Catcher 11

CONSTRAINT CK_TravelTrip_EndDate CHECK (EndDate<StartDate)

Answer: EndDate should be greater then StartDate..

CONSTRAINT CK_TravelTrip_EndDate CHECK (EndDate>StartDate)

Bug Catcher 12

ALTER TABLE dbo.Employee ADD CommisionRate null

Answer: To add a new field you must specify the data type

ALTER TABLE dbo.Employee ADD CommisionRate decimal null

ALTER TABLE dbo.Employee ADD CONSTRAINT DEFAULT 12 FOR CommissionRate WITH VALUES

Bug Catcher 13

Answer: Constraint should have a name.


ALTER TABLE dbo.Employee ADD CONSTRAINT df_Employee_CommissionRate DEFAULT 12 FOR CommissionRate WITH VALUES

ALTER TABLE dbo.Employee ADD CONSTRAINT df_Employee_CommissionRate DEFAULT 12 FOR CommissionRate FOR VALUES

Bug Catcher 14

Answer: Should be WITH VALUES

ALTER TABLE dbo.Employee ADD CONSTRAINT df_Employee_CommissionRate DEFAULT 12 FOR CommissionRate WITH VALUES

ALTER TABLE dbo.Employee ADD CONSTRAINT fk_Employee_Location_LocationID FOREIGN KEY (LocationID) REFERENCES (Location.LocationID)

Bug Catcher 15

Answer: You need to reference the table and column name in parens.
ALTER TABLE dbo.Employee ADD CONSTRAINT fk_Employee_Location_LocationID FOREIGN KEY (LocationID) REFERENCES Location(LocationID)

ALTER TABLE dbo.Employee ADD CONSTRAINT fk_Employee_Location_LocationID FOREIGN KEY (LocationID) REFERENCES Employee(LocationID)

Bug Catcher 16

Answer: A Foreign key should reference another table.


ALTER TABLE dbo.Employee ADD CONSTRAINT fk_Employee_Location_LocationID FOREIGN KEY (LocationID) REFERENCES Location(LocationID)

Bug Catcher 17 CREATE TABLE Sales.TravelTrip ( TripID int NOT NULL, StartDate datetime NOT NULL, EndDate datetime NOT NULL, Complaint bit NOT NULL DEFAULT(0), CONSTRAINT PK_TravelTrip_TripID PRIMARY KEY CLUSTERED (TripID) , CONSTRAINT ck_TravelTrip_EndDate CHECK (EndDate=>StartDate) ) GO

Answer: Should be >= not =>


CHECK (EndDate>=StartDate)

--Note: SSN is PRIMARY KEY --Note: (IGNORE_DUP_KEY = OFF) INSERT INTO [EmployeeHistory] SELECT * FROM Employee GO

Bug Catcher 18

INSERT INTO dbo.Employee VALUES ('369-152429','Alex','Smith','AlexS@YGIM.COM',0) GO INSERT INTO [EmployeeHistory] SELECT * FROM Employee

Answer: Trying to insert all rows again will result in duplicate records on your primary key.

You might also like