0% found this document useful (0 votes)
2 views

LAB Experiment-2

The document outlines various integrity constraints in SQL, including NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DROP constraints, which ensure data accuracy and consistency in relational databases. It also describes built-in SQL functions for string manipulation and data retrieval, along with practical lab exercises for creating and managing database tables. Additionally, it provides a university database schema and exercises for querying and modifying data.

Uploaded by

brandyadav696969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

LAB Experiment-2

The document outlines various integrity constraints in SQL, including NOT NULL, DEFAULT, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, and DROP constraints, which ensure data accuracy and consistency in relational databases. It also describes built-in SQL functions for string manipulation and data retrieval, along with practical lab exercises for creating and managing database tables. Additionally, it provides a university database schema and exercises for querying and modifying data.

Uploaded by

brandyadav696969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Experiment-2

INTEGRITY CONSTRAINTS IN SQL

Wednesday, January 15, 2025 Dr. Deepti Sisodia


Integrity constraints in SQL are rules that ensure the accuracy and reliability of the data in a
relational database. These constraints enforce data integrity and maintain consistency by limiting
the types of data that can be stored in a table. The main types of integrity constraints in SQL are:

1. NOT NULL: Ensures that a column cannot have a NULL value. This constraint ensures that a
field always contains a valid, non-missing value.

Example:

Wednesday, January 15, 2025 Dr. Deepti Sisodia


2. DEFAULT Constraint: The DEFAULT constraint provides a default value for a column when none is
specified during record insertion. This helps set a value automatically if no value is given.

Wednesday, January 15, 2025 Dr. Deepti Sisodia


3. UNIQUE Constraint:
The UNIQUE constraint ensures that all values in a column are different. Both
the UNIQUE and PRIMARY KEY constraints provide a guarantee for uniqueness for a
column or set of columns. A PRIMARY KEY constraint automatically has
a UNIQUE constraint. However, you can have many UNIQUE constraints per table, but
only one PRIMARY KEY constraint per table.

Wednesday, January 15, 2025 Dr. Deepti Sisodia


4. PRIMARY KEY:
The PRIMARY KEY constraint uniquely identifies each record in a table. Primary keys must
contain UNIQUE values, and cannot contain NULL values. A table can have only ONE
primary key; and in the table, this primary key can consist of single or multiple columns
(fields).

Wednesday, January 15, 2025 Dr. Deepti Sisodia


5. FOREIGN KEY:
The FOREIGN KEY constraint is used to prevent actions that would destroy links
between tables. A FOREIGN KEY is a field (or collection of fields) in one table, that
refers to the PRIMARY KEY in another table. The table with the foreign key is called the
child table, and the table with the primary key is called the referenced or parent table.

Wednesday, January 15, 2025 Dr. Deepti Sisodia


6. CHECK Constraint:
The CHECK constraint ensures that all values in a column satisfy a specific condition or rule. It is used
to validate data based on a given condition.

Wednesday, January 15, 2025 Dr. Deepti Sisodia


7. DROP Constraint:
Any constraint that you have defined can be dropped using the ALTER TABLE command with the DROP
CONSTRAINT option.

Wednesday, January 15, 2025 Dr. Deepti Sisodia


Dropping the CHECK Constraints

1. Drop the Category_Check Constraint


We’ll use ALTER TABLE to remove the Category_Check constraint:

Wednesday, January 15, 2025 Dr. Deepti Sisodia


Built-in Functions
1. LENGTH(string)
•Description: Returns the number of characters in the specified string, including spaces.
•Syntax: LENGTH(string1)
•Example:

2. LOWER(string)
•Description: Converts all letters in the string to lowercase. Non-letter characters remain unaffected.
•Syntax: LOWER(string)
•Example:

Wednesday, January 15, 2025 Dr. Deepti Sisodia


3. SUBSTR(string, start, count)
•Description: Extracts a substring from a string.
•start_position: Starting position (1-based index).
•length (optional): Number of characters to extract.
•Syntax: SUBSTR(string, start_position [, length])
•Example

4. UPPER(string)
•Description: Converts all letters in the string to uppercase. Non-letter characters are unaffected.
•Syntax: UPPER(string)
•Example

Wednesday, January 15, 2025 Dr. Deepti Sisodia


5. NVL(column, substitute_value)
•Description: Substitutes a given value when a NULL value is encountered.
•Syntax: NVL(column_name, substitute_value)
•Example

6. ROUND(value, precision)
•Description: Rounds a number to a specified number of decimal places.
•Syntax: ROUND(number, decimal_places)
•Example

Wednesday, January 15, 2025 Dr. Deepti Sisodia


7. TO_CHAR(value, format)
•Description: Converts a number or date to a formatted string.
•Syntax: TO_CHAR(value, format)
•Example:

8. LAST_DAY(date)
•Description: Returns the last day of the month for the given date.
•Syntax: LAST_DAY(date)
•Example

