0% found this document useful (0 votes)
110 views13 pages

DMS Unit 2 22319-1

The document discusses key concepts related to relational databases including tables, records, primary keys, foreign keys, and normalization. It provides examples of first normal form (1NF), second normal form (2NF), and third normal form (3NF). It also covers SQL data definition language (DDL) commands like CREATE, ALTER, DROP and data manipulation language (DML) commands like INSERT.

Uploaded by

Nisarg Gugale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
110 views13 pages

DMS Unit 2 22319-1

The document discusses key concepts related to relational databases including tables, records, primary keys, foreign keys, and normalization. It provides examples of first normal form (1NF), second normal form (2NF), and third normal form (3NF). It also covers SQL data definition language (DDL) commands like CREATE, ALTER, DROP and data manipulation language (DML) commands like INSERT.

Uploaded by

Nisarg Gugale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

DMS Unit 2: Relational Data Model (18 Marks)

Record, Table, Database:

Data: Raw fact (table, student, employee, etc)

Database: collection of data. Data stored in table format

Table: Collection of rows and columns. Row of table is referred as RECORD.

E.F. Codd’s Rules:

Rule 1: The information Rule


Rule 2: Guaranteed Access Rule
Rule 3: Systematic treatment of null values
Rule 4: Dynamic online Catalog
Rule 5: Data Sub language rule
Rule 6: View updating rule
Rule 7: High level insert, delete and update
Rule 8: Physical data independence
Rule 9: Logical data independence
Rule 10: Integrity independence
Rule 11: Distribution independence
Rule 12: No subversion rule

Key Concepts:

Student database

Rollno(pk) Name Fees Address City Contact


1 Anil 1000 Pune Pune 99
2 Anil 1000 Kolhar Rahuri 88

1. Primary key: contains unique values and does not allow the null value(s).
Ex: rollno

2. Super key: set of two or more attributes.


Ex: {rollno, name, city} {rollno, fees, city, contact}

3. Candidate key: proper subset of super key.


Ex: {rollno, name} {name, city}

4. Foreign key:
Foreign Key: Primary Key of one table can be referred in another table.
Primary key table is parent table and the table in which primary key is referred is child table.
For foreign key 2 tables required.
Primary key column should be same (name, datatype, size) in parent and child tables.

Parent table
Create table stud1(rollno number (3) constraint pk primary key, name char (5), fees number
(5), contact number (10));
Child table
Create table stud2(rollno number(3) references stud1(rollno), class char(4), exam char(5),
admission_year numer(4));
Stud1 (Parent table)
Rollno(PK) Name Fees Contact
1 Anil 1000 99

Stud2 (child table)

Rollno (FK) Class Exam Admission_Year


1 SYCM MSBTE 2020-21

Normalization: is process of decomposition of database to arrange the data and reduce the
duplicate records. (1NF, 2NF, 3NF)

First Normal Form (1NF)

o A relation will be 1NF if it contains an atomic value.


o It states that an attribute of a table cannot hold multiple values. It must hold only
single-valued attribute.
o First normal form disallows the multi-valued attribute, composite attribute, and their
combinations.

Example: Relation EMPLOYEE is not in 1NF because of multi-valued attribute empphone

EMPLOYEE table:

empid empname empphone empstate


14 John 7272826385, UP
9064738238
20 Harry 8574783832 Bihar
12 sam 7390372389, Punjab
8589830302

The decomposition of the EMPLOYEE table into 1NF has been shown below:

empid empname empphone empstate


14 John 7272826385 UP
14 John 9064738238 UP
20 Harry 8574783832 Bihar
12 sam 7390372389 Punjab
12 sam 8589830302 Punjab
Second Normal Form (2NF)
o In the 2NF, relational must be in 1NF.
o In the second normal form, all non-key attributes are fully functional dependent
on the primary key
Example: Let's assume, a school can store the data of teachers and the subjects they teach. In
a school, a teacher can teach more than one subject.
TEACHER table

tid subject age


25 Chemistry 30
25 Biology 30
47 English 35
83 Math 38
83 Computer 38

In the given table, non-prime attribute TEACHER_AGE is dependent on TEACHER_ID


which is a proper subset of a candidate key. That's why it violates the rule for 2NF.
To convert the given table into 2NF, we decompose it into two tables:
TEACHER_DETAIL table:
tid age
25 30
47 35
83 38

