0% found this document useful (0 votes)
28 views57 pages

Exp1 1

The document outlines the execution of various SQL commands (DDL, DML, DCL, and TCL) on a relational schema for employee data. It details different types of SQL commands, data types, and operators, along with examples of how to use them effectively. Additionally, it provides instructions for granting and revoking permissions in a database context.

Uploaded by

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

Exp1 1

The document outlines the execution of various SQL commands (DDL, DML, DCL, and TCL) on a relational schema for employee data. It details different types of SQL commands, data types, and operators, along with examples of how to use them effectively. Additionally, it provides instructions for granting and revoking permissions in a database context.

Uploaded by

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

DBMS LAB

EXPERIMENT-1
Execute DDL, DML, DCL and TCL Commands on below given
relational schema. EMP(Empno, Ename, Job, Salary, Mgr, Comm,
Hiredate, Deptno).

1
• Relational schema: A relational schema contains
the name of the relation and name of all columns or
attributes.
• Relational key: In the relational key, each row has
one or more attributes. It can identify the row in the
relation uniquely.

2
TYPES OF KEYS
 Primary key
 The PRIMARY KEY constraint uniquely
identifies each record in a table.
 Primary keys must contain UNIQUE
values, and cannot contain NULL values.
 {ID} or {SSN}
 It is chosen with care by DBA {ID}
DBMS software installation
Oracle 10 g XE download video
video link
Software download link https://bit.ly/2Rj7Ysc

5
DBMS software installation
Oracle video
installation link

6
Commands in SQL
VARIOUS COMMANDS OF SQL
SQL commands can be grouped into the following types based on their purpose

Data Definition Language (DDL) commands

Data Manipulation Language (DML) commands.

Data Control Language (DCL) commands.

Transaction Control Language (TCL) commands.

7
Commands in SQL

 Data definition language commands.

 Data manipulation language commands.

 Transaction control commands.

 Data control commands

8
• Different data types in SQLdata types

1) Numeric Data Types inSQL:

o INTEGER – Stores integer number, values from -2,147,483,648


to2,147,483,647.
o SMALLINT – Stores small integer number, values from -32,768
to32,767.
o TINYINT – Stores tiny integer number, values from 0 to255.
o NUMERIC(P,S) – Stores values from -10^38 +1 to 10^38 -1.
Where ‘p’ isprecision value and ‘s’ is scalevalue.
o REAL – Single precision floating point number, stores values from
-3.40E + 38 to3.40E o + 38.
o DECIMAL(P,S) – Stores values from -10^38 +1 to 10^38 -1,
where ‘p’ is precisionvalue and ‘s’ is scalevalue.
o FLOAT(P) – Stores values from -1.79E + 308 to 1.79E + 308,
where ‘p’ isprecision value.
o DOUBLE PRECISION – Double precision floating pointnumber.
o BIT(X) – Where ‘x’ is the number of bits to store, stores value from
0 to1.
o BIT VARYING(X) – ‘X’ is the number of bits to store
(length can vary up tox). 9
2) Character DataTypes:

o CHAR(X) – Where ‘x’ is the character’s number to store. It can store


upto 8,000 characters.
o VARCHAR2(X) – Where ‘x’ is the character’s number to store. It can
store upto8,000 characters.
o VARCHAR(MAX) – It can store upto 231characters.
o text – Can store upto 2,147,483,647characters.

3) Binary Data Types inSQL:

o binary – Can store upto 8,000 bytes (Fixed-length binarydata).


o varbinary – Can store upto 8,000 bytes. (Variable length
binarydata).
o varbinary(max) – Can store upto 231 bytes (Variable length
Binarydata).
o image – Can store upto 2,147,483,647 bytes. (Variable length
BinaryData).

10
4) Date and Time Data Types inSQL:

o DATE – Stores month, days and yearvalues.


o TIME – Stores hour, minute and secondvalues.
o TIMESTAMP – It stores year, month, day, hour, minute and second
values.

11
Operators
1) ArithmeticOperators: Arithmetic operators are used to
perform mathematical calculations like addition, subtraction,
multiplication, division and modulus in SQL statements.

Arithmetic Operators

+ (Addition) A+B
– (Subtraction) A-B
* (multiplication) A*B
/ (Division) A/B
% (Modulus) A%B

12
2) RelationalOperators: Relational operators in SQL are used to find
the relation between two columns. i.e. to compare the values of two
columns in SQL statements.

x > y (x is greater than y)