Wednesday, January 15, 2025 Dr. Deepti Sisodia


9. MONTHS_BETWEEN(date1, date2)
•Description: Returns the number of months between two dates.
•Syntax: MONTHS_BETWEEN(date1, date2)
•Example

10. NEXT_DAY(date1, 'weekday')


•Description: Returns the first weekday that occurs after the given date.
•Syntax: NEXT_DAY(date, 'weekday')
•Example

Wednesday, January 15, 2025 Dr. Deepti Sisodia


11. TO_DATE(string, 'format')
•Description: Converts a string to a date in the specified format.
•Syntax: TO_DATE(string, format)
•Example

Wednesday, January 15, 2025 Dr. Deepti Sisodia


LAB EXERCISES:
Consider the following schema:
Employee (EmpNo, EmpName, Gender, Salary, Address, DNo)
Department (DeptNo, DeptName, Location)
1. Create Employee table with following constraints:
Make EmpNo as Primary key.
Do not allow EmpName, Gender, Salary and Address to have null values.
Allow Gender to have one of the two values: ‘M’, ‘F’.

2. Create Department table with following:


Make DeptNo as Primary key
Make DeptName as candidate key

3. Make DNo of Employee as foreign key which refers to DeptNo of Department.


4. Insert few tuples into Employee and Department which satisfies the above constraints.
5. Try to insert few tuples into Employee and Department which violates some of the above constraints.
6. Try to modify/delete a tuple which violates a constraint.
(Ex: Drop a department tuple which has one or more employees)
7. Modify the foreign key constraint of Employee table such that whenever a department tuple is deleted, the employees
belonging to that department will also be deleted.
Naming Constraints:
8. Create a named constraint to set the default salary to 10000 and test the constraint by inserting a new record.
Wednesday, January 15, 2025 Dr. Deepti Sisodia
University Database Schema:

Student (ID, name,dept-name, tot-cred)


Instructor(ID, name, dept-name, salary)
Course (Course-id, title,dept-name, credits)
Takes (ID, course-id, sec-id, semester, year, grade)
Classroom (building, room-number, capacity)
Department (dept-name, building, budget)
Section (course-id, section-id, semester, year, building, room-number, time-slot-id)
Teaches (id, course-id, section-id, semester, year)
Advisor(s-id, i-id)
Time-slot (time-slot-id, day, start-time, end-time)
Prereq (course-id, prereq-id)

(Use University database for the exercise problems given below)

Wednesday, January 15, 2025 Dr. Deepti Sisodia


Retrieving records from a table:
9. List all Students with names and their department names.
10. List all instructors in CSE department.
11. Find the names of courses in CSE department which have 3 credits.
12. For the student with ID 12345 (or any other value), show all course-id and title of all courses registered for by
the student.
13. List all the instructors whose salary is in between 40000 and 90000.
Retrieving records from multiple tables:
14. Display the IDs of all instructors who have never taught a course.
15. Find the student names, course names, and the year, for all students those who have attended classes in room-
number 303.

Rename and Tuple Variables (Use as in select and from):


16. For all students who have opted courses in 2015, find their names and course id’s with the attribute course title
replaced by c-name.
17. Find the names of all instructors whose salary is greater than the salary of at least one instructor of CSE
department and salary replaced by inst-salary.

String Operations (Use %, _, LIKE):


18. Find the names of all instructors whose department name includes the substring ‘ch’.

Wednesday, January 15, 2025 Dr. Deepti Sisodia


Built-in Functions:
19. List the student names along with the length of the student names.
20. List the department names and 3 characters from 3rd position of each department name
21. List the instructor names in upper case.
22. Replace NULL with value1(say 0) for a column in any of the table
23. Display the salary and salary/3 rounded to nearest hundred from Instructor.

Add date of birth column (DOB) to Employee Table. Insert appropriate DOB values for different
employees and try the exercise problems given below:
24. Display the birth date of all the employees in the following format:
‘DD-MON-YYYY’
‘DD-MON-YY’
‘DD-MM-YY’
25. List the employee names and the year (fully spelled out) in which they are born
‘YEAR’
‘Year’
‘year’

Wednesday, January 15, 2025 Dr. Deepti Sisodia


ADDITIONAL EXERCISE:

1. Modify the employee table to check the salary of every employee to be greater than 5000.
2. Find the quarter of year from the given date.
3. Convert seconds to hours, minutes and seconds format.
4. Find the week of the year from the given date.
5. Find the names of all departments with instructor, and remove duplicates.
6. For all instructors who have taught some course, find their names and the course ID of the courses they
taught.
7. Find all the instructors with the courses they taught.
8. List all the students with student name, department name, advisor name and the number of courses
registered.

Wednesday, January 15, 2025 Dr. Deepti Sisodia

You might also like