Professional Documents
Culture Documents
Kshirsagar SYCM
-DMS
2. RELATIONAL DATA MODEL
DATABASE -
A database is an organized collection of data.
The main purpose of database is to operate large amount of information by storing , retrieving
and managing.
There are many database available like SQL,MySQL,Oracle,SQLserver,MongoDB,Sybase etc.
TABLE:
A table is a collection of data organized in terms of rows and columns .
Below is the example of student table:
RECORD:
A single entry in table is called a Record or Tuple or Row.
1 Priya 16 Pune
FIELDS:
A space allocated for a perticular item of information.
In Database a fields are the smallest units of information you can access.
fields may have an attributes associated with them.
DATA TYPES
1. NUMERIC DATA TYPES
INT or INTEGER
SMALLINT
NUMBER
2. APPROXIMATE NUMERIC DATA TYPES
FLOAT(p)
Real
3. DATE AND TIME DATA TYPES
datetime
date
time
timestamp
4. CHARACTER STRINGS DATA TYPES
char
varchar
varchar(max)
text
5. FORMATTED NUMBERS
DEC(p, s) or DECIMAL(p,s)
NUM(p,s) or NUMERIC(p,s)
p - precision value i.e. number of digits in a number
s - scale value i.e. number of digits to right side after point.
CONCEDPT OF RDBMS
In relational model data is stored in relations i.e tables and it is represented in the form of
rows/tuples.
RDBMS is used to manage realational database.
Relational database is a collection of organized set of tables related to each other and from it we
can access data easily.
Relational database is the most commonly used database.
E.F.codd was a scientist who invented the relational model for database managent. Based
onrelationalmodel,the relational database was created.codd proposed rules known as codd’s 12
rules. They are as follows:
Codd rules:
Rule 1 : The information rule
According to E.F. codd’s first rule, the whole data/information has to be presented in the form of
table.
Rule 2 : Guaranteed Access Rule
second rule states that whole data should be accessible to the user by the combination of table
name, primary key, and column name.
e.g. select name from student where rollno=3;
Database dictionary must have description of database. The database structures i. e metadata and
its contents are stored into tables so some commands should be used to access data as well as
catalogs.
The RDBMS must support at least one clearly defined language that includes functions for data
definition, data manipulation,data integrity and transaction control.
e.g. SQL
The application programs are not affected by changes in the physical data access and storage
methods.
e.g. if some file supporting table is renamed or moved from one disk to other ,it should not affect
the application.
Logical changes in tables and views such as adding/deleting columns or changing column size
should not affect on application program.
E.g. if a table split into two tables, a new view should give result as the join of the two tables.
KEY CONCEPTS:
Key:
To identify the entity in entity set.
Candidate key:
OR
Definition:In a relation, there may be a primary key or may not, but there may be a key or
combination of keys which uniquely identify the record. Such a key is called as Candidate key.
Primary key:
Definition :A key which is selected by the designer to uniquely identify the entity is called as
Primary key.
A primary key cannot contain duplicate values.
It can never contain null values inside it.
each table can have only ONE primary key
Foreign key:
Definition :A foreign key is an attribute of relation r1 which is used as primary key of other
relation r2.
Here , customer relation contains one attribute loan_id which is primary key of loan relation.
Such attribute is called as foreign key of customer relation.
2.3 Normalization
Definition: Normalization can be defined as process of decomposition of database tables to avoid
the data redundancy.
The normalization is an important process that removes the repeating data from database andalso
used to remove the inconsistency.
Need of normalization
Insert anomaly due to lack of that is all the data available for insertion such that null
values inkeys should be avoided. This kind anomaly and seriouslydamage a database.
Update anomalydue to data redundancy that is multiple occurrences of same values
incolumn.
Deletion anomaly it leads to loss of data for rows that are not stored else square it could
result in loss of vital data.
Types of Normalization:
1 NF
2NF
3NF
The above relation is in 1NF as all the domains are having atomic value. But it is not in 2NF
Example: In the above relation NAME, LOCATION depends on SNO and QTY on (SNO, PNO)
so the table can be split up into two tables as Supplier(SNO,SNAME,LOCATION) and Supplier
Products(SNO,PNO,QTY) and now both the tables are in second normal form.
Supplier
Supplier products
If the value of a non-key column is dependent on the value of another non-key column we have a
situation known as transitive dependency.
So in above supplier product table, the non key column QTY is dependent on non key column
PNO but non key column PNO is depends on primary key SNO, so QTY transitively depends on
SNO for removing this transitivity create table product with non key columns i.e. PNO and QTY.
{ QTY -> PNO
PNO -> SNO
QTY->SNO
}
Supplier
Suppliers product
SNO PNO
S1 P1
S2 P2
S3 P3
product
PNO QTY
P1 200
P2 300
P3 400
1NF:
1) Student (student_id, student_name, Dob, Steet, City, State, Zip)
2NF:
1) Student(Student_id, Student_name, Dob)
2) Student_details(student_id, , Street,City,State,Zip)
3NF:
1) Student(Student_id, Student_name, Dob)
2) Student_details(student_id ,Zip)
3)Address(Zip, Street,City,State)
This includes changes to the structure of the table like creation of the table, altering table,
deleting structure of the table(i.e. drop)
All DDL commands are auto committed that means it saves all the changes permanently.
e.g. List of DDL commands
1. Create
2. Alter
3. Rename
4. Drop
5. Truncate
6.Rename
DDL commands:
1) CREATE
This command is used to create the table or define the structure of the table.
Syntax:
e.g:
create table student ( rollno number(3), name varchar(20), city varchar(20), dob date);
2) DESCRIBE or DESC
Used to describe the structure of the table
Syntax:
desctable_name;
e.g:
desc student;
3)ALTER
This command is used to make certain changes in structure of table after creating it.
It helps in modifying the table if we want to change datatype or size of column ,add new column,
delete i.e drop any column from existing table.
Syntaxes:
syntax:
alter table table_name add column_namedatatype(size);
syntax:
alter table table_name drop column column_name;
Sytax:
Alter table table_name modify column_name datatype(size);
in above example we have changed the datatype of marks column number to float and size from
3 to 4.
Syntax:
Alter table table_name rename column old_column_name to new_column_name;
in above example we have renamed column name of city to address in an existing table student.
e)To add primary key
Syntax:
Alter table table_name add primary key(column_name);
in above example we have added or set the primary key for column rollno.
Syntax:
alter table table_name drop primary key;
4)TRUNCATE
6) DROP
This command is used to delete the structure of table.That is the whole table structure is deleted
with data permanently i.e. table does not exists.
Syntax:
in above example the structure of the student_info table is deleted .it will does not exist after
using this command.
Integrity constraints are used to ensure accuracy and consistencyof data in relational database.
In RDBMS accuracy and consistency stands for completeness and correctness.
I/O constraints
Constraints are the rule which apply to table columns to store valid data and prevents the user to
enter invalid data into columns.
a) Primary key constraints :
Syntax:
e.g. create table emp(eid number constraint eid_pk primary key, enamevarchar(20),city
varchar(20));
This is also called as referential integrity constraints. Parent table is known as referenced table
and table with foreign key is called as child constraint.
Syntax:
Create table table_name(column_name1 datatype(size),column_name2 datatype(size)
constraint constraint_name references referenced_table_name(column_name),
….,column_name_ndatatype(size));
Examples:
1)[ loan(lid,type,amount)
customer(cid,cname,caddr,lid) …..in this lid is foreign key i.e taken from loan table so we will
create table customer.]
2)[Dept-{deptno,deptname}
emp-{empid,empname,deptno,salary} …here deptno is foreign key which taken the reference of
dept table.]
Create table emp(empid number(4),empnamevarchar(15),deptno number(4) constraint
deptnofk references dept(deptno),salary number(8));
Unique Constraint: The UNIQUE constraint is used to avoid the duplicate values.
The unique and primary key constraints both provides the guarantee for uniqueness i.e both keys
are avoid duplicate values but unique key accept null values.
Syntax:
OR
create table table_name (column_namedata_type(size),…..,column_name_ndata_type(size)
unique);
default constraints are used to provide default values for a column. The default value will be
added to all new records if no other value is specified.
Syntax:
Create table table_name (column_name1 datatype(size),column_name_2 datatype (size) default
value,---, column_name n datatype(size) );
Example:
Student(rollno,name,city)….take default value for city as pune
Syntax:
Create table table_name(column_name1 datatype(size),column_name2 datatype (size)
constraint constraint_name not null,…,column_name_ndatatype(size));
OR
Example:
Example:
1) create table emp( empno number(5), ename varchar2(25), salary
number(7)constraintemp_sal_ck check(salary > 5000), job varchar2(15) );
or
create table emp( empno number(5), ename varchar2(25), salary number(7) check(salary >
5000), job varchar2(15) );
DML COMMANDS:
DML stands for data manipulation language,These commands are used to insert,update,delete
data in database table.
DML commands are insert,delete,update.
i)insert:
we can insert data in table by 4 ways:
consider table student(rollno,name,marks,city)
ii) Update:
this command is used to update the column values which are inserted already.This command is
used to update or modify single or multiple rows of the table.
Syntax:
Update table_name set column_name=value where condition;
Example: change the name of rollno3 byarjunto pravin
update student set name=’pravin’ where rollno=3;
iii) DELETE
Detele command is used to delete single or multiple records from table.You can delete specific
rows using where condition/clause.When all rows deleted structure of the table remains same
Syntax:
above example deletes all rows/details of table student but table structure remains same.
DCL commands
A data control language (DCL) is used to create rules,permissions and referential integrity.
It is used to control access to database by securing it.
DCL commands ibcludes following commands:
a)commit
Commit command is used to end the transaction and also make the effect permanent to the
database. That is it save the changes permanently.
Syntax:
Commit;
Or
Commit work;
Example: commit;
b)savepoint
savepoint command is used to temporarily save transaction so that you can rollback to that point
whenever required
Syntax:
Savepointsavepoint_name;
Example: Savepoint SV1;
c) Rollback:
This command restores the database to last committed state.It is also used with savepoint
command to jump toasavepoint in an ongoing transaction.
Syntax:
ROLLBACK;
Example: rollback;
OR
ROLLBACK WORK;
OR
ROLLBACK TO SAVEPOINT_NAME;
Example: update employee set salary=salary-500;
Savepoint s1;
d)GRANT
grantcommand is used to provide access or privileges on the database objects to the users.
Syntax
GRANT privilege_nameON object_name TO user_name ;
Example:
GRANT insert,updateON emp TO pravin ;
2. REVOKE
revokecommand removes user access rights or privileges to the database objects.
Once you have granted privileges,you may to remove some or all of these privileges.
Revoke means taking authorization back from user.
The revoke command is used to denied grant given on the object.
Syntax
Example
REVOKE insert, updateON emp FROM staff ;
Example:
Revoke all on employee from pravin;
DQL COMMANDS:
DQL stands for data query language which includes only one command. i.e select
SYNTAX:
a)To display all rows and columns (i.e all details or all data)
d)for applying more than one conditions we can use logical operators AND,OR,NOT with
select command
example: 1) select name, address from student where rollno< 3 and name= ‘pravin’;
2)selectrollno,name, address from student where rollno< 4 or name= ‘rupali’;
NOT
Example: select rollno, name where rollno not like 5;
SQL OPERATORS:
SQL operators are used to operate on value or numbers.
The SQL operators are as follows:
Consider table:
Empid Ename deptname deptno salary address
1 A Production 11 35000 Pune
2 B Manufacturer 22 20000 Mumbai
3 C Design 33 25000 Kolhapur
4 D Testing 44 15000 Delhi
5 E developing 55 60000 Pune
A) Arithmetic operators:
List and explain any 4 arithmetic operators in SQL with example.
+ (addition)
- (subtraction)
* (multiplication
/ (division)
Addition
Addition is performed using the plus (+) symbol.
Example
SELECT SALARY + 20000 “total_sal” FROM EMPLOYEE;
Meaning:
The SALARY column is added with the 20000 for each row of data .
Subtraction
Subtraction is performed using the minus (-) symbol.
e.g
SELECT SALARY –5000 “decremented_sal” FROM EMPLOYEE;
Meaning:
The SALARY column is substractedby5000 for each row of data
Multiplication
Multiplication is performed by using the asterisk (*) symbol.
Example:
SELECT SALARY * 10 FROM EMPLOYEE;
Division
Division is performed through the use of the ‘/’ symbol.
e.g
SELECT SALARY / 10 FROM EMPLOYEE;
Meaning:
The SALARY column is divided by 10
B)Range operators:
Explain range searching operators:
(i) IN and NOT IN
(ii) BETWEEN and NOT BETWEEN.
(Each operator – 1 Mark)
Ans:
(i) IN and NOT IN
IN: x in (y,z)
This operator tests that operand x matches at least one element of the list provided.
Example: select * from employee where salaryin(35000,60000,17000);
Meaning:it displays information of employees whose salary is in 35000,60000,17000.
.
Meaning: it will display details of employees whose salary between 15000 and 25000.
Example:
1 production
Meaning: It displaysempid , deptname whose deptname start from p i.e p on first location.
Display empid,deptname whose employee does not contain ‘a’ at 2nd location.
Ans: select empid,deptname from employee where deptname not like ‘_a%’;
1 production
2 design
4 testing
5 developing
3 design
3 design
1 testing
D) Comparision operators:
Used for comparing attribute values.
= -equal to
!= -Not equal to
>-greater than
<- lessthan
E) Logical Operators:
It is used with specific or combination of conditions.
Examples:
Select * from employee where salary > 20000 and salary < 40000;
Select empname, salary, address from employee where address not like ‘pune’;
F)SET OPERATORS:
When we want to combine the result of 2 queries then the special type of operators are used
known as set operators.
Union
Union all
Intersect
Minus
Employee
Empid Deptno
1 101
2 102
3 103
4 104
8 105
10 106
Department
Union operator selects different values of same attribute from different related tables.
e.g: select empid from employee union select empid from department;
output: empid
1
2
3
4
8
10
11
12
Union all
Union all operator combines all records from both the queries without avoiding duplicate values.
i. e. it selects all values of same attribute from different related tables without avoiding duplicate
values.
output: empid
1
2
3
4
8
10
1
2
8
10
11
12
e.g. select empid from employee intersect select empid from department;
Output: empid
1
2
8
10
Minus
It performs the subtraction. i.e. values present in first table and not in second table are displayed.
e.g. select empid from employee minus select empid from department;
Output: empid
3
4
e.g. select empid from department minus select empid from employee;
Output: empid
11
12
Ans:
i) select * from Emp where salary >= 5000;
ii) select * from Emp where job=’Manager’ or job=’Analyst’;
OR
ii) select * from Emp where job in(’Manager’ , ’Analyst’);
iii) update Emp set location=’Pune’ where deptno=40; {consider location attribute in Emp table}
iv) selectEname,Salary from Emp where salary >50000 and(deptno = 10 or deptno=30);
OR
Select emp_id from Employee where emp_cityin(‘Mumbai‘, ‘Pune‘);
iii) select * from Employee where join_date>’01-Apr-1997’;
iv) select count(emp_id) from Employee where emp_dept = 10;
Consider the structure of student (Name, Mark, Age, Place, Phone, Birthdate).
WriteSQL queries for the following :
To list name of student who do not have phone number
Select name from student where phone is null;
MSBTE QUESTIONS
Explain candidate key and primary key.
Describe following keys:
(i) Primary key.
(ii) Foreign key.
(Each key – 2 Marks)
Explain Foreign Key with suitable example.
(Foreign key explanation with example – 3Marks, On delete cascade – 1 Mark)
List first four codd rules.
(For each rule - ½ Mark)
Define normalization.
(Definition - 2 marks)
Explain need of normalization and state 3NF.
(Need - 3 Marks, 3NF - 1 Mark)
List advantages of Normalization.
State and explain INF and 2NF with example.
(For 1NF - 2 Marks, For 2NF -2 Marks)
Explain 3NF with example.
(Explanation - 2 marks; example - 2 marks)
Given: Student (Roll_No, Name, Class, Total_Marks, Percentage, Grade)
normalize up to 3NF
What is SQL? State its features.
List four DML commands.
(For each command - ½Mark)
List four DDL commands. 2M
List DCL commands any four. 2M
Give the syntax and example of CREATE and RENAME Commands. 4M
Explain ALTER command with any two options. 4M
Explain Alter command. Give syntax of add and modify option.
(Explanation - 2 Marks, for each syntax - 1 Mark)
Describe commit and rollback with proper syntax.
Give the use of grant and revoke command with syntax and example.
What are the four ways to insert a record in a table?
(Each Syntax or example- 1 Mark)
Explain the difference between DROP and TRUNCATE with example.
(Any 4 valid differences – 1 Mark each OR Explanation of DROP and TRUNCATE – 2
Marks each)
How to apply NOT NULL constraint at the time of table creation? Give syntax.
(For correct syntax - 2 Marks)
Explain Domain/Business integrity constraint with syntax and example.
Explain entity integrity constraints with syntax and example. 4M
Explain Domain/Business integrity constraint with syntax and example.
How to apply NOT NULL constraint at the time of table creation? Give syntax.
Sou.venutaichavan polytechnic, pune Page 33
Ms. S. A. Kshirsagar SYCM
-DMS
(For correct syntax - 2 Marks)
State use of ‘%’ character in string operations.
(Explanation - 2 Marks)
Explain range searching operators:
(i) IN and NOT IN
(ii) BETWEEN and NOT BETWEEN.
(Each operator – 1 Mark)
List and explain any 4 arithmetic operators in SQL with example.
(For each - 1 mark)
State 4 (four) difference between DBMS and RDBMS.
(Any correct difference - 1 Mark each)
Assignment No2
• Consider the structure of student (Name, Mark, Age, Place, Phone, Birthdate).
• WriteSQL queries for the following :
• To list name of student who do not have phone number
• To list students from Mumbai and Pune.
• To change mark of ‘Ajay’ to 88 instead of 80.
• To list the students whose age is more than 12.