Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

ALPHA COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER ENGINEERING (DIPLOMA)


ADVANCED DATABASE MANAGEMENT SYSTEM(3340701)

SQL (Structured Query Language):


Structured Query Language is a database computer language designed for managing
data in relational database management systems (RDBMS), and originally based upon
Relational Algebra. Its scope includes data query and update, schema creation
andmodification, and data access control.
SQL was one of the first languages for Edgar F. Codd's relational model and became
themost widely used language for relational databases.
• IBM developed SQL in mid of 1970’s.
• Oracle incorporated in the year 1979.
• SQL used by IBM/DB2 and DS Database Systems.
• SQL adopted as standard language for RDBS by ASNI in 1989.

DATA TYPES:

1. CHAR (Size):
This data type is used to store character strings values of fixed length.The size in
brackets determines the number of characters the cell can hold. The maximum number
of character is 255 characters.

2.VARCHAR (Size) / VARCHAR2 (Size):


This data type is used to store variable length alphanumeric data. The maximum
character can hold is 2000 character.

3. NUMBER (P, S):


The NUMBER data type is used to store number (fixed or floating point). Number of
virtually any magnitude may be stored up to 38 digits of precision.Number as large as
9.99 * 10 124. The precision (p) determines the number of places to the right of the
decimal. If scale is omitted then the default is zero. If precision is omitted, values are
stored with their original precision up to the maximum of 38 digits.

4. DATE:
This data type is used to represent date and time. The standard format is DD-MM-YY
as in 17-SEP-2009. To enter dates other than the standard format, use the appropriate
functions. Date time stores date in the 24-Hours format. By default the time in a date
field is 12:00:00 am, if no time portion is specified. The default date for a date field is
the first day the current month.

5. LONG:
This data type is used to store variable length character strings containing up to 2GB.
Long data can be used to store arrays of binary data in ASCII format. LONG values
cannot be indexed, and the normal character functions such as SUBSTR cannot be
applied.

6. RAW:
The RAW data type is used to store binary data, such as digitized picture or image.
Data loaded into columns of these data types are stored without any further
conversion. RAW data type can have a maximum length of 255 bytes. LONG RAW
data type can contain up to 2GB.
1 DATA DEFINITION LANGUAGE (DDL):
The Data Definition Language (DDL) is used to create and destroy databases
and database objects. These commands will primarily be used by database
administrators during the setup and removal phases of a database project.

Let's take a look at the structure and usage of four basic DDL commands:
1.CREATE
2.ALTER
3.DROP
4.RENAME

1. CREATE:This is used to create a new relation (table).


Syntax:
CREATE TABLE <relation_name/table_name >
(field_1 data_type(size),field_2 data_type(size), .. . );

Example:

SQL> CREATE TABLE Student (sno NUMBER (3), sname CHAR (10),
class CHAR (5));
ALTER:

(a) ALTER TABLE ...ADD...

This is used to add some extra fields into existing relation.

Syntax:

ALTER TABLE relation_name ADD (new field_1 data_type(size), new


field_2 data_type(size),..);

Example:

SQL>ALTER TABLE std ADD (Address CHAR(10));

(b) ALTER TABLE...MODIFY...

This is used to change the width as well as datatype of fields of existing


relations.

Syntax:

ALTER TABLE relation_name MODIFY (field_1 newdata_type(Size),


field_2 newdata_type(Size),....field_newdata_type(Size));

Example:
SQL>ALTER TABLE student MODIFY(sname VARCHAR(10),class
VARCHAR(5));
c) ALTER TABLE..DROP...
This is used to remove any field of existing relations.

Syntax:

ALTER TABLE relation_name DROP COLUMN (field_name);

Example:
SQL>ALTER TABLE student DROP column (sname);

d)ALTER TABLE..RENAME...
This is used to change the name of fields in existing relations.

Syntax:

ALTER TABLE relation_name RENAME COLUMN (OLD field_name) to


(NEW field_name);

Example:
SQL>ALTER TABLE student RENAME COLUMN sname to stu_name;
DROP TABLE:
This is used to delete the structure of a relation. It permanently deletes the
records in the table.

Syntax:
DROP TABLE relation_name;

Example:
SQL>DROP TABLE std;
RENAME:
To rename a table, the SQL ALTER TABLE syntax is:
For Oracle, MySQL, MariaDB, PostgreSQL and SQLite:
It is used to modify the name of the existing database object.

Syntax:
ALTER TABLE table_name RENAME TO new_table_name;
Example:
SQL>ALTER TABLE Student1 RENAME TO new;
Experiment No:2

Title : Implementation of DML commands of SQL with suitable examples


 Insert table
 Update table
 Delete Table

Objective :
 To understand the different issues involved in the design and implementation of a
