Assignment No. 2-A Title
Assignment No. 2-A Title
Assignment No. 2-A Title
Laboratory 22)
Signature
Page 1
Assignment No. 2
Title: Design and Develop SQL DDL statements which demonstrate the use of SQL objects
such as Table, View , Index, Sequence, Synonym
• If you want only structure without records then select statement must have
condition. Syntax:
Both the Create table & Alter Table SQL can be used to write SQL sentences that
attach constraints.
Basically constraints are of three types
1) Domain
- Not Null
- Check
2) Entity
- Primary Key
- Unique
3) Referential
- Foreign key
CREATE TABLE
<tableName>
(<ColumnName>
datatype(size),
<ColumnName> datatype(size),….,
CHECK (columnName condition));
After table creation
Alter table tablename
Add constraints constraintname ckeck(condition)
The PRIMARY KEY Constraint
A primary key is one or more column(s) in a table used to uniquely identify each row in
the table.
• A table can have only one primary key.
• Can not be left blank
Page 4
• Data must be UNIQUE.
• Not allows null values
• Not allows duplicate values.
• Unique index is created automatically if there is a primary
key. Primary key constraint defined at column level
Syntax:
CREATE TABLE <TableName>
(<ColumnName1> <DataType>(<Size>)PRIMARY
KEY,<columnname2
<datatype(<size>),…..);
• Primary key constraint defined at Table
level Syntax:
CREATE TABLE <TableName>
(<ColumnName1> <DataType>(<Size>) ,…,
PRIMARY
KEY(<ColumnName1> <ColumnName2>));
• key constraint defined at Table level
Syntax:
CREATE TABLE <TableName>
(<ColumnName1> <DataType>(<Size>) <columnname2
datatype<(size)<,<columnname3 datatype<size>constraint constraintname
PRIMARY KEY(<ColumnName1>));
After table creation
Alter table tablename
Add(constraint constraintname primary key(columnname));
The Unique Key Constraint
A foreign key is a column( or group of columns) whose values are derived from
primary key or unique key of some other table.
To drop the
constraints Syntax:-
Drop constraint constraintname;
Describe commands
To view the structure of the table created use the DESCRIBE command.The
command displays the column names and datatypes
Syntax:-
Desc[ribe]<table_name>
e.g desc student
Restrictions for creating a table:
1.Table names and column names must begin with a letter.
2.Table names and column names can be 1 to 30 characters long.
Case2:-
Alter table <table_name>
Modify(colume_name 1 datatype size(),
colume_name 2 datatype size(),
…...
colume_name n datatype size());
After you create a table, you may need to change the table structures because you need to have
a column definition needs to be changed. Alter table statement can be used for this purpose.
You can add columns to a table using the alter table statement with the ADD clause.
E.g. Suppose you want to add enroll_no in the student table then we write
Alter table student Add(enroll_no number(10));
You can modify existing column in a table by using the alter table statement with
modify clause.
E.g. Suppose you want to modify or chang the size of previously defined field name in
the student table then we write
Drop table command remnoves the definitions of an oracle table.When you drop a table
,the database loses all the data in the table and all the indexes associated with it.
e.g drop table student;
Truncate table
command Syntax:-
Trunc table<table_name>
The truncate table statement is used to remove all rows from a table and to release the storage
space used by the table.
e.g.Trunc table student;
Rename table
command Syntax:-
Page 8
Rename<oldtable_name> to<newtable_name>
Automatically :- A unique index is created automatically when you define a primary key or
unique key constraint in a table definition.
Manually :- users can create non unique indexes or columns to speed up access time to the rows.
Syntax:
Create index<index_name> On table(column[ , column]…);
Eg. Create index emp_ename_idx On emp(ename);
When to create an index
a) The column is used frequently in the WHERE clause or in a join condition.
b) The column contains a wide range of values.
c) The column contains a large number of values.
To display created index of a
table eg.
Removing an Index
Syntax:-
Drop index <index_name>; eg. Drop
index emp_name_idx;
Note: 1) we cannot modify indexes.
2) To change an index, we must drop it and the re-create it.
Views
View is a logical representation of subsets of data from one or more tables. A view takes the
output of a query and treats it as a table therefore view can be called as stored query or a
virtual table.The tables upon which a view is based are called base tables. In Oracle the SQL
command to create a view (virtual table) has the form
Create [or replace] view <view-name> [(<column(s)>)] as
<select-statement> [with check option [constraint <name>]];
The optional clause or replace re-creates the view if it already exists. <column(s)> names the
columns of the view. If <column(s)> is not specified in the view definition, the columns of
the view get the same names as the attributes listed in the select statement (if possible).
Example: The following view contains the name, job title and the annual salary of
employees working in the department 20:
Create view DEPT20 as
select ENAME, JOB, SAL 12 ANNUAL SALARY from EMP where DEPTNO = 20;
In the select statement the column alias ANNUAL SALARY is specified for the expression
SAL∗12 and this alias is taken by the view. An alternative formulation of the above view
definition is
Create view DEPT20 (ENAME, JOB, ANNUAL SALARY) as select ENAME, JOB, SAL
12 from EMP where DEPTNO = 20;
A view can be used in the same way as a table, that is, rows can be retrieved from a view(also
respective rows are not physically stored, but derived on basis of the select statement inthe view
definition), or rows can even be modified. A view is evaluated again each time it is accessed. In
Oracle SQL no insert, update, or delete modifications on views are allowed
that use one of the following constructs in the view definition:
• Joins
• Aggregate function such as sum, min, max etc.
• set-valued subqueries (in, any, all) or test for existence (exists)
• group by clause or distinct clause
In combination with the clause with check option any update or insertion of a row into the view
is rejected if the new/modified row does not meet the view definition, i.e., these rows would not
be selected based on the select statement. A with check option can be named using the
constraint clause.
After creating a sequence we can access its values with the help of pseudo
columns like curval & nextval.
Nextval : nextval returns initial value of the sequence when reference to for the
first time. Last references to the nextval will increment the sequence using the
increment by clause & returns the new value.
Curval : curval returns the current value of the sequence which is the value
returned by the last reference to last value.
Modifyning a sequence:
The sequence can be modified when we want to perform the following :
□
Set or eliminate minvalue or maxvalue
□
Change the increment value.
Synonym:
A synonym is a database object,which is used as an alias(alternative name)for a
table,view or sequence.
Syntax:-
Create[public]synonym
<synonym_name>for<table_name>;
In the
syntax
Tables
In relational database systems (DBS) data are represented using tables (relations). A query issued
against the DBS also results in a table. A table has the following structure:
← Tuple Record)
(or
Max
Max Max PL/SQL
Size: Max Size:
Datatype Description Size: Size: Subtypes/
Oracle Oracle 9
Oracle 7 8 PL/SQL Synonyms
Variable length
character string
having 2000 4000 32767
STRING
VARCHAR2(s maximum bytes bytes 4000 bytes bytes
VARCHA
ize) length size minimum minimu minimum is 1 minimum
R
bytes. is 1 m is 1 is 1
You must
specify size
Variable length
national
character set
4000 32767
string having STRING
NVARCHAR2 bytes 4000 bytes bytes
maximum N/A VARCHA
(size) minimu minimum is 1 minimum
length size R
m is 1 is 1
bytes.
You must
specify size
Now deprecated
- VARCHAR is
a synonym for
VARCHAR VARCHAR2 - - -
but this usage
may change in
future versions.
Fixed length
character data of 2000 32767
length size 255 bytes
bytes bytes
bytes. This Default 2000 bytes
Default Default
and Default and CHARAC
CHAR(size) should be used and and
minimum minimum size TER
for fixed length minimu minimum
data. Such as size is 1 is 1 byte.
m size is size is 1
codes A100, byte.
1 byte. byte.
B102...
Fixed length 2000 2000 bytes 32767
NCHAR
national N/A bytes Default and bytes
(size)
character set Default minimum size Default
Page 16
data of length and is 1 byte. and
size bytes. This minimu minimum
should be used m size is size is 1
for fixed length 1 byte. byte.
data. Such as
codes A100,
B102...
Magnitud
e
1E-130 ..
10E125
maximum
precision
fixed-point
of 126
numbers:
binary
DEC
digits,
DECIMAL
which is
NUMERIC
roughly
equivalen
floating-
t to 38
The point:
decimal
The precisio DOUBLE
digits
precision n p can PRECISIO
The precision The scale
p can range N FLOAT
p can range s can
range from 1 binary_dou
from 1 to 38. range
Number having from 1 to to 38. ble
from -84
NUMBER(p,s) precision p and 38. binary_floa
The scale s to 127.
scale s. The scale The scale t
can range For
s can s can
from -84 to floating
range range integers:
127. point
from -84 from -84 INTEGER
don't
to 127. to 127. INT
specify
SMALLIN
p,s
T
REAL
simple_inte
has a
ger(10g)
maximum
precision
BOOLEA
of 63
N
binary
REAL
digits,
which is
roughly
equivalen
t to 18
decimal
digits
signed integers magnitud
PLS_INTEGE PLS_INTEGER PL/SQL PL/SQL e range is
PL/SQL only
R values require only only -
less storage and 21474836
provide better 47 ..
performance 21474836
than NUMBER 47
values.
So use
PLS_INTEGER
where you can!
NATURA
magnitud L
signed integers e range is NATURA
(older slower - LN
BINARY_INT
version of 21474836 POSITIVE
EGER
PLS_INTEGER 47 .. POSITIVE
) 21474836 N
47 SIGNTYP
E
32760
bytes Note
Character data this is
of variable smalller
2 2 Gigabytes -
length (A bigger 2 than the
LONG Gigabyt but now
version the Gigabytes maximum
es deprecated
VARCHAR2 width of a
datatype) LONG
column
from
from January 1,
from
January 4712 BC
January 1,
1, 4712 from January to
4712 BC
BC to 1, 4712 BC to December
DATE Valid date range to
Decemb December 31, 31, 9999
December
er 31, 9999 AD. AD.
31, 4712
9999 (in
AD.
AD. Oracle7 =
4712 AD)
Accepted
the number of
TIMESTAMP values of
digits in the
(fractional_sec fractional_sec
fractional part of - -
onds_precision onds_precisio
the SECOND
) n are 0 to 9.
datetime field. (default = 6)
TIMESTAMP Accepted
(fractional_sec As above with values of
onds_precision time zone fractional_sec
- -
) WITH displacement onds_precisio
{LOCAL} value n are 0 to 9.
TIMEZONE (default = 6)
INTERVAL Time in years - - Accepted
Database Management System TE Computer Engineering (2021-
Laboratory 22)
YEAR and months, values are 0 to
(year_precision where 9. (default =
) TO MONTH year_precision 2)
is the number of
digits in the
YEAR datetime
field.
Time in days,
hours, minutes,
and seconds.
day_precision
INTERVAL day_precision is may be 0 to 9.
DAY the maximum (default = 2)
(day_precision) number of digits
TO SECOND in 'DAY' - - fractional_sec
(fractional_sec onds_precisio
onds_precision fractional_secon n may be 0 to
) ds_precision is 9. (default =
the max number 6)
of fractional
digits in the
SECOND field.
Raw binary data
of length size Maximu
Maximum Maximum
bytes. m size is 32767
RAW(size) size is 255 size is 2000
You must 2000 bytes
bytes. bytes
specify size for bytes
a RAW value.
32760
bytes Note
this is
Raw binary data smalller
of variable 2 2 2 Gigabytes - than the
LONG RAW length. (not Gigabytes Gigabyt but now maximum
intrepreted by . es. deprecated width of a
PL/SQL) LONG
RAW
column
Hexadeci
Hexadecimal
mal string
string
representi
representing the
ng the
unique address
unique
of a row in its
ROWID 8 bytes 10 bytes 10 bytes address of
table. (primarily
a row in
for values
its table.
returned by the
(primarily
ROWID
for values
pseudocolumn.) returned
Page 19
by the
ROWID
pseudocol
umn.)
universal
rowid -
UROWID
Hex string
representi
ng the
logical
The See
Hex string address of
maximu CHARTO
representing the The maximum a row of
m size ROWID
logical address size and defaultan index-
N/A and and the
of a row of an is 4000 bytes organized
default is package:
index-organized table,
4000 DBMS_R
table either
bytes OWID
physical,
logical, or
foreign
(non-
Oracle)
Binary format of
an operating
system
MLSLABEL label.This
datatype is used
with Trusted
Oracle7.
Character Large 4Gigabyte 4Gigaby 4Gigabyt
CLOB Object s tes 4Gigabytes es
National
Character Large 4Gigaby 4Gigabyt
NCLOB 4Gigabytes
Object tes es
Binary Large 4Gigaby 4Gigabyt
BLOB 4Gigabytes
Object tes es
The size
of a
BFILE is
system
dependent
pointer to binary 4Gigaby but cannot
BFILE 4Gigabytes
file on disk tes exceed
four
gigabytes
(2**32 -
1 bytes).
Populate
with
XML
from a
CLOB or
VARCH
XMLType XML data - - 4Gigabytes AR2.
or query
from
another
XMLTyp
e column.
1.Create tables ;
create table account (username varchar (20) PRIMARY KEY, password varchar (20) NOT NULL,
email_id varchar (30) NOT NULL, address varchar (40) NOT NULL);
2.Insert values in tables.
insert into Account values(‘Ajit’,’Ajit222’,’Ajit@gmail.com’,’Goa’);
insert into Account values(‘Akash’,’Akash678’,’Akash@gmail.com’,’Chennai’);
insert into Account values(‘Amit’,’Amit’,’Amit@gmail.com’,’Bhopal’);
insert into Account values(‘Alisha’,’Alish555,’Alisha@gmail.com’,’Pune’);
insert into Account values(‘Chaitanya’,’Chaitanya111,’Chaitanya@gmail.com’,’Banglore);
insert into Account values(‘Swapnil’,’Swapnil123’,’Swapnil@gmail.com’,’Delhi’);
insert into Account values(‘Smith’,’Smith123’,’Smith@gmail.com’,’Mumbai’);
1.Create tables ;
create table conductor (Conductor_ID int(5) PRIMARY KEY, First_Name varchar(20), Last_Name
varchar(20));