Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

SQL Part 4 Practice Questions and Answers

The document outlines the SQL commands to create and manage an Employees table with various constraints, including primary keys, foreign keys, unique constraints, and check constraints. It also details the creation of related tables such as Departments, Projects, Cities, and Bonuses, along with rules to ensure data integrity and specific conditions for employee attributes. Additionally, it includes commands to set default values and restrict certain fields to predefined options.

Uploaded by

Priyanshu Jakhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

SQL Part 4 Practice Questions and Answers

The document outlines the SQL commands to create and manage an Employees table with various constraints, including primary keys, foreign keys, unique constraints, and check constraints. It also details the creation of related tables such as Departments, Projects, Cities, and Bonuses, along with rules to ensure data integrity and specific conditions for employee attributes. Additionally, it includes commands to set default values and restrict certain fields to predefined options.

Uploaded by

Priyanshu Jakhar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Create the Employees table with constraints


CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY, -- PRIMARY KEY constraint
FirstName VARCHAR(50) NOT NULL, -- NOT NULL constraint
LastName VARCHAR(50) NOT NULL, -- NOT NULL constraint
Department VARCHAR(50) NOT NULL, -- NOT NULL constraint
Salary INT CHECK (Salary > 50000 AND Salary < 100000), -- CHECK constraint
HireDate DATE NOT NULL, -- NOT NULL constraint
City VARCHAR(50) DEFAULT 'Unknown' -- DEFAULT constraint
);

2. Insert records while satisfying constraints


INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary,
HireDate, City)
VALUES
(1, 'John', 'Doe', 'Finance', 75000, '2020-01-15', 'New York');

3. Ensure department names are unique


ALTER TABLE Employees
ADD CONSTRAINT Unique_Department UNIQUE (Department);

4. Add a foreign key relationship to a new Departments table


CREATE TABLE Departments (
DepartmentID INT PRIMARY KEY, -- Primary key for Departments table
DepartmentName VARCHAR(50) UNIQUE NOT NULL -- UNIQUE and NOT NULL
constraint
);

ALTER TABLE Employees


ADD DepartmentID INT;

ALTER TABLE Employees


ADD CONSTRAINT FK_Department FOREIGN KEY (DepartmentID) REFERENCES
Departments(DepartmentID);

5. Ensure EmployeeID is unique (redundant with PRIMARY KEY for


demonstration)
ALTER TABLE Employees
ADD CONSTRAINT Unique_EmployeeID UNIQUE (EmployeeID);

6. Add a check constraint on HireDate


ALTER TABLE Employees
ADD CONSTRAINT Check_HireDate CHECK (HireDate >= '2000-01-01');

7. Default city for employees not explicitly assigned


ALTER TABLE Employees
ALTER COLUMN City SET DEFAULT 'Unknown';

8. Ensure salary is a positive number


ALTER TABLE Employees
ADD CONSTRAINT Check_Salary_Positive CHECK (Salary > 0);

ALTER TABLE Employees


ADD CONSTRAINT Unique_FullName UNIQUE (FirstName, LastName);

10. Restrict employees to specific cities only


ALTER TABLE Employees
ADD CONSTRAINT Check_City CHECK (City IN ('New York', 'San Francisco', 'Los
Angeles', 'Chicago', 'Houston', 'Seattle', 'Phoenix', 'Boston', 'Denver',
'Austin', 'Dallas', 'San Diego'));

11. Create an index on Department for faster queries


CREATE UNIQUE INDEX idx_Department ON Employees(Department);

12. Create a table for Projects with a foreign key to Employees


CREATE TABLE Projects (
ProjectID INT PRIMARY KEY,
ProjectName VARCHAR(100) NOT NULL,
EmployeeID INT,
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID)
);

13. Add a default salary if not provided


ALTER TABLE Employees
ALTER COLUMN Salary SET DEFAULT 60000;

14. Ensure names are not empty strings


ALTER TABLE Employees
ADD CONSTRAINT Check_Name_Not_Empty CHECK (FirstName <> '' AND LastName <>
'');

15. Add a foreign key to a Cities table


CREATE TABLE Cities (
CityID INT PRIMARY KEY,
CityName VARCHAR(50) UNIQUE NOT NULL
);

ALTER TABLE Employees


ADD CityID INT;

ALTER TABLE Employees


ADD CONSTRAINT FK_City FOREIGN KEY (CityID) REFERENCES Cities(CityID);

16. Set a default hire date


ALTER TABLE Employees
ALTER COLUMN HireDate SET DEFAULT GETDATE(); -- For SQL Server
-- Use CURRENT_DATE for PostgreSQL or MySQL

17. Restrict department names to specific values


ALTER TABLE Departments
ADD CONSTRAINT Check_DepartmentName CHECK (DepartmentName IN ('Finance', 'IT',
'HR', 'Marketing'));

18. Ensure no duplicate combinations of EmployeeID and CityID


ALTER TABLE Employees
ADD CONSTRAINT Unique_Employee_City UNIQUE (EmployeeID, CityID);

19. Create a Bonuses table with constraints and foreign keys


CREATE TABLE Bonuses (
BonusID INT PRIMARY KEY,
EmployeeID INT NOT NULL,
BonusAmount INT CHECK (BonusAmount >= 0),
FOREIGN KEY (EmployeeID) REFERENCES Employees(EmployeeID)
);

20. Ensure all employees in IT have a salary above 80,000


ALTER TABLE Employees
ADD CONSTRAINT Check_IT_Salary CHECK (Department <> 'IT' OR Salary >= 80000);

You might also like