introduction to dbms and sql

34
Introduction to DBMS Introduction to DBMS and SQL and SQL GUIDED BY : GUIDED BY : MR. YOGESH SAROJ (PGT- MR. YOGESH SAROJ (PGT- CS) CS) Presented By Presented By : : JAYA XII –COM JAYA XII –COM SEEMA XII-COM. SEEMA XII-COM.

Upload: hailey

Post on 30-Jan-2016

100 views

Category:

Documents


7 download

DESCRIPTION

Introduction to DBMS and SQL. GUIDED BY : MR. YOGESH SAROJ (PGT-CS) Presented By : JAYA XII –COM SEEMA XII-COM. Data Base Management System (DBMS) Data :- Data means raw fact. Database :- A collection of meaningful information. Eg. Telephone directory. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction to DBMS and SQL

Introduction to DBMS and SQLIntroduction to DBMS and SQL

GUIDED BY :GUIDED BY :

MR. YOGESH SAROJ (PGT-CS)MR. YOGESH SAROJ (PGT-CS)

Presented ByPresented By : :

JAYA XII –COM JAYA XII –COM SEEMA XII-COM.SEEMA XII-COM.

Page 2: Introduction to DBMS and SQL

Data Base Management System (DBMS)Data Base Management System (DBMS)

DataData:-:- Data means raw fact. Data means raw fact.

DatabaseDatabase :- :- A collection of meaningful information. A collection of meaningful information. Eg. Telephone directory.Eg. Telephone directory.

Data Base Management SystemData Base Management System:-:-The collection of interrelated data The collection of interrelated data containing information about one particular field & set of programs to containing information about one particular field & set of programs to access those data.access those data.

Manage the whole data in perfect manner for following reasons:Manage the whole data in perfect manner for following reasons: User can search information easilyUser can search information easily User can insert data easily in orderUser can insert data easily in order Updating easilyUpdating easily Deleting information easilyDeleting information easily Save data permanently Save data permanently

Page 3: Introduction to DBMS and SQL

Advantage of DBMSAdvantage of DBMS

1. It reduces data redundancy.1. It reduces data redundancy. 2. It controls data inconsistency to a large 2. It controls data inconsistency to a large

extent.extent. 3. It restricted unauthorized access.3. It restricted unauthorized access. 4. It facilitates sharing of data.4. It facilitates sharing of data. 5. It provides to describe backup & 5. It provides to describe backup &

recovery.recovery.

Page 4: Introduction to DBMS and SQL

We have so many softwares for managing We have so many softwares for managing Database:Database:

DB2 DB2 MS AccessMS Access Fox ProFox Pro SQL ServerSQL Server OracleOracle My SQLMy SQL

Page 5: Introduction to DBMS and SQL

SQLSQL

SQL (Structure Query Language)SQL (Structure Query Language)

It is relational database language that enables It is relational database language that enables you to create and operate on relational you to create and operate on relational database.database.

Page 6: Introduction to DBMS and SQL

Feature of SQLFeature of SQL

It is a non procedural language.It is a non procedural language. It is a 4GL programming language.It is a 4GL programming language.

(i.e only What to do? not How to do?).(i.e only What to do? not How to do?). It is a case insensitive language.It is a case insensitive language.

Page 7: Introduction to DBMS and SQL

Constraints of SQLConstraints of SQL

A A constraintconstraint is a is a conditioncondition or or checkcheck that is applied to that is applied to a column or set of columns in a table. a column or set of columns in a table.

Null ConstraintNull Constraint: : It means a It means a Unknown ValueUnknown Value..

Eg.Eg. mobile number(10) null mobile number(10) null

Not Null ConstraintNot Null Constraint:: It means always a It means always a Known Known ValueValue..

Eg.Eg. name varchar2(20) not null name varchar2(20) not null

Page 8: Introduction to DBMS and SQL

Unique ConstraintUnique Constraint: : It ensures that no two rows have It ensures that no two rows have the same value in the specified column(s). the same value in the specified column(s). i.e Known i.e Known Value (Distinct) or Unknown Value.Value (Distinct) or Unknown Value.

