SQL Lab File (1-10) - Tushar-21scse2030128
SQL Lab File (1-10) - Tushar-21scse2030128
EXPERIMENT 1:
It allows the data professionals and users to retrieve the data from the
relational database management systems.
It also helps in creating the view, stored procedure, and functions in the
relational database.
Database
A database is a systematic collection of data.
They support electronic storage and manipulation of data.
Databases make data management easy.
Table
Table is a collection of data, organized in terms of rows and columns.
Record
A record is a data structure that can hold data items of diff erent kinds. Records
DDL changes the structure of the table like creating a table, deleting a table,
altering a table, etc.
All the command of DDL are auto-committed that means it permanently save
all the changes in the database.
o CREATE
o ALTER
o DROP
Syntax:-
CREATE TABLE TABLE_NAME (COLUMN_NAME DATATYPES[,....]);
b. DROP: It is used to delete both the structure and record stored in the table.
Syntax:-
DROP TABLE table_name;
c. ALTER: It is used to alter the structure of the database. This change could be either to
modify the characteristics of an existing attribute or probably to add a new attribute.
Syntax:-
ALTER TABLE table_name ADD column_name COLUMN-definition;
o SELECT
o INSERT
o UPDATE
o DELETE
Syntax:-
Syntax:-
INSERT INTO TABLE_NAME VALUES (value1, value2, value3, .... valueN);
Syntax:-
UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHERE CON
DITION]
d. DELETE: It deletes the records in the table and even delete the
complete table.
Syntax:-
DELETE FROM table_name [WHERE condition];
Q1. Creation of a database and writing SQL queries to retrieve information from the
database.
Roll int,
Name varchar(30),
Address varchar(30),
City varchar(30));
ADD surname;
UPDATE students
UPDATE students
UPDATE students
UPDATE students
EXPERIMENT 2(a):
1. SELECT DISTINCT:- It is used to return only distinct (different) values.
Syntax:-
Syntax:-
Syntax:-
Syntax:-
SELECT column1, column2,..FROM table_name WHERE condition1 OR
u condition2 OR condition3.. ;
Syntax:-
Syntax:-
SELECT MIN(column_name)FROM table_name WHERE condition;
Syntax:-
Syntax:-
SELECT COUNT(column_name)FROM table_name WHERE condition;
Syntax:-
Syntax:-
OUTPUT:-
Date: 18th November 2021
EXPERIMENT 2(b):
SQL Constraints
SQL constraints are used to specify rules for the
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.
The following constraints are commonly used in
SQL:
Example:-
CREATE TABLE student (
ID int NOT NULL,
FirstName varchar(80) NOT NULL,
LastName varchar(80) NOT NULL,
Age int );
SQL UNIQUE Constraint
The UNIQUE constraint in SQL is used to ensure that no duplicate values
will be inserted into a specific column or combination of columns that
are participating in the UNIQUE constraint and not part of the PRIMARY
KEY.
Example:-
create table person(Id int NOT NULL, FirstName varchar(124),
LastName varchar(124), Age int, UNIQUE(Id));
Example:-
CREATE TABLE Peoples (
PersonID int NOT NULL,
PersonName varchar(43) NOT NULL,
Age int,
PRIMARY KEY(PersonID));
Example:-
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY(OrderID),
Example:-
CREATE TABLE Children (
ID int NOT NULL,
FirstName varchar(54),
LastName varchar(54) NOT NULL,
Age int CHECK (Age>=18));
Example:-
CREATE TABLE Women (
ID int NOT NULL,
LastName varchar(54) NOT NULL,
FirstName varchar(65),
Age int,
City varchar(54) DEFAULT 'Rajgir');
Example:-
EXPERIMENT -3:
a.) Personal
b.) Official
---------------------------------------------------------------
a.) Personal:-
Table Created:-
Describe Table:-
Insert Table:-
Output:-
b.) Official:-
Table Created:-
Describe Table:-
Insert Table:-
Output:-
Date: 9thDecember 2021
EXPERIMENT -4
Q. Creating of:-
1. Views
2. Sequences
3. Savepoint
1.Views
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.
You can add SQL statements and functions to a view and present the
data as if the data were coming from one single table.
A view is created with the CREATE VIEW statement.
Examples:-
OUTPUT:-
2.Sequences
Sequence is a set of integers 1, 2, 3, … that are generated and
supported by some database systems to produce unique values on
demand.
Syntax
CREATE SEQUENCE sequence_name
START WITH initial_value
INCREMENT BY increment_value
MINVALUE minimum value
MAXVALUE maximum value
CYCLE|NOCYCLE ;
Where:-
sequence_name: Name of the sequence.
Examples:-
OUTPUT:-
TRANSACTIONS
Transaction Control
The following commands are used to control transactions.
COMMIT − to save the changes.
ROLLBACK − to roll back the changes.
SAVEPOINT − creates points within the groups of transactions in which to
ROLLBACK.
SET TRANSACTION − Places a name on a transaction.
Savepoint
Savepoint is a command in SQL that is used with the rollback
command.
It is a command in Transaction Control Language that is used to
mark the transaction in a table.
Savepoint is helpful when we want to roll back only a small part of
a table and not the whole table. In simple words, we can say
savepoint is a bookmark in SQL.
If you made a transaction in a table, you could mark the
transaction as a certain name, and later on, if you want to roll back
to that point, you can do it easily by using the transaction's name.
Syntax
SAVEPOINT SAVEPOINT_NAME;
Examples:-
ROLLBACK
The ROLLBACK command is the transactional command used to undo
transactions that have not already been saved to the database.
This command can only be used to undo transactions since the last COMMIT
or ROLLBACK command was issued.
Syntax
ROLLBACK TO SAVEPOINT_NAME;
Examples:-
SQL JOIN
A JOIN clause is used to combine rows from two or more tables, based on a
related column between them.
(INNER) JOIN: Returns records that have matching values in both tables
LEFT (OUTER) JOIN: Returns all records from the left table, and the
matched records from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and the
matched records from the left table
FULL (OUTER) JOIN: Returns all records when there is a match in either
left or right table
EXPERIMENT -6
PL/SQL
Print Hello with PL/SQL
1. set serveroutput on
2. SQL> declare
3. a varchar2(20):='Hello World';
4. begin
5. dbms_output.put_line(a);
6. end;
7. /
Hello World
Print Welcome with PL/SQL
1. SQL> declare
2. b varchar2(20):='Welcome';
3. begin
4. dbms_output.put_line(b);
5. end;
6. /
Welcome
declare
2 x integer:=10;
3 y integer:=20;
4 c integer;
5 f number(4,2);
6 begin
7 c:=x+y;
8 dbms_output.put_line('Value of c:'||c);
9 f:=25.0/3.0;
10 dbms_output.put_line('Value of f:'||f);
11 end;
12 /
Value of c:30
Value of f:8.33
SQL> declare
2 --constant declaration
3 pi constant number:=3.141592654;
4 --other declaration
5 radius number(5,2);
6 dia number(5,2);
7 circumference number(9,2);
8 area number(12,2);
9 begin
10 --processing
11 radius:=8.5;
12 dia:=radius*2;
13 circumference:=2.0*pi*radius;
14 area:=pi*radius*radius;
15 --output
16 dbms_output.put_line('Radius:'||radius);
17 dbms_output.put_line('Diameter:'||dia);
18 dbms_output.put_line('Circumference:'||circumference);
19 dbms_output.put_line('Area:'||area);
20 end;
21 /
Radius:8.5
Diameter:17
Circumference:53.41
Area:226.98
Date: 12thJanuary 2022
EXPERIMENT -7
Q. Write PL/SQL block to satisfy some condition by
accepting input from the user?
1. Use the select into statement of SQL to assign values to PL/SQL variables.
2. For each item in the select list, there must be a corresponding, type – compatible variable
in the into list
Program
Table Created:-
Table:-
Program Executed:-
Date: 19thJanuary 2022
EXPERIMENT -8
Algorithm:
Step 1: Start
Step 2: Declare variables a, b and c
Step 3: Declare two exceptions as exp1 and exp2
Step 4: Perform the operations using If else
Step 5: Print the output according to the exception
Step 6: Stop.
Executed Program:
1.
2.
3.
Algorithm:
Step 1: Start
Step 2: Create table customer8 with variables c_id, c_name,
age, address, and salary
Step 3: Insert the values into customer8 table
Step 4: Display the customer8 table using select command
Step 5: Create trigger
Step 6: Declare variable
Step 7: Store the difference of new salary and old salary of
customers in variable
Step 8: Print the old salary, new salary and salary difference
Step 9: Insert the new value into customer5 table
Step 10: Update the salary of the customer in the table
Step 11: Display the customer8 table again using select
command
Step 12: Stop.
Executed Program:
Result: Using Trigger the salary difference of the employee has
been successfully calculated.
Date: 27thJanuary 2022
EXPERIMENT -10(b)
1.
2.
THANK YOU