DMS Unit 2 22319-1
DMS Unit 2 22319-1
Key Concepts:
Student database
1. Primary key: contains unique values and does not allow the null value(s).
Ex: rollno
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
Normalization: is process of decomposition of database to arrange the data and reduce the
duplicate records. (1NF, 2NF, 3NF)
EMPLOYEE table:
The decomposition of the EMPLOYEE table into 1NF has been shown below:
TEACHER_SUBJECT table:
tid subject
25 Chemistry
25 Biology
47 English
83 Math
83 Computer
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
Non-prime attributes: In the given table, all attributes except EMP_ID are non-
prime.
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:
EMPLOYEE_ZIP table:
Datatypes are the variables which accepts the specific values. (ex. number, alphabets, alphanumeric,
etc)
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
These commands are used to create and modify the tables in DB.
Ex: Select rollno, name from stud1; (displays only rollno and name)
Grant succeeded.
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
Table created.
Revoke succeeded.
User dropped.
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
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:
Logical Operators:
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’
Ex: Select * from emp where salary between 3000 and 5000;
Ex: Select * from emp where salary not between 3000 and 5000;
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.
Integrity constraints are used to insert the correct data by applying the different rules.
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);
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.