Eg.Eg. ecode number(5) unique ecode number(5) unique

Primary Key ConstraintPrimary Key Constraint: : It is similar to Unique It is similar to Unique constraint except that the Primary Key can not allow constraint except that the Primary Key can not allow Null values so that this constraint must be applied to Null values so that this constraint must be applied to columns declared as Not Null. i.e columns declared as Not Null. i.e Always Known Always Known Value (Distinct)Value (Distinct)..

Eg.Eg. empid char(5) primary key empid char(5) primary key

Page 9: Introduction to DBMS and SQL

Default ConstraintDefault Constraint: : A default value can be A default value can be specified for a column using default clause specified for a column using default clause when a user does not enter a value for that when a user does not enter a value for that column.column.

Eg. Eg. grade char(2) default= ‘E1’grade char(2) default= ‘E1’

Check ConstraintCheck Constraint:: It limits values that can be It limits values that can be inserted into a column.inserted into a column.

Eg.Eg. sal number(10) check(sal > 2000) sal number(10) check(sal > 2000)

Page 10: Introduction to DBMS and SQL

Foreign Key ConstraintForeign Key Constraint: : Whenever two tables are related by a common Whenever two tables are related by a common column then Foreign Key is present in the column then Foreign Key is present in the Child table (Related Table or Child table (Related Table or Detail Table)Detail Table) and it is derived from primary key of and it is derived from primary key of Parent Table (Primary Parent Table (Primary Table or Master Table)Table or Master Table). .

Eg.Eg. Two Tables:Two Tables: ItemsItems ( (ItemnoItemno, Description, Price), Description, Price) OrdersOrders ( (OrdernoOrderno, Orderdate, Itemno, Qty), Orderdate, Itemno, Qty) where where ItemnoItemno & & OrdernoOrderno are Primary Key and Itemno is Foreign Key. i.e are Primary Key and Itemno is Foreign Key. i.e

both the tables are related through common column Itemno.both the tables are related through common column Itemno.

NoteNote:: It may be possible that Primary Key and Foreign Key are same. It may be possible that Primary Key and Foreign Key are same. Eg.Eg. create table Itemscreate table Items ( Itemno char(5) Primary Key,( Itemno char(5) Primary Key, ………………………….);.); create table Orderscreate table Orders ( Orderno number(5) Primary Key,( Orderno number(5) Primary Key, Itemno char(5) references Items(Itemno),Itemno char(5) references Items(Itemno), ………………………….. ););

Page 11: Introduction to DBMS and SQL

Classification of SQL CommandsClassification of SQL Commands

DDL CommandsDDL Commands DML CommandsDML Commands DCL CommandsDCL Commands TCL CommandsTCL Commands Query LanguageQuery Language

Page 12: Introduction to DBMS and SQL

DDL CommandsDDL Commands

DDL (Data Definition Language):DDL (Data Definition Language): It It provides commands for defining various provides commands for defining various database objects (i.e defining relation schemas, database objects (i.e defining relation schemas, deleting relations, creating indexes, and deleting relations, creating indexes, and modifying relation schemas etc.)modifying relation schemas etc.)

Eg.Eg. Create, Alter, Drop etc. Create, Alter, Drop etc.

Page 13: Introduction to DBMS and SQL

Create CommandCreate Command The tables are created by using The tables are created by using CreateCreate TableTable command and also its columns are command and also its columns are

named, data types and sizes are supplied for each column.named, data types and sizes are supplied for each column. SyntaxSyntax:: create table <table_name>create table <table_name> (( <col1> <datatype> [<size>] <col1> <datatype> [<size>]

[<constraint>],[<constraint>], <col2> <datatype> [<size>] <col2> <datatype> [<size>]

[<constraint>],[<constraint>], ……………….... <coln> <datatype> [<size>] <coln> <datatype> [<size>]

[<constraint>][<constraint>] );); Eg.Eg. create table emp1create table emp1 (( empid char(4)empid char(4) primary key,primary key, ename varchar2(20) not null,ename varchar2(20) not null, sal number(5) check(sal>2000)sal number(5) check(sal>2000) ););

