Practical 1
Practical 1
Practical 1
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.
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
Example:
SQL> CREATE TABLE Student (sno NUMBER (3), sname CHAR (10),
class CHAR (5));
ALTER:
Syntax:
Example:
Syntax:
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:
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:
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
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 :
15
Example: SQL>INSERT INTO std SELECT sno,sname FROM student
WHERE name = ‘Ramu‘;
3. DELETE-FROM: This is used to delete all the records of a relation but it will retain the
structure of that relation.
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.
17
Example: SQL> select * FROM dept WHERE deptno<=20;
DEPTNO DNAME LOC
------ ----------- ------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
************************************
18
Experiment No: 10
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;
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
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?
47