database system
 To understand and use data manipulation language to query, update, and manage a
database

Theory :

DATA MANIPULATION LANGUAGE (DML): The Data Manipulation Language


(DML) is used to retrieve, insert and modify database information. These commands will be
used by all database users during the routine operation of the database. Let's take a brief look
at the basic DML commands:
1. INSERT 2. UPDATE 3. DELETE
1. INSERT INTO: This is used to add records into a relation. These are three type of
INSERT INTO queries which are as
a) Inserting a single record
Syntax: INSERT INTO < relation/table name> (field_1,field_2……field_n)VALUES
(data_1,data_2,........data_n);
Example: SQL>INSERT INTO student(sno,sname,class,address)VALUES
(1,’Ravi’,’M.Tech’,’Palakol’);
b) Inserting a single record
Syntax: INSERT INTO < relation/table name>VALUES (data_1,data_2,........data_n);
Example: SQL>INSERT INTO student VALUES (1,’Ravi’,’M.Tech’,’Palakol’);

c) Inserting all records from another relation


Syntax: INSERT INTO relation_name_1 SELECT Field_1,field_2,field_n
FROM relation_name_2 WHERE field_x=data;

15
Example: SQL>INSERT INTO std SELECT sno,sname FROM student
WHERE name = ‘Ramu‘;

d) Inserting multiple records


Syntax: INSERT INTO relation_name field_1,field_2,.....field_n) VALUES
(&data_1,&data_2,........&data_n);
Example: SQL>INSERT INTO student (sno, sname, class,address)
VALUES (&sno,’&sname’,’&class’,’&address’);
Enter value for sno: 101
Enter value for name: Ravi
Enter value for class: M.Tech
Enter value for name: Palakol

2. UPDATE-SET-WHERE: This is used to update the content of a record in a relation.


Syntax: SQL>UPDATE relation name SET Field_name1=data,field_name2=data,
WHERE field_name=data;
Example: SQL>UPDATE student SET sname = ‘kumar’ WHERE sno=1;

3. DELETE-FROM: This is used to delete all the records of a relation but it will retain the
structure of that relation.

a) DELETE-FROM: This is used to delete all the records of relation.


Syntax: SQL>DELETE FROM relation_name;
Example: SQL>DELETE FROM std;

b) DELETE -FROM-WHERE: This is used to delete a selected record from a relation.


Syntax: SQL>DELETE FROM relation_name WHERE condition;
Example: SQL>DELETE FROM student WHERE sno = 2;

5. TRUNCATE: This command will remove the data permanently. But structure will not be
removed.

16
Difference between Truncate & Delete:-
 By using truncate command data will be removed permanently & will not get back
where as by using delete command data will be removed temporally & get back by
using roll back command.
 By using delete command data will be removed based on the condition where as by
using truncate command there is no condition.
 Truncate is a DDL command & delete is a DML command.

Syntax: TRUNCATE TABLE <Table name>


Example TRUNCATE TABLE student;

 To Retrieve data from one or more tables.

1. SELECT FROM: To display all fields for all records.


Syntax : SELECT * FROM relation_name;
Example : SQL> select * from dept;
DEPTNO DNAME LOC
-------- ----------- ----------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON

2. SELECT FROM: To display a set of fields for all records of relation.


Syntax: SELECT a set of fields FROM relation_name;
Example: SQL> select deptno, dname from dept;
DEPTNO DNAME
------- ----------
10 ACCOUNTING
20 RESEARCH
30 SALES
3. SELECT - FROM -WHERE: This query is used to display a selected set of fields for a
selected set of records of a relation.
Syntax: SELECT a set of fields FROM relation_name WHERE condition;

17
Example: SQL> select * FROM dept WHERE deptno<=20;
DEPTNO DNAME LOC
------ ----------- ------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS

LAB PRACTICE ASSIGNMENT:

Create a table EMPLOYEE with following schema:


(Emp_no, E_name, E_address, E_ph_no, Dept_no, Dept_name,Job_id , Salary)

Write SQL queries for following question:


1. Insert aleast 5 rows in the table.
2. Display all the information of EMP table.
3. Display the record of each employee who works in department D10.
4. Update the city of Emp_no-12 with current city as Nagpur.
5. Display the details of Employee who works in department MECH.
6. Delete the email_id of employee James.
7. Display the complete record of employees working in SALES Department.

************************************

18
Experiment No: 10

Title: Creating Database/ Table Space


 Managing Users: - Create User, Delete User
 Managing Passwords
 Managing roles: - Grant , Revoke

Objective:
 To understand the concept of administrative commands

Theory:
DATABASE is collection of coherent data.
To create database we have :
Syntax: CREATE DATABASE <database_name>
Example : CREATE DATABASE my_db;

