RDBMS 2
RDBMS 2
RDBMS 2
of
RDBMS
SubmittedTo: SubmittedBy:
Mrs.Priya Robin
Dept.ofCSE BCA 2ndyear
RollNo:22001041047
Signature:
DeenbandhuChhotuRamUniversityofScienceandTechnologyMurthal,Sonipat-INDIA, Haryana
State Government University
EXPERIMENT NO:1
String Datatypes:
SQL character/string datatypes are used to store alpha numeric characters in a database. The
most commonly used of these are CHAR and VARCHAR .
2. Alter: The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
3. Drop: The DROP TABLE statement is used to drop an existing table in a database.
Example: Drop Table lib;
OUTPUT:
4. Truncate: The TRUNCATE TABLE statement is used to delete the data inside a table, but not the
table itself.
Example: Truncate Table lib;
Output:
EXPERIMENT NO:3
2. Insert: The SQL INSERT statement inserts one or more rows of data into a table. You can also see
INSERT written as INSERT INTO, but both behave the same. The inclusion of INTO is optional in
most variants of SQL. The INSERT statement is considered a SQL data manipulation command.
Output:
4. Update: An SQL UPDATE statement changes the data of one or more records in a table. Either all
the rows can be updated, or a subset may be chosen using a condition.
Output:
EXPERIMENT NO:4
Output:
1. NOT NULL-
Example- create table lib
(Book_name varchar(100) Not Null,
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date);
Desc lib;
Output:
2. Unique-
3. PRIMARY KEY-
Example-
Create table lib
(Book_name varchar(100) Not Null,
Issued_to char(30) Unique,
Issuing_authority Char(30),
Date_of_issue date,
Student_ID Int Primary Key,
);
Desc lib;
5. CHECK
Example- Condition 1: All conditions are true
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18));
insert into lib values('Morris Mano', 'Deepak', 'University', '2024-01-25',30),
('Wing on Fire','Sweety', 'University', '2024-01-26',23),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27',34),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27',27),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44);
Output-
Example- Condition 2: All conditions are true
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18));
insert into lib values('Morris Mano', 'Deepak', 'University', '2024-01-25',30),
('Wing on Fire','Sweety', 'University', '2024-01-26',23),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27',34),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27',17),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44);
Output –
6. Default –
Example –
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Trans');
insert into lib values('Morris Mano', 'Deepak', 'University', '2024-01-25',30, default),
('Wing on Fire','Sweety', 'University', '2024-01-26',23, default),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27',34, default),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male'),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, default);
select * from lib;
Output:
7. Create Index-
Syntax –
CREATE INDEX index_name
ON table_name (column1, column2, ...);
Example –
EXPERIMENT NO:5
Syntax:
COUNT(*)
or
COUNT( [ALL|DISTINCT] expression )
Example:
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Trans');
insert into lib values('Morris Mano', 'Deepak', 'University', '2024-01-25',30, default),
('Wing on Fire', 'Sweety', 'University', '2024-01-26',23, default),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27',34, default),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male'),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male'),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male'),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, default);
select count(Book_name)
from lib;
Output:
The GROUP BY statement groups rows that have the same values into summary rows, like "find
the number of customers in each country".
The GROUP BY statement is often used with aggregate functions
(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by one or more columns.
GROUP BY Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
Example:
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Trans',
Price int);
Output:
2. Sum()-The SUM() function returns the total sum of a numeric column.
Syntax:
SUM()
or
SUM( [ALL|DISTINCT] expression )
Example:
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Trans',
Price int);
insert into lib values('Morris Mano', 'Deepak', 'University', '2024-01-25',30, default, 120),
('Wing on Fire', 'Sweety', 'University', '2024-01-26',23, default, 400),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27',34, default, 211),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 320),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 149),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 200),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, default, 80);
select sum(Price)
from lib;
Output:
Syntax:
AVG()
or
AVG( [ALL|DISTINCT] expression )
Example:
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Trans',
Price int);
insert into lib values('Morris Mano', 'Deepak', 'University', '2024-01-25',30, default, 120),
('Wing on Fire', 'Sweety', 'University', '2024-01-26',23, default, 400),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27',34, default, 211),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 320),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 149),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 200),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, default, 80);
select avg(Price)
from lib;
Output:
4. Min()-The MIN() aggregate function returns the lowest value (minimum) in a set of non-
NULL values.
Syntax:
MIN()
or
MIN( [ALL|DISTINCT] expression )
Example:
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Trans',
Price int);
insert into lib values('Morris Mano', 'Deepak', 'University', '2024-01-25',30, default, 120),
('Wing on Fire', 'Sweety', 'University', '2024-01-26',23, default, 400),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27',34, default, 211),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 320),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 149),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 200),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, default, 80);
select min(Price)
from lib;
Output:
The HAVING clause was added to SQL because the WHERE keyword cannot be used with
aggregate functions.
HAVING Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
Example:
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Trans',
Price int);
Syntax:
Max()
or
Max( [ALL|DISTINCT] expression )
Example:
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Trans',
Price int);
insert into lib values('Morris Mano', 'Deepak', 'University', '2024-01-25',30, default, 120),
('Wing on Fire','Sweety', 'University', '2024-01-26',23, default, 400),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27',34, default, 211),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 320),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 149),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 200),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, default, 80);
select Max(Price)
from lib;
Output:
EXPERIMENT NO:6
Aim: To implement Join statement in SQL.
SQL Join statement is used to combine data or rows from two or more tables based on a
common field between them.
Types of DDL Commands:
1. (INNER) JOIN: Returns records that have matching values in both tables.
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Example:
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Others',
Price int);
Output:
2. LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records
from the right table.
Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Example:
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Others',
Price int);
insert into lib values
('Morris Mano', 'Deepak', 'University', '2024-01-25', 30, 'Male', 120),
('Wing on Fire','Sweety', 'University', '2024-01-26', 33, default, 400),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27', 34, default, 211),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 320),
('Rich Dad, Poor Dad','Sweety', 'Library', '2025-01-26', 41, default, 450),
('Java Programming', 'Hitashi', 'Dcrust', '2024-11-07', 86, default, 123),
('The unsung Heroes', 'Sarojini', 'Office', '2023-01-25', 28, 'Female', 365),
('First Love', 'Poonam Pandey', 'College', '2024-02-24', 78, 'Female', 900);
select * from lib;
create table example
(Student_name varchar(300),
Issuing_authority char(40),
Roll_no int primary key,
Mobile_number bigint);
Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Example:
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Others',
Price int);
insert into lib values
('Morris Mano', 'Deepak', 'University', '2024-01-25', 30, 'Male', 120),
('Wing on Fire','Sweety', 'University', '2024-01-26', 33, default, 400),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27', 34, default, 211),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 320),
('Rich Dad, Poor Dad','Sweety', 'Library', '2025-01-26', 41, default, 450),
('Java Programming', 'Hitashi', 'Dcrust', '2024-11-07', 86, default, 123),
('The unsung Heroes', 'Sarojini', 'Office', '2023-01-25', 28, 'Female', 365),
('First Love', 'Poonam Pandey', 'College', '2024-02-24', 78, 'Female', 900);
select * from lib;
create table example
(Student_name varchar(300),
Issuing_authority char(40),
Roll_no int primary key,
Mobile_number bigint);
insert into example values
('Gagan', 'Dcrust', 17, 7988755597),
('Deepak', 'University', 14, 7988068458),
('Deepak Prajapati', 'Dcrust', 15, 9050431145),
('Hema', 'Library', 56, 8930556580);
select * from example;
select lib.Issuing_authority, example.Student_name
from lib
right join example
on lib.Issuing_authority = example.Issuing_authority;
Output:
4. FULL (OUTER) JOIN: Returns all records when there is a match in either left or right
table.
Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
Example:
create table lib
(Book_name varchar(100),
Issued_to char(30),
Issuing_authority Char(30),
Date_of_issue date,
Age int check(Age>=18),
Gender Char(40) default 'Others',
Price int);
insert into lib values
('Morris Mano', 'Deepak', 'University', '2024-01-25', 30, 'Male', 120),
('Wing on Fire','Sweety', 'University', '2024-01-26', 33, default, 400),
('Python Programming', 'Hitesh', 'Dcrust', '2024-01-27', 34, default, 211),
('The unsung Heroes', 'Pinki', 'College', '2024-02-24', 44, 'Male', 320),
('Rich Dad, Poor Dad', 'Sweety', 'Library', '2025-01-26', 41, default, 450),
('Java Programming', 'Hitashi', 'Dcrust', '2024-11-07', 86, default, 123),
('The unsung Heroes', 'Sarojini', 'Office', '2023-01-25', 28, 'Female', 365),
('First Love', 'Poonam Pandey', 'College', '2024-02-24', 78, 'Female', 900);
select * from lib;
create table example
(Student_name varchar(300),
Issuing_authority char(40),
Roll_no int primary key,
Mobile_number bigint);
insert into example values
('Gagan', 'Dcrust', 17, 7988755597),
('Deepak', 'University', 14, 7988068458),
('Deepak Prajapati', 'Dcrust', 15, 9050431145),
('Hema', 'Library', 56, 8930556580);
select * from example;
select lib.Issuing_authority, example.Student_name
from lib
right join example
on lib.Issuing_authority = example.Issuing_authority;
Output:
+--------------------+---------------+-------------------+---------------+------+--------+-------+-----
-------------+-------------------+---------+---------------+
| Book_name | Issued_to | Issuing_authority | Date_of_issue | Age | Gender | Price |
Student_name | Issuing_authority | Roll_no | Mobile_number |
+--------------------+---------------+-------------------+---------------+------+--------+-------+-----
-------------+-------------------+---------+---------------+
| Morris Mano | Deepak | University | 2024-01-25 | 30 | Male | 120 | Hema
| Library | 56 | 8930556580 |
| Morris Mano | Deepak | University | 2024-01-25 | 30 | Male | 120 |
Gagan | Dcrust | 17 | 7988755597 |
| Morris Mano | Deepak | University | 2024-01-25 | 30 | Male | 120 |
Deepak Prajapati | Dcrust | 15 | 9050431145 |
| Morris Mano | Deepak | University | 2024-01-25 | 30 | Male | 120 |
Deepak | University | 14 | 7988068458 |
| Wing on Fire | Sweety | University | 2024-01-26 | 33 | Others | 400 | Hema
| Library | 56 | 8930556580 |
| Wing on Fire | Sweety | University | 2024-01-26 | 33 | Others | 400 |
Gagan | Dcrust | 17 | 7988755597 |
| Wing on Fire | Sweety | University | 2024-01-26 | 33 | Others | 400 |
Deepak Prajapati | Dcrust | 15 | 9050431145 |
| Wing on Fire | Sweety | University | 2024-01-26 | 33 | Others | 400 |
Deepak | University | 14 | 7988068458 |
| Python Programming | Hitesh | Dcrust | 2024-01-27 | 34 | Others | 211 | Hema
| Library | 56 | 8930556580 |
| Python Programming | Hitesh | Dcrust | 2024-01-27 | 34 | Others | 211 |
Gagan | Dcrust | 17 | 7988755597 |
| Python Programming | Hitesh | Dcrust | 2024-01-27 | 34 | Others | 211 |
Deepak Prajapati | Dcrust | 15 | 9050431145 |
| Python Programming | Hitesh | Dcrust | 2024-01-27 | 34 | Others | 211 |
Deepak | University | 14 | 7988068458 |
| The unsung Heroes | Pinki | College | 2024-02-24 | 44 | Male | 320 | Hema
| Library | 56 | 8930556580 |
| The unsung Heroes | Pinki | College | 2024-02-24 | 44 | Male | 320 |
Gagan | Dcrust | 17 | 7988755597 |
| The unsung Heroes | Pinki | College | 2024-02-24 | 44 | Male | 320 |
Deepak Prajapati | Dcrust | 15 | 9050431145 |
| The unsung Heroes | Pinki | College | 2024-02-24 | 44 | Male | 320 |
Deepak | University | 14 | 7988068458 |
| Rich Dad, Poor Dad | Sweety | Library | 2025-01-26 | 41 | Others | 450 | Hema
| Library | 56 | 8930556580 |
| Rich Dad, Poor Dad | Sweety | Library | 2025-01-26 | 41 | Others | 450 |
Gagan | Dcrust | 17 | 7988755597 |
| Rich Dad, Poor Dad | Sweety | Library | 2025-01-26 | 41 | Others | 450 |
Deepak Prajapati | Dcrust | 15 | 9050431145 |
| Rich Dad, Poor Dad | Sweety | Library | 2025-01-26 | 41 | Others | 450 |
Deepak | University | 14 | 7988068458 |
| Java Programming | Hitashi | Dcrust | 2024-11-07 | 86 | Others | 123 | Hema
| Library | 56 | 8930556580 |
| Java Programming | Hitashi | Dcrust | 2024-11-07 | 86 | Others | 123 |
Gagan | Dcrust | 17 | 7988755597 |
| Java Programming | Hitashi | Dcrust | 2024-11-07 | 86 | Others | 123 |
Deepak Prajapati | Dcrust | 15 | 9050431145 |
| Java Programming | Hitashi | Dcrust | 2024-11-07 | 86 | Others | 123 |
Deepak | University | 14 | 7988068458 |
| The unsung Heroes | Sarojini | Office | 2023-01-25 | 28 | Female | 365 | Hema
| Library | 56 | 8930556580 |
| The unsung Heroes | Sarojini | Office | 2023-01-25 | 28 | Female | 365 |
Gagan | Dcrust | 17 | 7988755597 |
| The unsung Heroes | Sarojini | Office | 2023-01-25 | 28 | Female | 365 |
Deepak Prajapati | Dcrust | 15 | 9050431145 |
| The unsung Heroes | Sarojini | Office | 2023-01-25 | 28 | Female | 365 |
Deepak | University | 14 | 7988068458 |
| First Love | Poonam Pandey | College | 2024-02-24 | 78 | Female | 900 | Hema
| Library | 56 | 8930556580 |
| First Love | Poonam Pandey | College | 2024-02-24 | 78 | Female | 900 |
Gagan | Dcrust | 17 | 7988755597 |
| First Love | Poonam Pandey | College | 2024-02-24 | 78 | Female | 900 |
Deepak Prajapati | Dcrust | 15 | 9050431145 |
| First Love | Poonam Pandey | College | 2024-02-24 | 78 | Female | 900 |
Deepak | University | 14 | 7988068458 |
+--------------------+---------------+-------------------+---------------+------+--------+-------+-----
-------------+-------------------+---------+---------------+