TEACHER_SUBJECT table:
tid subject
25 Chemistry
25 Biology
47 English
83 Math
83 Computer

Third Normal Form (3NF)

o A relation will be in 3NF if it is in 2NF and not contain any transitive partial
dependency.
o 3NF is used to reduce the data duplication. It is also used to achieve the data integrity.
o If there is no transitive dependency for non-prime attributes, then the relation must be
in third normal form.

A relation is in third normal form if it holds atleast one of the following conditions for every
non-trivial function dependency X → Y.

1. X is a super key.
2. Y is a prime attribute, i.e., each element of Y is part of some candidate key.

Example:

EMPLOYEE_DETAIL table:
empid ename zip state city
222 Harry 201010 UP Noida
333 Stephan 02228 US Boston
444 Lan 60007 US Chicago
555 Katharine 06389 UK Norwich
666 John 462007 MP Bhopal

Super key in the table above:

1. {EMP_ID}, {EMP_ID, EMP_NAME}, {EMP_ID, EMP_NAME, EMP_ZIP}.


...so on

Candidate key: {EMP_ID}

Non-prime attributes: In the given table, all attributes except EMP_ID are non-
prime.

Here, EMP_STATE & EMP_CITY dependent on EMP_ZIP and EMP_ZIP


dependent on EMP_ID. The non-prime attributes (EMP_STATE, EMP_CITY)
transitively dependent on super key(EMP_ID). It violates the rule of third normal
form.

That's why we need to move the EMP_CITY and EMP_STATE to the new
<EMPLOYEE_ZIP> table, with EMP_ZIP as a Primary key.

EMPLOYEE table:

empid ename zip


222 Harry 201010
333 Stephan 02228
444 Lan 60007
555 Katharine 06389
666 John 462007

EMPLOYEE_ZIP table:

zip state city


201010 UP Noida
02228 US Boston
60007 US Chicago
06389 UK Norwich
462007 MP Bhopal
Datatypes in SQL

Datatypes are the variables which accepts the specific values. (ex. number, alphabets, alphanumeric,
etc)

1. Char: only accepts alphabets.


Ex: A-Z/a-z (256 characters only)
Create table emp(ename char(5));

2. Number / Numeric: only accepts numbers/digits.


Ex: 0-9
Create table emp (contact number(10));

3. Date: accepts only dates.


Ex: date in format mm/dd/yyyy or dd/mm/yyyy
Create table emp(joindate date);

4. Varchar: flexible version of character datatype.


Varchar accepts characters as well as numbers.
Accepts the characters up to 2 GB.
EX: anil1055
Create table emp (address varchar (15));

Rollno number (2) Name char (4) Address varchar (15) joindate
10 Anil 16B Pune 12/12/2002
1A Sunil14 16A 12

In above table first record is correct and second record (red color) is incorrect.
SQL: Structured Query Language

DDL Commands: Data Definition Language

These commands are used to create and modify the tables in DB.

1. Create: use to create a new table in DB


Ex: Create table stud (rollno number (2), name char(6), DOB date, address varchar(15));

2. Alter: use to modify or change the existing table structure.

Ex: Alter table stud modify rollno number (3);


Ex: Alter table stud modify name varchar(10);
Ex: Alter table stud add fees number(10);
Ex: Alter table stud drop column fees;
Ex: Alter table stud rename column name to fname;

3. Drop: deletes the table structure as well as data from database.


Ex: Drop table stud;

4. Truncate: delete the data not structure of table.


Ex: Truncate table stud;

5. Rename: change the name of the table


Ex: Rename stud to stud1;

6. Desc: see the description of the table


Ex: Desc stud1;

DML commands: Data Manipulation Language

These commands are used to manipulate (insert/delete/update) the data.

1. Insert: add new record in table(data).


Ex: Insert into stud1 values(11,’Anil’,’12-dec-2020’,’16BPune’);

2. Update: modify the existing data.


Ex: Update stud1 set name=’Sunil’;(update all names)
Ex: Update stud1 set name=’Sunil’; where rollno=11; (update the name of rollno 11 only)

3. Delete: delete the specific or all data.


Ex: Delete from stud1; (deletes all data from table)
Ex: Delete from stud1 where rollno=11; (deletes only the data whose rollno is 11)

