Exp1 1
Exp1 1
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
8
• Different data types in SQLdata types
10
4) Date and Time Data Types inSQL:
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.
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
15
DDL commands Contd
CREATE
EXAMPLE:
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.
19
DDL Commands Contd…
SYNTAX:-
20
DDL Commands Contd…
Adding Column to existing table:
ALTER table student ADD phone char(10);
21
DDL Commands Contd…
Rename a column name of table:
ALTER table student RENAME COLUMN phone to 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);
23
DDL commands Contd…
Adding constraint to a particular column:
24
DDL Commands Contd…
DROP
USAGE: It is used to remove the table from the database.
25
DDL Commands Contd…
Truncate:
26
DML Commands
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…
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…
31
DML Commands contd…
Distinct
The distinct key word is used to return only distinct
(different values).
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
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.
35
DML Commands contd…
GROUP BY
38
39
DML Commands contd…
40
DML Commands contd…
UPDATE SEVERAL COLUMNS IN A ROW
42
DML Commands contd…
DELETE
Usage: it is used to delete rows from a table based on
condition.
43
DML Commands contd…
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)
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);
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)
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)
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.
54
Transaction Control Language (TCL)
55
Transaction Control Language (TCL)
57