Page 14: Introduction to DBMS and SQL

EmpidEmpid EnameEname SalSal

E001E001 SmithSmith 50005000

E002E002 JohnJohn 1000010000

E003E003 JamesJames 25002500

Page 15: Introduction to DBMS and SQL

Alter CommandAlter Command Altering TableAltering Table: : The alter table command is used to modify The alter table command is used to modify

the structure of existing table. (i.e adding a column, add an the structure of existing table. (i.e adding a column, add an integrity constraint etc.).integrity constraint etc.).

Adding Columns:Adding Columns: The new column will be added with NULL The new column will be added with NULL values for all rows currently in table.values for all rows currently in table.

SyntaxSyntax:: alter table <table_name>alter table <table_name> add (<col1> <datatype> <size> [<constraint>],add (<col1> <datatype> <size> [<constraint>], <col2> <datatype> <size> <constraint>] <col2> <datatype> <size> <constraint>]

…….); …….); Eg.Eg. alter table empalter table emp add (tel_number number(11) );add (tel_number number(11) );

Page 16: Introduction to DBMS and SQL

Alter tableAlter table

Modifying Column DefinitionsModifying Column Definitions:: To change To change datatype, size, default value and NOT NULL datatype, size, default value and NOT NULL column constraint of a column definition.column constraint of a column definition.

SyntaxSyntax::alter table <table_name>alter table <table_name> modify (<col_name> <new_datatype> modify (<col_name> <new_datatype>

<new_size> );<new_size> ); Eg.Eg. alter table empalter table emp modify (tel_number number(13) );modify (tel_number number(13) );

Page 17: Introduction to DBMS and SQL

Drop tableDrop table

Drop Table Command: Drop Table Command: It removes a table It removes a table from the database .from the database .

SyntaxSyntax:: drop table <table_name>;drop table <table_name>;

Eg.Eg. Drop table emp; Drop table emp;

Page 18: Introduction to DBMS and SQL

DMLDML

((Data Manipulation LanguageData Manipulation Language): ): It enables It enables users to manipulate data (i.e commands to users to manipulate data (i.e commands to insert, delete, and modify tuples in the insert, delete, and modify tuples in the database).database).

Eg.Eg. Insert, Update, Delete etc. Insert, Update, Delete etc.

Page 19: Introduction to DBMS and SQL

Insert tableInsert table Inserting Data into TableInserting Data into Table The data can be inserted in a table using The data can be inserted in a table using Insert IntoInsert Into command. command. SyntaxSyntax:: insert into <table_name> [<column_lists>]insert into <table_name> [<column_lists>] values (<value1>, <value2>, …………….);values (<value1>, <value2>, …………….);

Eg.Eg. insert into emp1insert into emp1 values(‘E001’,’Vipin’,5000);values(‘E001’,’Vipin’,5000); NoteNote:: Here the order of values matches the order of columns Here the order of values matches the order of columns

in the create table command of the table.in the create table command of the table.

OrOr insert into emp1 (empid, ename, sal)insert into emp1 (empid, ename, sal) values(‘E001’,’Vipin’,5000);values(‘E001’,’Vipin’,5000);NoteNote:: The columns not listed in the insert into command will The columns not listed in the insert into command will

have their default values or null values.have their default values or null values.

Page 20: Introduction to DBMS and SQL

Insert TableInsert Table Mass Level Data InsertionMass Level Data Insertion If you want to insert data from user online then you can use insertion If you want to insert data from user online then you can use insertion

through substitution of parameters.(i.e & as substitution operator or place through substitution of parameters.(i.e & as substitution operator or place holder).holder).

SyntaxSyntax:: insert into <table_name>insert into <table_name> values(‘&col1’,’&col2’,………);values(‘&col1’,’&col2’,………); NoteNote:: The parameters for character values are enclosed in single quote. The parameters for character values are enclosed in single quote.