DQL: Data Query Language

These commands are used to display the records from table.

Select: use to display all or specific data from table.

Ex: Select * from stud1; (displays all records)

Ex: Select rollno, name from stud1; (displays only rollno and name)

Ex: Select * from stud1 where address=’Pune’;

Ex: Select name from stud1 where address=’Pune’;


DCL: Data Control Language

These commands are used to control access of users to Database.

Grant: is used to give the privileges (rights) to users.

Ex: Grant select, insert on emp to anil;

Revoke: is used to cancel the given privileges (rights) from users.

Ex: Revoke select, insert on emp from anil;

create user abc identified by abc;

grant create session ,create table,unlimited tablespace to abc;

Grant succeeded.

SQL> grant select on system.emp to abc;

Grant succeeded.

SQL> exit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production

C:\Windows\system32>sqlplus
nter user-name: abc
Enter password:

Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production

SQL> create table s(rno number(2));

Table created.

SQL> select * from system.emp ;

EMPNO ENAME SALARY BONUS


---------- ---------- ---------- ----------
101 anil 2000 500
102 sunil 3000 1000
103 Akash 5000 2000
104 Anil 6000 3000

revoke select on system.emp from abc;

Revoke succeeded.

drop user abc cascade;

User dropped.

select owner,table_name from all_tables; //to view list of tables


TCL: Transaction Control Language

These commands are used to control or manage the transactions in database.

1. COMMIT: used to permanently save any transaction in database.


Ex: commit;

2. ROLLBACK: used to restores the database to last committed state.


Ex: rollback to sp1;

3. SAVEPOINT: used to save the transaction temporarily for rollback.


Ex: savepoint sp1;

Consider stud table.

Insert two records in table. (stud table contains two records)

Savepoint sp1; (creates savepoint sp1 i.e. sp1 stores two records)

Delete one record from table. (stud table contains one record)

Rollback to sp1; (restores stud table up to savepoint sp1 i.e. now stud table contains two records)

Delete one record from table; (stud table contains one record)

Commit; (permanently saves the changes i.e. one record in stud table)

Rollback to sp1; (do not restores the stud table because commit command is executed)
SQL Operators

Ex: emp table

empno Ename Salary bonus


101 Anil 2000 500
102 Sunil 3000 1000
103 Akash 5000 2000
104 Anil 6000 3000

Arithmetic Operators:

1. Addition +
Ex: Select salary+bonus from emp;
2. Subtraction –
Ex: Select salary-bonus from emp;
3. Multiplication *
Ex: Select salary*12 from emp;
4. Division /
Ex: Select salary/2 from emp;

Comparison Operators:

1. Less than <


Ex: Select * from emp where salary<3000;
2. Greater than >
Ex: Select * from emp where salary>3000;
3. Less than equal to <=
Ex: Select * from emp where salary<=3000;
4. Greater than equal to >=
Ex: Select * from emp where salary>=3000;
5. Equal to =
Ex: Select * from emp where salary+bonus=4000;
Ex: Select * from emp where salary+bonus<=3000;
6. Not equal to != (<>)
Ex: Select * from emp where salary!=3000;
Ex: Select * from emp where salary<>3000;

Logical Operators:

1. AND: displays the record(s) if both conditions can be TRUE


Ex: Select * from emp where salary=3000 and ename=’Akash’;
Ex: Select * from emp where salary+bonus<7000 and ename=’Anil’;

2. OR: display the record(s) if any one condition from two conditions can be TRUE
Ex: Select * from emp where salary=3000 or ename=’Akash’;
Ex: Select * from emp where salary+bonus<7000 or ename=’Anil’

3. NOT: is the negation of given condition.


Ex: Select * from emp where salary not between 3000 and 5000;
Ex: Select * from emp where salary not in (1500, 2000, 3500, 6000);
Range Searching Operators:

1. BETWEEN: displays the data between given range.

Ex: Select * from emp where salary between 3000 and 5000;

Ex: Select * from emp where salary not between 3000 and 5000;

2. IN: displays the data from given values.


Ex: Select * from emp where salary in (1500, 2000, 3500, 6000);
Ex: Select * from emp where salary not in (1500, 2000, 3500, 6000);

Pattern Matching Operator:

