Professional Documents
Culture Documents
D
D
Database
Database is required for keeping the information of an organization at one central place (not geographical
- consider distributed database on the NET) so that it can meet the requirements of different applications.
Database should be made in such a way that:
1. It can accept the required changes in future
2. It can be used by more than one independent applications
3. Avoid redundancy
4. Ensure data independence
Database is collection of interrelated files containing collection of records, where each record of file is
collection of logically related data items. Database overcomes the following problems faced in a
traditional file processing systems:
1. Data redundancy
2. Data inconsistency
3. Data not sharable
4. Non-standard data
5. Insecure data
Relational Database: It is a collection of related tables / relations. The software required to handle or
manipulate these tables or relations is known as Relational Database Management System (RDBMS).
Oracle, DB2, Access, MySQL and Visual FoxPro are some commonly used RDBM.
Table / Relation: A collection of rows and columns form a table. A relation among set of values (in each
column / attribute) is represented by a row of a table, hence the term relational database. The horizontal
subset of a Table (Relation) is known as a Row (Tuple). The vertical subset of a Table (Relation) is
known as a Column (Attribute).
A relation in a database has the following characteristics:
1. Every value in a relation is atomic, that is, 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
A relation is a set, elements in set are not ordered, no ordering defined on rows of relation.
Domain is a set of values from which actual values of an attribute of a Table (Relation) (i.e. the column
of relation obtains values from domain) are obtained.
An attribute having distinct values that can be used to identify a tuple (row) uniquely is known as Key.
A relation may have more than one such key. All the keys are known as Candidate Keys. Out of all the
possible Candidate keys, one is selected as Primary Key, and rest are called Alternate Keys. Attributes
or columns which are not Candidate Keys are known as Non-Key attributes.
Primary Key is an attribute (column) that contains distinct values which can be used to identify a tuple
(row) uniquely in a relation (table).
Non Key Attribute is an attribute which not a Candidate Key (attribute not having distinct values).
Relation: Employee
ENO NAME DESIG DOJ SAL MOB
1045 ADITYA JAIN MANAGER 2010-07-02 80000 99456972
1072 ALEX JOHN MANAGER 2007-04-09 75000 55012016
1032 RITA GUPTA MANAGER 2010-07-02 80000 66442288
1012 NITU BHAT DIRECTOR 2008-10-15 95000 55123765
1049 ALOKA SINGH DIRECTOR 2007-04-09 90000 64192837
1025 RITA GUPTA DIRECTOR 2008-10-15 95000 55876534
1017 ADITYA JAIN MANAGER 2008-09-03 75000 98283761
Non-key Attributes
In the Employee relation, ENO and MOB are Key attributes. ENO is selected as Primary key, then
MOB is the Alternate key. Attributes NAME, DESIG, DOJ and SAL are non-key attributes.
FAIPS, DPS Kuwait Page 2 of 18 © Bikram Ally
Database & SQL Class XII
Operators of Relational Algebra (Operations that can be carried out on a relation):
1. Selection (Unary Operator): Selecting horizontal subset of a relation or selecting rows from a relation.
2. Projection (Unary Operator): Selecting vertical subset of a relation or selecting columns from a
relation.
3. Cartesian Product (Binary Operator): obtaining a new relation by pairing every rows of one relation
with every rows of the other relation. It operates on two or more relations and is denoted by X. For
example Cartesian Product of two relations R1 and R2 is represented by R1 X R2. The degree of the
new relation (R1 X R2) is equal to sum of degrees of relation R1 and relation R2. The cardinality of
the new relation (R1 X R2) is product of cardinality of relation R1 and cardinality of relation R2.
Example:
4. Union (Binary Operator): Obtain a new relation by getting rows from both the relations. It operates
on two or more relations and is indicated by U. For example, R1 U R2 represents union operation
between two relations R1 and R2. The degree of the relation new relation (R1 U R2) is equal degree
of relation R1 (or degree of relation R2). The cardinality of the new relation (R1 U R2) is sum of
cardinality of relation R1 and cardinality of relation R2. Two relations are compatible for union
operation when:
a) Degree of relation R1 = Degree of relation R2
b) Corresponding attributes of the two relations (R1 and R2) must have a common domain
Example:
Relation: CS12A (R1) Relation: CS12B (R2) CS12A (R1) U CS12B (R2)
NAME MARKS GRADE NAME MARKS GRADE NAME MARKS GRADE
FATIMA 69.5 C1 ATUL 89.0 A2 FATIMA 69.5 C1
FARUQ 78.0 B2 EKTA 67.5 C2 FARUQ 78.0 B2
GAURI 95.5 A1 REKHA 88.5 B1 GAURI 95.5 A1
REKHA 89.0 A2
EKTA 67.5 C2
ATUL 88.5 B1
FAIPS, DPS Kuwait Page 3 of 18 © Bikram Ally
Database & SQL Class XII
SQL - Structured Query Language
DDL – Data Definition Language
DDL deals with structure of a table, which sets up, changes or removes data structures from the database.
A DDL command includes the word ‘table’ after the SQL command name. DDL commands include:
• Commands to create database or table
• Command to delete database or table
• Command to change structure of a table by adding column(s) or deleting column(s)
2. NUMERIC: Used to store numeric data. Any number, either integer or floating point value (real
number) can be a numeric type.
empno numeric(4,0) / empno numeric(4) – column name empno that will store 4 digits integer
value (excluding sign)
marks numeric(5,1) – column name marks that will store a floating value, 5 significant digits, 4
p1aces before decimal point and 1 place after the decimal and significant digits does not include
decimal point).
Examples of numeric type data: 12, -30, 15.5, -9.5
3. DATE: Used to store date type data. A date type value is represented as YYYY-MM-DD. Date type
constant is enclosed with either pair single quote (') or pair of double quote ("). Delimiter for char
type and date are same but char type and date are treated differently.
Date can be entered as YYYY/MM/DD format but it will be stored as YYYY-MM-DD format.
dob date – column name dob is a date type where a date will be stored in YYYY-MM-DD format.
Examples of date type: "1995-09-11", '1947-08-15'
3. In case one has forgotten the name of the database, one can find the names all the databases stored in
the backing storage.
Syntax for listing all the database names is SHOW DATABASES;
Example: mysql>SHOW DATABASES;
Database
information_schema
class12
mysql
performance_schema
test
4. In case one has forgotten the names of the tables, one can find the names in the data base by using the
SQL command show tables. Show tables command works only after use command.
Syntax for listing all the table names is SHOW TABLES;
Example: mysql>USE CLASS12;
mysql>SHOW TABLES;
Tables_in_CLASS12
student
employee
classteacher
CS12A
CS12B
5. Sometimes we need to remove the database from the backing storage. Removing a database will delete
all the tables and other files which are stored under that database will also be removed.
Syntax for removing a database is DROP DATABASE DATABASENAME;
Example: mysql>DROP DATABASE CLASS12A;
mysql>DROP DATABASE CLASS12B;
Syntax:
Create Table TableName (Col1 DataType1, Col2 DataType2, …);
Create Table command creates a new table without any rows (tuples).
Example:
create table student(roll numeric(2), name char(25), fees numeric(8,2),
dob date);
Name of the table is student. The table student will have 4 columns (attributes). Column roll will
be integer type (maximum 2 digit), column name will be character type (maximum 25 characters), column
fees will be floating point type (8 significant digits, 6 places before decimal point and 2 places after the
decimal point), column dob will be date type.
Syntax:
i) Insert Into TableName Values (Value1, Value2, Value3, …);
ii) Insert Into TableName (Col1, Col2,…) Values (Value1, Value2,…);
Insert Into command adds (inserts) a new row (tuple) in a table (relation).
Syntax i) is used when values are available for all the columns of a row.
Syntax ii) is used when values are available for selected columns of a row and remaining columns in the
will have value NULL.
Note: Values inside the parenthesis should match data type of the columns and number of values
should be equal to number of columns. If a value exceed range or date is invalid MySQL will
flag syntax error and the row will not be inserted in the table.
Examples:
i) Insert Into Student Values(21,'MANAS VERMA',7800,'1989-09-07');
Insert Into Student Values(7,'DHRUTI GUPTA',8800,'1988-02-28');
ii) Insert Into Student(Roll,Name,Fees) Values(16,'PRATAP RAO',7500);
Insert Into Student(Roll,Name,Fees) Values(2,'ANIL JAIN',8300);
Note: The column dob will have value NULL.
Syntax:
Alter Table TableName ADD Col DataType;
Alter Table command adds a new column to an existing table.
Examples:
Alter Table Student ADD Class Numeric(2);
Alter Table Student ADD Sec Char;
Note: The columns Class and Sec will have value NULL.
Syntax:
Update TableName Set Col1=Value1, Col2=Value2, … Where Condition;
Note: Update command is used to makes changes in row (rows) of an existing table. With Where
Condition changes will made (values will be updated) in row(s) where Condition is true.
Without Where Condition changes will made (values will be updated) in all the row(s).
• Operator =, when used in a condition, compares two values.
• Operator =, when used with set will assign value to a column.
Example:
Update Student Set Class=12, Sec='A' Where Roll=16;
Update Student Set Fees=7800 Where Roll=16;
Update Student Set Dob='1989-04-18' Where Roll=31;
Update Student Set Sec='D' Where Sec='A';
Update Student Set Fees=1.1*Fees;
FAIPS, DPS Kuwait Page 6 of 18 © Bikram Ally
Database & SQL Class XII
Deleting a row/rows from a table (DML)
Syntax:
Delete From TableName Where Condition;
Note: Delete From removes row(s) from an existing table. With Where Condition, row(s)
where Condition is true will be deleted. Without Where Condition, all the row(s) will
be deleted.
Examples:
Delete From Student Where Roll=13;
Delete From Student Where Name='SUSHIL JAIN';
Delete From Student Where Roll=12 Or Roll=17;
Delete From Student Where Class=12;
Delete From Student;
Syntax:
Drop Table TableName;
Drop Table command deletes entire the contents of the table along with the structure of the table.
Examples:
Drop Table Student;
Drop Table Employee;
Syntax:
i) Select * From TableName Where Condition;
ii) Select Col1, Col2, Col3, … From TableName Where Condition;
Select commands retrieves data stored in a table. Columns could be an expression also. If Where
Condition is omitted then all the rows of a table are selected.
Examples:
Select * From Student;
Roll Name Fees Dob Class Sec
21 MANISH SINGH 7800 1989-09-07 12 B
17 DHRUTI GUPTA 8800 1988-02-28 12 C
Select * From Student Where Fees Not Between 7000 And 9000;
Roll Name Fees Dob Class Sec
33 SUBHOJIT SAHA 6800 1989-01-31 12 C
10 SUSHIL JAIN 6900 1988-03-13 12 A
Syntax:
i) Select * From TableName
Order By Col1 [Asc/Desc], Col2 [Asc/Desc], …;
ii) Select * From TableName
Where Condition Order By Col1 [Asc/Desc], Col2 [Asc/Desc], …;
iii) Select Col1, Col2, Col3, … From TableName
Where Condition Order By Col1 [Asc/Desc], Col2 [Asc/Desc], …;
Order By clause used with Select command displays the rows sorted on column (Columns). By
default the sorting is in ascending order. Order By will sort the rows logically, that is, rows will appear
on sorted order only on the screen but inside the table row position remains unaltered. By adding Desc
after the column name, rows appear in descending order. Asc after the column name will sort the rows in
ascending order. Since by default sorting is in ascending order, Asc is optional after the column name. If
Where Condition is omitted then all the rows of a table are sorted.
Examples:
Select * From Student Order By Roll;
Roll Name Fees Dob Class Sec
10 SUSHIL JAIN 6900 1988-03-13 12 A
13 ASIT BHARGAV 8500 1989-04-29 12 B
14 ARPITA SHETTY 7200 1988-06-15 12 C
16 PUNEET ARORA 7500 1990-05-12 12 A
17 DHRUTI GUPTA 8800 1988-02-28 12 C
21 MANISH SINGH 7800 1989-09-07 12 B
24 SUNIT KUMAR 7500 1989-08-24 12 A
27 SUNILA DESAI 7000 1990-11-29 12 B
33 SUBHOJIT SAHA 6800 1989-01-31 12 C
FAIPS, DPS Kuwait Page 10 of 18 © Bikram Ally
Database & SQL Class XII
Select * From Student Where Sec='B' Order By Name;
Roll Name Fees Dob Class Sec
13 ASIT BHARGAV 8500 1989-04-29 12 B
21 MANISH SINGH 7800 1989-09-07 12 B
27 SUNILA DESAI 7000 1990-11-29 12 B
Select Name, Sec, Dob, From Student Where Sec='A' Or Sec='B' Order By
Sec, Name;
Name Sec Dob
PUNEET ARORA A 1990-05-12
SUNIT KUMAR A 1989-08-24
SUSHIL JAIN A 1988-03-13
ASIT BHARGAV B 1989-04-29
MANISH SINGH B 1989-09-07
SUNILA DESAI B 1990-11-29
Select Name, Sec, Dob, From Student Where Sec='A' Or Sec='B' Order By
Sec, Name Desc;
Name Sec Dob
SUSHIL JAIN A 1988-03-13
SUNIT KUMAR A 1989-08-24
PUNEET ARORA A 1990-05-12
SUNILA DESAI B 1990-11-29
MANISH SINGH B 1989-09-07
ASIT BHARGAV B 1989-04-29
Select Name, Sec, Dob From Student Where Sec in ('A','B') Order By
Sec Desc, Name;
Name Sec Dob
ASIT BHARGAV B 1989-04-29
MANISH SINGH B 1989-09-07
SUNILA DESAI B 1990-11-29
PUNEET ARORA A 1990-05-12
SUNIT KUMAR A 1989-08-24
SUSHIL JAIN A 1988-03-13
SQL Union
Example of Union:
Table: Stu1 Table: Stu2
Adno Name Adno Name
20050014 FATIMA 20050017 REKHA
20050029 FARAZ 20050023 TUSHI
20050004 GAURI 20050008 ATUL
To obtain the Cartesian Product of STUDENT and TEACHER, following SQL command is executed:
SELECT * FROM STUDENT, TEACHER;
ROLL SNAME CLA TNAME SUBJECT CLA
1 AKASH 12 MS. RITA GEOGRAPHY 11
1 AKASH 12 MR. ANUP POLITICAL SC 12
5 DIPAK 11 MS. RITA GEOGRAPHY 11
5 DIPAK 11 MR. ANUP POLITICAL SC 12
24 RUPA 12 MS. RITA GEOGRAPHY 11
24 RUPA 12 MR. ANUP POLITICAL SC 12
31 STUTI 11 MS. RITA GEOGRAPHY 11
31 STUTI 11 MR. ANUP POLITICAL SC 12
Fist row of STUDENT is paired with both rows of TEACHER, second row of row of STUDENT is
paired with both rows of TEACHER, third row of STUDENT is paired with both rows of TEACHER
and fourth row of STUDENT is paired with both rows of TEACHER. Instead of displaying all the
columns, we select columns to be displayed in the Cartesian Product. Edited SELECT command is given
below:
SELECT ROLL, SNAME, TNANE, CLA FROM STUDENT, TEACHER;
Executing this SQL query will trigger a syntax error because column CLA is common to both relations.
So, SQL is confused since it cannot decide which CLA from which table. To resolve the issue, one has to
specify the name of the common column to be preceded by the table name. SQL query SELECT * FROM
STUDENT, TEACHER; does not create any confusion since SQL has to select common column CLA
from both the tables. Corrected SELECT command is given below:
SELECT ROLL, SNAME, TNANE, STUDENT.CLA FROM STUDENT, TEACHER;
OR,
SELECT ROLL, SNAME, TNANE, TEACHER.CLA FROM STUDENT, TEACHER;
ROLL SNAME TNAME SUBJECT CLA
1 AKASH MS. RITA GEOGRAPHY 11
1 AKASH MR. ANUP POLITICAL SC 12
5 DIPAK MS. RITA GEOGRAPHY 11
5 DIPAK MR. ANUP POLITICAL SC 12
24 RUPA MS. RITA GEOGRAPHY 11
24 RUPA MR. ANUP POLITICAL SC 12
31 STUTI MS. RITA GEOGRAPHY 11
31 STUTI MR. ANUP POLITICAL SC 12
In the table TEACHER, MS. RITA teaches class 11 and MR. ANUP teaches class 12. While doing
Cartesian Product, students of class 11 and paired with teacher of class 12 and student of class 12 are
paired with teacher of class 11. So, we need to pair student class 11 with teacher of class 11 and student
of class 12 with teacher of class 12. This can be done through EQUI JOIN.
FAIPS, DPS Kuwait Page 17 of 18 © Bikram Ally
Database & SQL Class XII
SQL EQUI JOIN
A new table will be obtained by joining two tables using a common column.
SELECT ROLL, SNAME, TNANE, STUDENT.CLA FROM STUDENT, TEACHER
WHERE STUDENT.CLA=TEACHER.CLA;
ROLL SNAME TNAME SUBJECT CLA
1 AKASH MR. ANUP POLITICAL SC 12
5 DIPAK MS. RITA GEOGRAPHY 11
24 RUPA MR. ANUP POLITICAL SC 12
31 STUTI MS. RITA GEOGRAPHY 11
Instead of using complete table names in the query we can use an alias in place of table names. An alias
can be a single letter or string of letters.
SELECT ROLL, SNAME, TNANE, S.CLA FROM STUDENT S, TEACHER T
WHERE S.CLA=T.CLA;
OR,
SELECT ROLL, SNAME, TNANE, ST.CLA FROM STUDENT ST, TEACHER TE
WHERE ST.CLA=TE.CLA;
ROLL SNAME TNAME SUBJECT CLA
1 AKASH MR. ANUP POLITICAL SC 12
5 DIPAK MS. RITA GEOGRAPHY 11
24 RUPA MR. ANUP POLITICAL SC 12
31 STUTI MS. RITA GEOGRAPHY 11
Letter S(ST) is the alias for STUDENT and letter T(TE) is the alias for TEACHER.
SQL UNION
Table: CS12A Table: CS12B
NAME MARKS GRADE NAME MARKS GRADE
FATIMA 69.5 C1 ATUL 89.0 A2
FARUQ 78.0 B2 EKTA 67.5 C2
GAURI 95.5 A1 REKHA 88.5 B1
To obtain the UNION of two tables CS12A and CS12B, following SQL query is needed:
SELECT * FROM CS12A UNION SELECT * FROM CS12B;
NAME MARKS GRADE
FATIMA 69.5 C1
FARUQ 78.0 B2
GAURI 95.5 A1
REKHA 89.0 A2
EKTA 67.5 C2
ATUL 88.5 B1
OR,
SELECT * FROM CS12B UNION SELECT * FROM CS12A;
NAME MARKS GRADE
REKHA 89.0 A2
EKTA 67.5 C2
ATUL 88.5 B1
FATIMA 69.5 C1
FARUQ 78.0 B2
GAURI 95.5 A1
FAIPS, DPS Kuwait Page 18 of 18 © Bikram Ally