x < y (x is less than y)
x >= y (x is greater than or equal to y)
x <= y (x is less than or equal to y)
X<= y (x is less than or equal to y)
!= or <> x != y or x <> y (x is not equal to y)
!< x !< y (x is not less than y)
!> x !> y (x is not greater than y)

13
3. LogicalOperators: Logical operators in SQL are used to
perform logical operations on the given expressions in SQL
statements. There are many operators in SQL which are
used in SQL statements in the WHERE clause. They are,
AND
OR
NOT
BETWEEN…AND
IS NULL, IS NOTNULL
LIKE
UNIQUE

14
DDL Commands

Data Definition Language (DDL) commands

As the name DDL suggests, these commands allow you


to perform task related to data definition.

The DDL commands are CREATE, ALTER, DROP,


TRUNCATE etc.

15
DDL commands Contd

CREATE

USAGE: It is used to create a particular table.

SYNTAX: Create table <tablename> (<columnname1> <data


type(size)>,<columnname2> <data type(size)>,…<columnname-
N> <data type(size)>);

EXAMPLE:

create table Student(pin char(11),sname varchar2(10),marks


number(3),Address varchar2(30));
16
DDL commands Contd
Example:

CREATE TABLE Persons (ID int NOT NULL, LastName varchar(25)


NOT NULL,FirstName varchar(255),Age int, PRIMARY KEY (ID));

17
DDL Commands Contd…
ALTER
USAGE: By using this command you can modify the table
structure.
The ALTER TABLE statement can:
 Add a column definition to a table.

 Modify a column data type or size.

 Rename column name and table name.

 Drop a column from a table.


.
18
DDL Commands Contd…
 Add or Drop a primary key to /from a table.

 Add or Drop a foreign key to / from a table.

 Add or Drop a unique constraint to /from a table.

 Add or Drop a check constraint to / from a table

19
DDL Commands Contd…
SYNTAX:-

ALTER TABLE tablename


[ ADD column-definition ]
[ MODIFY column-definition ]
[DROP column column_name ]
[ ADD primary key-definition ]
[ ADD foreign key-defintion ]
[ ADD unique-constraint ]
[ ADD check-constraint ]
[ DROP CONSTRAINT constraint_name ]

20
DDL Commands Contd…
Adding Column to existing table:
ALTER table student ADD phone char(10);

Modifying column data type and size:


ALTER table student MODIFY phone number(10);

21
DDL Commands Contd…
Rename a column name of table:
ALTER table student RENAME COLUMN phone to mobile;

Rename table name:


ALTER table student RENAME TO student1;

Drop column from a table:


ALTER table student1 DROP COLUMN mobile;

22
DDL commands Contd…
Adding constraint to a particular column:
ALTER TABLE student1 ADD CONSTRAINT std_pk PRIMARY KEY(PIN);
(or)
ALTER TABLE student1 ADD PRIMARY KEY(PIN);

Drop constraint example:


ALTER table student1 DROP CONSTRAINT std_pk.
(or)
ALTER table student1 DROP PRIMARY KEY;

23
DDL commands Contd…
Adding constraint to a particular column:

ALTER TABLE Orders ADD FOREIGN KEY (PersonID)


REFERENCES Persons(PersonID);
(or)
ALTER TABLE Orders
ADD CONSTRAINT FK_PersonOrder
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);
Drop constraint example:
ALTER table student1 DROP CONSTRAINT std_pk.
(or)
ALTER table student1 DROP PRIMARY KEY;

24
DDL Commands Contd…

DROP
USAGE: It is used to remove the table from the database.

SYNTAX: drop table tablename;

EXAMPLE: drop table student;

25
DDL Commands Contd…

Truncate:

USAGE: The SQL TRUNCATE TABLE command is


used to delete complete data from an existing table.

SYNTAX: truncate table tablename;

EXAMPLE: truncate table student;

26
DML Commands

Data Manipulation Language (DML) command


DML commands is used to manipulate data.

DML commands are


 Insert
 Select
Update
Delete

27
DML Commands contd…

INSERT
It is used to insert the data into a table.
Syntax:
insert into table_name values(value1,value2,….value-n);
Example:
insert into student values(‘CS102’,’sri’,990);

28
DML Commands contd…

INSERT DATA IN SPECIFIED COLUMNS


SYNTAX
insert into table name(col1,col2,…coln)
values ( value1,value2….valuen);

Example: insert into student(pin,name,marks)


values(‘CS001’,’Rajesh’,980);