Eg.Eg. insert into emp1insert into emp1 values(‘&empid’,’&ename’,&sal);values(‘&empid’,’&ename’,&sal);

It will asked from you to Enter value for empid, Enter value for ename and It will asked from you to Enter value for empid, Enter value for ename and Enter value for sal. Enter value for sal.

Then a message display that 1 row created.Then a message display that 1 row created.

Page 21: Introduction to DBMS and SQL

Update tableUpdate table Modifying Data with Update CommandModifying Data with Update Command This is a DML statement used to modify or change some or all of the This is a DML statement used to modify or change some or all of the

values in an existing row of a table.values in an existing row of a table. SyntaxSyntax: : update <table_name>update <table_name> set col1 = <new_value>,set col1 = <new_value>, col2 = < new_value>,col2 = < new_value>, ……....coln = <new_value>coln = <new_value> [where <condition>];[where <condition>];

Eg.Eg. update empupdate emp set sal= 400;set sal= 400; ‘updates all rows‘updates all rows

Eg.Eg. update empupdate emp set sal= sal*2, ename= ‘JONES’set sal= sal*2, ename= ‘JONES’ where empno = 7844;where empno = 7844; ‘update only one row‘update only one row

Page 22: Introduction to DBMS and SQL

Delete CommandDelete Command This is also a DML statement used to remove This is also a DML statement used to remove

row(s) of a table.row(s) of a table. SyntaxSyntax:: delete from <table_name>delete from <table_name> [where <condition>];[where <condition>];

Eg.Eg. delete from empdelete from emp where sal < 5000;where sal < 5000;

Page 23: Introduction to DBMS and SQL

DCL (Data Control Language) DCL (Data Control Language) CommandsCommands

The rights or permissions assigned to user(s) to use some or all of Oracle objects are The rights or permissions assigned to user(s) to use some or all of Oracle objects are known as privileges.known as privileges.

Granting PrivilegesGranting Privileges:: It is used to assigning permissions to users. (Only DBA can It is used to assigning permissions to users. (Only DBA can assign)assign)

SyntaxSyntax:: grant <permissions>grant <permissions> ‘‘select, insert, delete, updateselect, insert, delete, update on <object_name>on <object_name> to <username>;to <username>;

Eg.Eg. grant insert on emp to user1;grant insert on emp to user1; ‘‘only user1 can insertonly user1 can insert grant all on emp to public;grant all on emp to public; ‘‘assign all permissions to all usersassign all permissions to all users..

Revoking PrivilegesRevoking Privileges:: It get back permissions from the users. It get back permissions from the users. SyntaxSyntax:: revoke <permission> on <object_name> from <username>;revoke <permission> on <object_name> from <username>;

Eg.Eg. revoke all on emp from user1;revoke all on emp from user1; ‘‘get back all permissions from get back all permissions from user1.user1.

revoke select on emp from public;revoke select on emp from public; ‘‘get back select permission from all get back select permission from all users.users.

Page 24: Introduction to DBMS and SQL

TCLTCL

((Transaction Control LanguageTransaction Control Language): ): It controls It controls over transaction processing by specifying the over transaction processing by specifying the beginning and ending of transactions.beginning and ending of transactions.

Eg.Eg. Commit, Rollback, Rollback to, Save Commit, Rollback, Rollback to, Save point etc.point etc.

Page 25: Introduction to DBMS and SQL

TCL CommandsTCL Commands Oracle treat a transaction as a single entity & incase of successful Oracle treat a transaction as a single entity & incase of successful

termination of transaction the changes are made permanent. The termination of transaction the changes are made permanent. The commands used with transactions are:commands used with transactions are:

COMMIT:COMMIT: It ends the current transaction by saving database changes It ends the current transaction by saving database changes & starts a new transaction. & starts a new transaction.

Eg.Eg. commit; commit; ‘‘i.e end or start a transactioni.e end or start a transaction

ROLLBACK:ROLLBACK: It ends the current transaction by discarding database It ends the current transaction by discarding database changes & starts a new transaction. changes & starts a new transaction.

