05 CS Notes - RDBMS & SQL
05 CS Notes - RDBMS & SQL
Relational Database Management System: The software required to handle/manipulate these
tables/relations is known as Relational Database Management System (RDBMS) – Oracle, Sybase,
DB2, MS SQL Server, MYSQL, etc.
Table/Relation: Table is the collection of related data entries which means that the table should
consists of columns and rows. The horizontal subset of the Table is known as a Row/Tuple. The
vertical subset of the Table is known as a Column/an Attribute. A relation in a database has the
following characteristics:
1. Every value in a relation is atomic - i.e. it cannot be further divided
2. Names of columns are distinct and order of columns is immaterial
3. The rows in the relation are not ordered
“Since relation is a set, and sets are not ordered hence no ordering defined on tuples of relation”
Table:Employee Table:Pay
Key: An attribute/group of attributes in a table that identifies a tuple uniquely is known as a
Key. A table may have more than one such attribute/group of attribute that identifies a tuple
uniquely, all such attribute(s) are known as Candidate Keys. Out of the Candidate keys, one is
selected as Primary Key, and others become Alternate Keys.
Table:Stock Table:Department
Resultant : R = R1 X R2
RollNo Name Class Code TeacherName
1 Akash XII 102 Ms Rinee.
1 Akash XII 309 Mr.Tanmoy
4 Debakar X 102 Ms. Rinee
4 Debakar X 309 Mr. Tanmoy
10 Rishi XI 102 Ms. Rinee
10 Rishi XI 309 Mr. Tanmoy
4. Union (binary operator): It operates on two relations and is indicated by U. For
example, R=R1 U R2 represents union operation between two relations R1 and R2. The
degree of R is equal to degree of R1. The cardinality of R is sum of cardinality of R1 and
cardinality of R2. Following have to be considered for the operation R1 U R2.
Examples:
ALTER TABLE student ADD (ClassSec VARCHAR(3));
Syntax:
SELECT */<Col1>,<Col2>, ... <Col N>
FROM <Table Name>
WHERE <Condition>;
Example:
SELECT * FROM student;
RNO NAME FEES DOB CLASSSEC
34 Manish 7800 2007-09-03
35 Dhruv 8800 2000-02-29
38 Gagan 9800 2010-09-01
23 Rimal 6700 12C
Use of IN (used for distinct set) and BETWEEN (used for a range) with all data types
SELECT * FROM student WHERE Rno IN (23,34,38);
RNO NAME FEES DOB CLASSSEC
34 Manish 7800 2007-09-03
38 Gagan 9800 2010-09-01
23 Rimal 6700 12C
Example:
UPDATE student SET ClassSec='12A' WHERE Rno<36 AND Name<>'Rimal';
Example:
SELECT * FROM student ORDER BY Rno;
RNO NAME FEES DOB CLASSSEC
23 Rimal 6700 2001-07-01 12C
34 Manish 7800 2007-09-03 12A
35 Dhruv 8800 2000-02-29 12A
38 Suryansh 9800 2010-09-01 12B
SELECT ClassSec,Name,DOB,Fees
FROM student ORDER BY ClassSec, Name;
CLASSSEC NAME DOB FEES
12A Dhruv 2000-02-29 8800
12A Manish 2007-09-03 7800
12B Suryansh 2010-09-01 9800
12C Rimal 2001-07-01 6700
SELECT ClassSec, Name, DOB, Fees FROM student ORDER BY ClassSec DESC, Name;
CLASSSEC MAX(DOB)
12A 2007-09-03
12B 2010-09-01
Precedence of Clauses
SELECT */<Col1>,<Col2>,..., <Col.N>/<Expression>/<Agg.Func.> FROM
<Table(s) Name(s)>
[WHERE <Condition>]
[GROUP BY <Grouping Col.>]
[HAVING <Aggregate Condition>]
[ORDER BY <OrderingCol1>[ASC/DESC],<OrderingCol2> [ASC/DESC]...];
Syntax:
DELETE FROM <Table Name> [WHERE <Condition> ];
Example:
DELETE FROM Student WHERE Rno=13;
To delete all rows of a table (Does not delete the structure of the table)
To delete structure of a table – (DDL) To delete the data as well as the structure
Syntax:
DROP TABLE <Table Name>;
Example:
DROP TABLE Student;
To modify the data type of a column - (DDL)
Syntax:
ALTER TABLE <Table Name> MODIFY <Col1> <Data Type>;
Example:
ALTER TABLE Student MODIFY Name CHAR(30);
To delete a column from a table - (DDL)
Syntax:
ALTER TABLE <Table Name> DROP COLUMN <Column Name>;
Example:
ALTER TABLE Student DROP COLUMN Rno;
Join
Table:ADMISSION Table: FEE
RNO NAME RNO FEES
2 Fardeen 3 3500
3 Harish 1 2500
1 ANIK 4 3000
4 PRIYA
SELECT A.Rno,Name,Fees
FROM Admission A,Fee B WHERE A.Rno=B.Rno;
RNO NAME FEES
3 Harish 3500
1 ANIK 2500
4 PRIYA 3000
SELECT A.Rno,Name,Fees
FROM Admission A,Fee B WHERE A.Rno=B.Rno ORDER BY 1;
RNO NAME FEES
1 ANIK 2500
3 Harish 3500
4 PRIYA 3000
SELECT A.Rno,Name,Fees
FROM Admission A,Fees B WHERE A.Rno=B.Rno ORDER BY 3 DESC;
RNO NAME FEES
3 Harish 3500
4 PRIYA 3000
1 ANIK 2500
Union
• The number of columns selected from each table should be same
• The data types of corresponding columns selected from each table should be same
Table: Boys Table: Girls
Rno Name Rno Name
1 Rahat 7 Tara
2 Harish 12 Jaya
13 Tarun
Rno Name
1 Rahat
2 Harish
13 Tarun
7 Tara
12 Jaya
SELECT Rno,Name FROM Boys UNION SELECT Rno,Name FROM Girls ORDER BY 2;
Rno Name
2 Harish
12 Jaya
1 Rahat
7 Tara
13 Tarun
SELECT Name FROM Boys WHERE Rno<13 UNION SELECT Name FROM Girls WHERE Rno>7;
Name
Rahat
Harish
Jaya