29
DML Commands contd…

SELECT
The select statement is used to select data from a table.
Syntax: SELECT [ALL/DISTINCT]
Column name(s)
FROM table(s)
[WHERE conditional expression]
[GROUP BY column(s)]
[HAVING conditional expression]
[ORDER BY column(s)];

30
DML Commands contd…

SELECT ALL rows and columns


Example:
select * from student;

SELECT selected columns and all rows


Example:
select pin,sname from student;

31
DML Commands contd…

Distinct
The distinct key word is used to return only distinct
(different values).

Syntax: select distinct column name(s) from tablename;


Question: write a sql query for displaying names of students
with out duplicates.
Example: select DISTINCT sname from student;

32
DML Commands contd…

WHERE
It is used to specify a selection criterion to conditionally
select data from a table a where clause can be added to the select
statement.
Syntax: select column-name from table-name where column-
name operator value;

33
DML Commands contd…

Examples:-
SELECT all columns and selected ROWS

select * from STUDENT where sname=’MAHESH’;

SELECT selected rows and columns:

select sname from student where pin=110;

34
DML Commands contd…

ORDER BY:
order by clause displays the table data in proper order (i.e.
Ascending or descending).
order by clause by defaults gives ascending order.

Ex:- select * from student order by sname;


Ex:- select * from student order by sname desc;

35
DML Commands contd…

GROUP BY

The GROUP BY clause is used in a SELECT statement to


collect data across multiple records and group the results by one or
more columns. Related rows can be grouped together by the
GROUP BY clause by specifying a column as a grouping column.
Example:
SELECT COUNT(CustomerID), Country FROM Customers
GROUP BY Country;
example
36
37
DML Commands contd…
HAVING

The HAVING is used along with the GROUP BY clause.


HAVING imposes a condition on the GROUP BY clause, which
further filters the groups created by the GROUP BY clause.
Example:

SELECT COUNT(CustomerID), Country FROM Customers

GROUP BY Country having count(customerID)>2;

38
39
DML Commands contd…

UPDATE: is used to update data in a table.

UPDATE ONE COLUMN IN A ROW


USAGE: It is used to modify the data in a one column of table.

SYNTAX: update tablename set columnname=new-value


where columnname=value;
Example:
update student set name=’sujith’ where name=’subbu’;

40
DML Commands contd…
UPDATE SEVERAL COLUMNS IN A ROW

USAGE: It is used to update multiple columns at a time based on


condition.

SYNTAX: update table name set columnname1 = value1,


columnname2=value2…..,columnnameN =valuen where
columnname= value;

EXAMPLE: update student set name=’satish’,marks=890 where


pin=‘CS001’;
41
DML Commands contd…
Update all records or Rows in a table:

Syntax: update tablename set columnname=value;

Example: update student set pin=‘cs-047’;

42
DML Commands contd…

DELETE
Usage: it is used to delete rows from a table based on
condition.

Syntax: DELETE from tablename where condition;

Example: DELETE from student where pin =‘CS- 047’;

43
DML Commands contd…

Delete all rows from table

Syntax: delete from tablename;

Example: delete from student;

44
Data Control Language (DCL)
DCL commands mainly deals with the rights , permissions , and other
controls of the database system.
List of DCL commands :
1.GRANT – this command gives users access privileges to the database.
2.REVOKE – this command withdraws the users access privileges given by
the GRANT command.
•These permissions are given by DBA.
•DCL commands interact with database directly.
•Cannot undo/rollback the changes.
•No buffer involvement, hence they are faster in response.
Syntax:
Grant privilege_name on object_name to {usesr_name};

45
Data Control Language (DCL)

DCL commands mainly deals with the rights , permissions , and other
controls of the database system.
List of DCL commands :
1.GRANT – this command gives users access privileges to the database.
2.REVOKE – this command withdraws the users access privileges given by the
GRANT command.
•These permissions are given by DBA.
•DCL commands interact with database directly.
•Cannot undo/rollback the changes.
•No buffer involvement, hence they are faster in response.
Syntax:
Grant privilege_name on object_name to {usesr_name};

46
Data Control Language (DCL)

 System level privileges: Available only to administrators and application


developers
 Object level privileges: Allow users to perform particular task on a
specific database object.
 Object privileges are granted to end users so that they can use a database
application to accomplish specific task.
 Example: select privilege on emp table
delete privilege on student table
DCL commands
 Grant
 Revoke
 Set role