TABLESPACE:
The oracle database consists of one or more logical storage units called tablespaces. Each
tablespace in an Oracle database consists of one or more files called datafiles, which are
physical structures that confirm to the operating system in which Oracle is running.
Syntax:
CREATE<tablespace name> DATAFILE'C:\oraclexe\app\oracle\product\10.2.0\
server \<file name.dbf ’SIZE 50M;
Example:
Create tablespace te_cs DATAFILE 'C:\oraclexe\app\oracle\product\10.2.0\
server\usr.dbf ’SIZE 50M;
CREATE USER:
The DBA creates user by executing CREATE USER statement.
The user is someone who connects to the database if enough privilege is granted.

43
Syntax:
SQL> CREATE USER < username> -- (name of user to be created )
IDENTIFIED BY <password> -- (specifies that the user must
login with this password)
SQL> user created
Eg: create user James identified by bob;
(The user does not have privilege at this time, it has to be granted.These privileges determine
what user can do at database level.)
PRIVILEGES:
A privilege is a right to execute an SQL statement or to access another user's object.
In Oracle, there are two types of privileges
 System Privileges
 Object Privileges
 System Privileges : are those through which the user can manage the performance of
database actions. It is normally granted by DBA to users.
Eg: Create Session,Create Table,Create user etc..
 Object Privileges : allow access to objects or privileges on object, i.e. tables, table
columns. tables,views etc..It includes alter,delete,insert,select update etc.
(After creating the user, DBA grant specific system privileges to user)

GRANT:
The DBA uses the GRANT statement to allocate system privileges to other user.
Syntax:
SQL> GRANT privilege [privilege…. … ]
TO USER ;
SQL> Grant succeeded
Eg: Grant create session, create table, create view to James;
Object privileges vary from object to object.An owner has all privilege or specific privileges
on object.
SQL> GRANT object_priv [(column)]
ON object

44
TO user;
SQL>GRANT select, insert ON emp TO James;
SQL>GRANT select ,update (e_name,e_address)
ON emp TO James;
CHANGE PASSWORD:
The DBA creates an account and initializes a password for every user.You can change
password by using ALTER USER statement.
Syntax:
Alter USER <some user name>
IDENTIFIED BY<New password>
Eg: ALTER USER James
IDENTIFIED BY sam
REVOKE:
REVOKE statement is used to remove privileges granted to other users.The privileges you
specify are revoked from the users.
Syntax:
REVOKE [privilege.. …]
ON object
FROM user
Eg:
 REVOKE create session,create table from James;
 REVOKE select ,insert
ON emp
FROM James
ROLE:
A role is a named group of related privileges that can be granted to user.In other words, role
is a predefined collection of previleges that are grouped together,thus privileges are easier to
assign user.
SQL> Create role custom;
SQL> Grant create table, create view TO custom;
SQL> Grant select, insert ON emp TO custom;

45
Eg: Grant custom to James, Steve;

LAB PRACTICE ASSIGNMENT:

1. Create user and implement the following commands on relation (Emp and Dept).
2. Develop a query to grant all privileges of employees table into departments table.
3. Develop a query to grant some privileges of employees table into departments table.
4. Develop a query to revoke all privileges of employees table from departments table.
5. Develop a query to revoke some privileges of employees table from departments table.

************************************

46
VIVA-VOCE

1. Define DCL?

The DCL language is used for controlling the access to the table and hence securing
the database. DCL is used to provide certain privileges to a particular user. Privileges are
rights to be allocated.
2. List the DCL commands used in data bases

The privilege commands are namely, Grant and Revoke


3. Write the syntax for grant command

Grant < database_priv [database_priv…..] > to <user_name> identified by <password>


[,<pass word…..];
Grant <object_priv> | All on <object> to <user | public> [ With Grant Option ];
4. What are TCL commands?

*Commit *Rollback *save point


5. What are single row functions?

A single row function or scalar function returns only one value for every row
queries in table. Single row function can appear in a select command and can also be
included in a where clause. The single row function can be broadly classified as,
* Date Function * Conversion Function
* Numeric Function * Miscellaneous Function
*Character Function
6. List some character functions

initcap(char);
lower (char);
upper (char);
ltrim (char,[set]); rtrim (char,[set]);
7. What is a view?

A view is a logical table based on a table or another view. A view contains no data of
its own but is like a window through which data from tables can be viewed or changed.
8. List any two advantages of view?

1. Hides data complexity.


2. Simplifies the usage by combining multiple tables into a single table
9. List the set operations of SQL?

1) Union 2) Intersect operation 3) The except operation (minus)

10. What is the use of sub Queries?

A sub Queries is a select-from-where expression that is nested with in another


Queries. A common use of sub Queries is to perform tests for set membership, make set
comparisons and determine set cardinality

47

You might also like