Eg.Eg. rollback; rollback; ‘‘i.e undo upto commiti.e undo upto commit

SAVEPOINT:SAVEPOINT: It defines breakpoints or bookmarks for the transaction It defines breakpoints or bookmarks for the transaction to allow partial rollbacks. to allow partial rollbacks.

Eg.Eg. savepoint P1; savepoint P1;

ROLLBACK TOROLLBACK TO:: Its undo up to given bookmark or breakpoint. Its undo up to given bookmark or breakpoint.Eg.Eg. rollback to P1; rollback to P1;

Page 26: Introduction to DBMS and SQL

Query LanguageQuery Language Query languageQuery language : The : The SelectSelect command of SQL make queries command of SQL make queries

on the database i.e it is given to produce certain specified on the database i.e it is given to produce certain specified information from the database table(s).information from the database table(s).

There are various ways and combinations to use a select There are various ways and combinations to use a select statement.statement.

The The complex syntax of SQL Select commandcomplex syntax of SQL Select command is as given below: is as given below: Select <column_list>Select <column_list> From <table_name>From <table_name> Where <condition>Where <condition> Group By <list_of_column(s)>Group By <list_of_column(s)> Having <search_condition>Having <search_condition> ‘‘Having is dependent upon Group Having is dependent upon Group

ByBy Order By <column_name>;Order By <column_name>;

Page 27: Introduction to DBMS and SQL

Select CommandSelect Command

Select * from emp;Select * from emp; Select 2+3 from dual;Select 2+3 from dual; Select empid, ename from emp;Select empid, ename from emp; Select * from emp where ename=‘smith’;Select * from emp where ename=‘smith’; Select distinct sal from emp;Select distinct sal from emp; Select job from emp where deptno= (select Select job from emp where deptno= (select

deptno from emp where ename=‘SMITH’);deptno from emp where ename=‘SMITH’);

Page 28: Introduction to DBMS and SQL

Select CommandSelect Command

Eg.Eg. select sum(sal), deptnoselect sum(sal), deptno from empfrom emp group by deptno;group by deptno;

Output:Output: SUM(SAL) DEPTNOSUM(SAL) DEPTNO --------- ---------------------------- ------------------- 8750 108750 10 10875 2010875 20

94009400 30 30

Page 29: Introduction to DBMS and SQL

Select CommandSelect Command

Eg.Eg. select sum(sal), deptnoselect sum(sal), deptno from empfrom emp group by deptnogroup by deptno having deptno= 30;having deptno= 30; Output:Output: SUM(SAL) DEPTNO SUM(SAL) DEPTNO -------- ---------------------------- -------------------- 9400 309400 30

Page 30: Introduction to DBMS and SQL

Select CommandSelect Command

Eg.Eg. select sum(sal), deptnoselect sum(sal), deptno from empfrom emp group by deptnogroup by deptno order by deptno desc;order by deptno desc; Output: Output: SUM(SAL) DEPTNO SUM(SAL) DEPTNO --------- ----------------------------- -------------------- 9400 309400 30 10875 2010875 20 8750 108750 10

Page 31: Introduction to DBMS and SQL

QuestionsQuestions Ques1. What is difference b/w having clause and Ques1. What is difference b/w having clause and

where clause in select query?where clause in select query?

Ques2. What is difference b/w delete and drop Ques2. What is difference b/w delete and drop command?command?

Ques3. Write a query to select second largest salary Ques3. Write a query to select second largest salary from emp table?from emp table?

Page 32: Introduction to DBMS and SQL

QuestionsQuestions

Ques4. What is the output of following query?Ques4. What is the output of following query?

select 2+3 from emp;select 2+3 from emp;

Ques5. Give three application areas of Ques5. Give three application areas of Database.Database.

Ques 6: What is the difference b/w unique and Ques 6: What is the difference b/w unique and primary key constraint?primary key constraint?

Page 33: Introduction to DBMS and SQL

Queries?Queries?

Page 34: Introduction to DBMS and SQL

ThanksThanks