47
Grant
 Q1:Grant select permission on emp table to other user abc.
 Step 0:login to system account
 Step1:create table emp(eid number,ename varchar2(20),sal
number,dob date);
 Step 2:insert into emp values(:eid,:ename,:sal,:dob);
 Step 3:insert some records.
 Step 4:select * from emp;
 Step 5:Grant select on emp to abc;
 Step 6:logout from system account
 Step 7:login to abc account
 Step 8:select * from system.emp;
 Step9:verify---insert into system.empvalues(:eid,:ename,:sal,:dob);

48
Grant
Q2:Grant all previleges on emp to other user abc
Step1:grant all on emp to abc
Step 2:verify-- insert into system.emp values(:eid,:ename,:sal,:dob);

Q3:Grant privileges(single/multiple) on single table to multiple users


Step 1:grant select on emp to abc,xyz;
Step 2:grant select,update on emp to abc,xyz;
Step 3:grant all on emp to abc,xyz;

Q4:Grant privileges(single/multiple) on multiple table to single user.(Not valid)


Step 1:grant select,update on emp,dept to abc---X

Q5:Grant privileges(single/multiple) on multile table to multiple users.(Not valid)


Step 1:grant select, update on emp,dept to abc,xyz----X

49
Revoke
Q1:revoke select privilege on emp from iln(system account)
Step1:revoke select on emp from abc;
Step 2:verify---select * from system.emp(abc account)

Q2:revoke multiple privileges (system account)


Step 1:revoke select,update from abc;(system account)

Q3:revoke all privileges on emp from abc(system account)


Step 1:revoke all on emp from abc;(system account)

50
Set role
Problem with grant
We can not give privileges on multiple objects to single
user/multiple user
Example: Grant select on emp,dept to abc(system account)---X (not possible)

Grant select on emp to abc ;


Grant select on dept to abc;

Assigning a specific role to end users.


Role means set of privileges on set of tables.
Instead of giving single privilege at a time we can give set of
privileges with the help of role.
51
Set role

SSE-100 privileges on 80 objects


SE-75 privileges on 50 objects
JSE-50 privileges on 30 objects
Freshers-30 privileges on 10 objects
52
Set role

Query 1:create role r1 Or


Query 2:create role SSE;
Query 3:create role r1 identified by password;
•Creating role with password
Query 4:create role r1 not identified;
•creating role without password
Query 5:grant p1,p2,p3….pn on table to r1;(Always remember we
grant privileges on one object at a time)
Ex: grant select on emp to r1;
Grant update on emp1 to r1;
Query 6:grant role r1 to u1;--single user
Query 7:grant role r1 to u1,u2,..un;--multiple users
Query 8:alter role r1 identified by password;
Query 9:alter role r1 not identified;
Query 10:alter user u1 default role r1;

53
Transaction Control Language (TCL)
List of TCL commands :
1.COMMIT – commits a transaction.
2. ROLLBACK – rollbacks a transaction in case of any error
occurs.
3. SAVEPOINT – sets a save point within a transaction.

COMMIT : Commit command is used to save all transactions to the


database.
Example:

insert into emp values(7782,'clark','manager',20000,7839,1500,'9-jan-81',40);


insert into emp values(7934,'miller','clerk',1300,7782,0,'23-jan-82',10);
Commit;

54
Transaction Control Language (TCL)

ROLLBACK : It is used to undo transactions that have not


already been saved to the database.
Example:
insert into emp values(7782,'clark','manager',20000,7839,1500,'9-jan-81',40);
insert into emp values(7934,'miller','clerk',1300,7782,0,'23-jan-82',10);
Commit;
insert into emp values(7783,‘rahul','manager',20000,7839,1500,'9-jan-81',40);
insert into emp values(7935,'mill','clerk',1300,7782,0,'23-jan-82',10);
Rollback;

55
Transaction Control Language (TCL)

SAVEPOINT : It is used to roll the transaction back to a certain


point without rolling back the entire transaction.
Example:
insert into emp values(7902,'ford','analyst',30000,7566,0,'3-dec-91',10);
insert into emp values(7900,'james','clerk',3000,7698,100,'4-nov-81',30);
savepoint A;
Savepoint created.
insert into emp values(7876,'adems','accounting',20000,7546,1000,'5-nov-
81',10);
savepoint B;
Savepoint created.
insert into emp values(7844,'tunner','salesman',2000,7698,0,'5-jan-82',10);
rollback to savepoint B;
Rollback complete.
56
Thank you

57

You might also like