1. LIKE: used to display the records by matching the exact data or pattern.
Ex: Select * from emp where ename like ‘Anil’;
Ex: Select * from emp where ename not like ‘Anil’;
Ex: Select * from emp where ename like ‘A%’;
Ex: Select * from emp where ename like ‘%h’;
Ex: Select * from emp where ename like ‘_k%’;
Ex: Select * from emp where ename like ‘___s%’;
Ex: Select * from emp where ename like ‘S__i%’;
Integrity Constraints in SQL
Integrity means correct data.

Constraint means rules.

Integrity constraints are used to insert the correct data by applying the different rules.

1. Entity Integrity Constraint:


a. Primary Key Constraint: does not accept the duplicate and null values. (null means
empty)
Ex: create table stud(rollno number(2) constraint pk primary key, name char(5));

b. Unique Key: not accept duplicate value but accept null value
Ex: create table stud(rollno number(2) constraint pk primary key, name char(5), aadharID
number(12) constraint uk unique);

2. Domain Integrity Constraint:


a. Not Null: does not accept null value
Ex: create table stud(rollno number(2) constraint pk primary key, name char(5), aadharID
number(12) constraint uk unique, contact number(10) not null);

b. Check: accept the data as per given condition


Check(fees>5000)
Ex: create table stud(rollno number(2) constraint pk primary key, name char(5), aadharID
number(12) constraint uk unique, contact number(10) not null, fees number(5) constraint
chk check(fees>5000));

3. Referential Integrity Constraint:


a. Foreign Key: Primary Key of one table can be referred in another table.
Primary key table is parent table and the table in which primary key is referred is child table.
For foreign key, 2 tables required.
Primary key column should be same (name, datatype, size) in parent and child tables.

Ex: for Foreign key two tables can be created as below


Parent table
Create table stud1(rollno number(3) constraint pk primary key, name char(5), fees
number(5), contact number(10));
Child table
Create table stud2(rollno number(3) references stud1(rollno), class char(4), exam char(5),
admission_year numer(4));

Stud1 (Parent table)


Rollno(PK) Name Fees Contact
1 Anil 1000 99
2
Stud2 (child table)

Rollno (FK) Class Exam Admission_Year


1 SYCM MSBTE 2020-21
2
Assignment
1. State any two Codd's rules. (2)
2. List four DDL commands. (2)
3. List four set operators. (2)
4. Define primary and candidate key. (2)
5. List four datatypes in SQL. (2)
6. Explain primary and foreign key constraints with example. (4)
7. Explain grant and revoke commands with example. (4)
8. Explain different arithmetic operators in SQL. (4)
9. Enlist and explain DML commands with example. (4)
10. Define normalization. Explain 1NF and 2NF. (4)
11. Explain comparison operators with example. (4)

12. Consider the relation: stud (rollno, name, dob, fees, class)
Solve the following:
1. Create stud table by applying primary key.
2. Add new column city in table.
3. Change the name of column class to course.
4. Set the size of rollno column to 5.
5. Delete class column from table.
6. Insert new record in table.
7. Change the name of anil to arun.
8. Remove the record whose fees is 8000.
9. Update the class of suresh to SYCM.
10. Edit the name to Nilesh whose rollno is 5.
11. Remove the record from table whose city is pune.
12. Delete all records from table.
13. Update the fees to 10000 of all students.
14. Change the datatype of class column.
15. Update the city to Mumbai whose city is loni.
13. Consider the relation emp(empid, ename, salary, comm, joindate, contact)
Solve the following:
1. Create above table: empid primary key, contact not null, joindate unique, salary should
be greater than 10000.
2. Display the records whose salary is between 9000 and 15000.
3. Display the records whose annual commission is greater than annual commission of
anil.
4. Display the records whose annual salary is like as suresh.
5. Display the records whose total income is greater than 50000.
6. Display the records whose name’s second letter is n and fifth letter is d.
7. Display the records by decreasing the salary of each by 2000.
8. Display the records by giving increment by 15% in salary.
9. Display the records whose salary is 5000, 7000, 9000, 15000.
10. Display the records who joined before anil.
11. Display the records whose annual salary is greater than 50000 and who joined after
suresh.
12. Display the records whose salary is 50000 or comm is greater than akash.
13. Display the records whose salary is not 15000.
14. Display the records whose salary is between akash and 50000.
15. Display the records whose comm is not like as prakash.

You might also like