Database (Joining, Trigger & Cursor)
15-02-2019
DATABASE
It Is a collection of interrelated data
Data-it is unorganized ,unstructured , unmanaged fact & figure.
Information-It is organized, structured ,managed fact & figure.
DBMS(Database Management System)
IT is a software package which is used to managed & Create database.
Example:-dbase ,DB1,DB2, MS-acess oracle ,PostgreSQL,
SQL –SERVER ,SQL lite Ingress etc.
Type of DBMS based on model
• RDBMS-Ms Access, Oracle ,MYSql etc
• Hierarchical DBMS
• Network database Management System
• Object-oriented Database management System.
TYPE OF KEYS
• Super Keys-Group of attribute which are uniquely identify each record.
• Primary key-Those attribute which are uniquely identify each record and does not
store null value.
• Candidate keys-The minimal set of attribute which can uniquely identify each
tuple. Each table may have one or more candidate keys. But one candidate key is
unique. And it is called the primary keys.
• Foreign Key- It should be primary key of another table used to establish
relationship between two and more tables. It based on referential integrity .
• Alternative keys- After selecting candidate keys, The rest keys is known as
Alternative keys.
SQL(Structured query language)
• DDL(Data Definition language)
• Create, Alter, Truncate , Drop, Rename
• DML(Data Manipulation Language)
• Insert , Update, Select , Delete , move
• DCL(Data Control Language)
• Grant Revoke
• TCL(transaction Control Language)
Commit Rollback Save point
SQL query example
1.SELECT * FROM student WHERE S_ID=`100`;
2.SELECT * FROM student WHERE course=`BCA` AND city=``Delhi`;
3. 2.SELECT * FROM student WHERE course=`BCA` OR city=``Delhi`;
4.SELECT * FROM student ORDER BY course ASC,City DESC;
SQL EXAMPLE
• INSERT INTO STUDENT
• (S_ID, S_NAME,S_COURSE,S_PHN NO)
• VALUES(`100`,`vikash``MBA``998765123`);
• UPDATE student
• SET s_name=`vikash`
• WHERE PHN NO=`987617717` ;
SOME EXMPLE(using function)
• SELECT COUNT(S_ID)
• FROM student ;
Some example using LIKE operator
• SELECT *FROM student
• WHERE S_name LIKE `%vk%`;
• SELECT * FROM STUDENT
• WHERE CITY IN(`DELHI`.`PATNA`);
Example of DDL
• CREATE TABLE Student
(S_ID int NOT NULL,
Name varchar(255) NOT NULL,
Course varchar(200), NOT NULL);
• DROP table student;
• AlTER TABLE Student
DROP COLUMN DOB;
Relational Algebra
• It is a procedural language Which take instances of relation as input and give
occurrence of relation as output .it uses various operation to perform this action
• Type of operator-
• Select
• Projection
• Union
• Set different
• Cartesian product
• Rename
Type of relational operator
• SELECT (σ)
• The SELECT operation is used for selecting a subset of the tuples according to a given selection condition. Sigma(σ)Symbol denotes it.
• Example-
•
• Output − Selects tuples from books where subject is 'database'
σsubject = "database"(Books)
LEVEL OF DATA BASE
SQL JOIN
• A JOIN clause is used to combine rows from two or more tables, based on a related column between them.
• different type of SQL JOINs
• 1.INNER JOIN
• 2.LEFT(OUTER) JOIN
• 3.RIGHT(OUTER) JOIN
• FULL(OUTER) JOIN
INNER JOIN
• Return records that have matching value in both table
• Syntax-
• SELECT column_name(s)FROM table1INNER JOIN table2ON table1.column_name = table2.column_name;
•
LEFT(OUTER) JOIN
• The LEFT OUTER keywords returns all records from left table and the matches records from the right table.
• Syntax-
• SELECT column_name(s)FROM table1LEFT JOIN table2ON table1.column_name = table2.column_name;
•
RIGHT(OUTER) JOIN
• RIGHT(OUTER) keywords returns all records from the right table and matched records from the left table.
• Syntax-
• SELECT column_name(s)FROM table1RIGHT JOIN table2ON table1.column_name = table2.column_name;
FULL(OUTER) JOIN
The FULL OUTER JOIN keywords returns all records when there is matching in either left table or right table.
Syntax-
SELECT column_name(s)FROM table1FULL OUTER JOIN table2ON table1.column_name = table2.column_name;
TRIGGER IN DBMS
• Trigger in DBMS is a special kind of procedural that automatically executed when an event(INSERT,UPDATE,DELETE) occurs in the database server.
• Trigger executes when a user tries to modify data through a DML event such as Insert, Delete, Update.
Syntax of trigger
• CREATE or REPLACE TRIGGER display_marks_changesBEFORE DELETE OR INSERT OR UPDATE ON studentsFor EACH ROWWHEN(NEW.ID > 0)DECLARE
Marks_diff number;BEGIN
Marks_Diff:= :New. Marks - :Old. Marks;Dbms_output.put_line (`old Marks` : || :OLD. Marks);Dbms_output.put_line (`New Marks` : || :New. Marks);Dbms_output.put_line (`Marks difference` : || :Marks_diff);
END;
Cursor
• A cursor is a pointer to this context area
• PL/SQL control context the through a cursor.
• A cursor holds the rows return by a SQL Statement.
• Context area-Oracle Create a memory area as the context area, for processing an SQL statement which contains all the information needed for processing the statement:
• example-no of rows processed etc.
Type of cursor
• Implicit cursor
• IT is automatically created by oracle whenever an SQL Statement is executed.
• Explicit Cursor
• IT is a programmer-defined cursor for gaining more control over the context area.
Cursor Action-----
• Declare - Cursor is declare by defining the SQL
• Open- A cursor is opened and populated by executing the SQL
• Fetch- When the cursor is opened rows can be fetched from the cursor one by one
• Close- After data manipulation close the cursor
• Deallocate- finally delete the cursor and release all the system
General syntax of cursor-
CURSOR cursor_name IS Select _Statement.