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

Experiment No. 4 - DBMS Lab Final

The document describes an experiment involving nested queries, constraints, and views in SQL. It provides examples of nested queries, different SQL constraints like NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, and INDEX. It also describes how to create views in SQL and provides an example view query.

Uploaded by

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

Experiment No. 4 - DBMS Lab Final

The document describes an experiment involving nested queries, constraints, and views in SQL. It provides examples of nested queries, different SQL constraints like NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT, and INDEX. It also describes how to create views in SQL and provides an example view query.

Uploaded by

grzmpgqn4f
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

EXPERIMENT NO: 4

Environment: Microsoft Windows


Tools/ Language: MySql/Oracle

Objective: Nested queries, constraints and view

Theory & Concepts:

a. Nested Query
A nested query is a select query that is nested inside a SELECT,UPDATE,INSERT, or
DELETE SQL query.

Example:
SELECT Model FROM Product WHERE ManufacturerID IN (SELECT ManufacturerID FROM
Manufacturer WHERE Manufacturer = 'Dell')

b. SQL Constraints

SQL constraints are used to specify rules for data in a table Constraints are used to limit the
type of data that can go into a table. This ensures the accuracy and reliability of the data in
the table. If there is any violation between the constraint and the data action, the action is
aborted.

Constraints can be column level or table level. Column level constraints apply to a column,
and table level constraints apply to the whole table.

The following constraints are commonly used in SQL:

1. NOT NULL - Ensures that a column cannot have a NULL value


2. UNIQUE - Ensures that all values in a column are different

3. PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely


identifies each row in a table

4. FOREIGN KEY - Uniquely identifies a row/record in another table

5. CHECK - Ensures that all values in a column satisfies a specific condition

6. DEFAULT - Sets a default value for a column when no value is specified

7. INDEX - Used to create and retrieve data from the database very quickly

SQL NOT NULL Constraint

By default, a column can hold NULL values. The NOT NULL constraint enforces a column
to NOT accept NULL values.
Example:

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255) NOT NULL,
Age int );

SQL 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.

Example-

CREATE TABLE Persons (


ID int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int );

SQL PRIMARY KEY Constraint

The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary
keys must contain UNIQUE values, and cannot contain NULL values.

Example-

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID) );

SQL FOREIGN KEY Constraint

A FOREIGN KEY is a key used to link two tables together. A FOREIGN KEY is a field (or
collection of fields) in one table that refers to the PRIMARY KEY in another table.

Example-

CREATE TABLE Orders (


OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID) );

SQL CHECK Constraint

The CHECK constraint is used to limit the value range that can be placed in a column. If you
define a CHECK constraint on a single column it allows only certain values for this column.

Example-

CREATE TABLE Persons (


ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18) );

SQL DEFAULT Constraint


The DEFAULT constraint is used to provide a default value for a column. The default value
will be added to all new records IF no other value is specified.

Example-
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
City varchar(255) DEFAULT 'Sandnes' );

b) SQL Views

In SQL, a view is a virtual table based on the result-set of an SQL statement. A view contains
rows and columns, just like a real table. The fields in a view are fields from one or more real
tables in the database.

Example-

CREATE VIEW view_name AS


SELECT column1, column2, ...
FROM table_name
WHERE condition;
Practical Assignment 1

SQL QUERIES:

1. Write syntax for creating the following tables:


STUDENT (ROLL_NO, NAME, BRANCH, YEAR, SECTION, F_NAME, ADDRESS)
BOOK (BOOK_ID, TITLE, AUTHOR, PUBLISHER, COST, COPIES)
TRANSACTION (ROLL_NO, BOOK_ID, DATE_ISSUE, DATE_RETURN, FINE)
Add Primary Key and Foreign Key to appropriate attributes in the above mentioned table.
Add a constraint on Book table for accepting value in copies attributes not less than 0.

Creation of tables.
Student Table.

Book Table

Transaction Table.
2. Write SQL command to add a new field DOB in the STUDENT table.

3. Write SQL command to drop Fine column from TRANSACTION table.


4. Insert at least 5 records in each table.

Insertion in Student..

Insertion in book..
Insertion in Transaction..

5. Update title of books from ‘DATABASE MANAGEMENT SYSTEMS’ to ‘DBMS’.


6. Delete all entries from the Hostel column of STUDENT table.

7. Write SQL Queries for the following

a). To display the details of all students.


b). Display the records of (a) in ascending order of Roll_no .

c). To display all the ‘Networking Books’ in the library.


d). To display those books which have ‘Computer’ word in their title (like Computer
Fundamentals, Computer Graphics etc.) .

e). To display all the books which have been issued so far.
f). To display all the books which have not been issued so far.

g). To display all the students of 4th year CSE branch who are staying in the
hostel.
h). To display the student’s names who have not returned the books issued to them.

i). To display the students name who have been issued DBMS book by Korth.
j). To display the students who have not issued any book so far.

k). To display the students who have been issued at least one book.
l). To display the title of the second costliest book.

m). To display the students who have not returned the book more than 6
weeks.
o). To display the students of 4th year from CSE branch who have been issued Graphics
book(s) for more than 15 days , which have not been returned.

You might also like