Академический Документы
Профессиональный Документы
Культура Документы
vi SEMESTER
DATABASE LABORATORY
ISE
Name:
Reg. No.
Batch
CONTENTS
DO …..
• Come prepared to the Lab.
• Submit your Records to the Lecturer and sign in the Log Book on entering
the Lab.
• Follow the Lab exercise cycles as instructed by the Department. Violating
the same will result in deduction of marks.
• Use the same login(if any) assigned to you.
• Put the chairs back to its position before you leave.
• Backlog exercises to be executed after completing regular exercises.
DON’T …..
• Move around in the lab during the lab session.
• Tamper System Files or Try to access the Server.
• Write Data Sheets or Records in the Lab.
• Change the system assigned to you without the notice of the Lab Staff.
• Teaching your friends.
SQL is the language used in most RDBMSs to enable users to access data.
Developed by a prototype RDBMS called SYSTEM R by IBM in mid seventies.
ORACLE introduced the first commercially viable SQL implementation in 1979.
SQL *PLUS does not care whether SQL commands are given in lowercase or
uppercase.
Case matters only when SQL *PLUS or ORACLE is checking a value for equality
in the database.
A statement partially consists of SQL reserved words, whoch cannot be used for any
other purpose.
Data Definition Statements are : Create, Alter, Drop, Rename, Grant, Revoke.
In order to create any table using DDL, datatypes are essentially required to define the
various data entities of the table definition.
Just as people can be classified into different types, based on characteristics (shy,
outgoing, smart, talkative), different kinds of data can be classified according to the
characteristics.
Null/Not Null :
o Null specifies that the column may contain value or may not contain any.
o By default all column in a table allow Null values.
o Not Null specifies that all rows in a table must have value for specified
column. All NOT NULL columns are mandatory.
o NULL should be however specified for numeric fields, as any arithmetic
with NULL results in a NULL value.
Check :
o Specifies a condition which must be true for the data being inserted into the
table. A single column can have multiple checks.
o Check involving multiple columns is given as table level check constraint.
Primary Key :
o Refers to one or more column values in a table that can uniquely identify
each row in a table.
o A table can have only ONE Primary key.
o Primary Key can be made up of one column or more than one column.
When a primary key is made up of more than one column, then the Key is
called Composite Primary key. ‘
o A primary key column cannot contain nulls and is unique by default.
Foreign Key :
o A Foreign Key is one or more columns with values based on the Primary
key values from another table.
o A foreign key constraint specifies that the values stored in foreign key
column correspond to values of the primary key in other table.
o Foreign key constraint is also known as referential Integrity constraint.
o The table containing foreign key is known as Dependent table (Master
Table) and the table that is referenced by foreign key is called Referenced
table (Detail Table)
Default :
o Specifies a value to be assigned to the column if a subsequent INSERT
statement omits a value for the column. The datatypes of the expression
must match the datatype of the column.
CREATING A TABLE
Tables are fundamental storage object of the Oracle database.
Tables consist of rows and columns. A single table can have maximum of 1000 columns.
Syntax :
Create Table [Schema] table_name (
Column datatype [column constraint],
……………..,
[table constraint]);
Table_name : Is the name of the table to be created. Table name and column name can
be 1 to 30 characters long.
The character must be alphabetic, but name may include letters, numbers and
underscores.
The table name cannot be the name of another object owned by same user and cannot be
reserved word.
Column : Specifies the name of a column of the table. The number of column in a table
can range from 1 to 1000. This was limited to 254 columns in the last version of Oracle.
datatype : Is the datatype of a column. Specifies the type and width of data to be stored
in the column.
If more than one constraint is to be specified to a single column, then it can be declared
as a table constraint.
REFERENCES Keyword specifies a foreign key in the detail table.
Syntax :
INSERT INTO tablename ([field1, field2 ,….]) VALUES (value1, value2…)
Examples :
Character and Date values must be enclosed in single quotes and date must be in
default format.
Columns can be inserted out of order they appear in the table. This does not change
the fundamental order of columns in the table. It simply allows listing of the data
fields in a different order.
To insert date in different format, to_date function can be used. TO_date can also be
used to format a time into a date column.
ALTERING TABLE :
Examples :
In Oracle every row has a unique ROWID column. Even duplicate records are unique on
Oracle because of ROWID. The ROWID column exists in every row in a database and
no two ROWIDs are same. Duplicate roes can be deleted using the ROWID.
UPDATING A TABLE
Values stored in a table can be updated using the UPDATE statement.
Syntax :
UPDATE tablename SET (field1=value, field2=value ……)
WHERE condition;
Example :
To Update all rows in a table
A new table can be created from a table using CREATE … SELECT statement.
Example :
CREATE TABLE backup AS
SELECT * FROM dept1;
The new table created can have selected columns from the table from, which it is created.
CREATE TABLE emp_grade AS
SELECT eno, ename Employee_Name, dname
FROM employee1, dept1 where sal>2000;
The new table can have different column names. The above examples shows column
ename in employee1 table is named as Employee_name in the new table emp_grade.
Another way of creating a table with selected columns and column names is
CREATE TABLE emp_grade AS
SELECT eno EMPLOYEE_NO, dept1.dno, dname DEPT_NAME
FROM employee1,dept1
RENAMING A TABLE :
DROPPING A TABLE :
CONTROLLING TRANSACTIONS:
When inserting toed in a table, the transactions can be controlled so that user has
option to return the table back to the original status, or to make the changes in the table
permanent so that the changes are available to all.
COMMIT :
Committing a transaction permanently saves the transaction changes to the
database.
Until the commit command is not given, the work done till then can be undone.
Committing transactions makes changes available to other users who otherwise will
continue to get old information.
Data Definition commands force a commit to occur, without user instructing it to.
Ex. Quit, create table, create view, alter table, drop table, drop view commands perform
implicit commit.
AUTOCOMMIT :
The SQL *Plus autocommit feature can cause changes to be committed after each
SQL command, without user explicitly giving commit command.
The autocommit feature is set OFF by default and can be set ON with the SET
command :
To check and display the current status of autocommit, SHOW command is used :
SHOW autocommit;
ROLLBACK:
This command undoes all changes since the beginning of the current transaction
using the corresponding rollback segment.
This command can be used to manually undo the work done by an in-doubt distributed
transaction.
In case of server failure with host computer or application program, Oracle performs an
implicit rollback.
SELECT ….
Syntax
SELECT column(s) FROM tablename
WHERE condition;
Examples
To display all the columns of a table :
Using & - the substitution variable : Used to fetch data or value at the time of execution
Example :
Exercise 1:
1. Consider the Insurance database given below. The primary keys are
underlined and the data types are specified.
PERSON (driver-id#:String, name : string, address:string)
CAR (Regno:String, model:string, year:int)
ACCIDENT (Report-Number:int, date: date, location:string)
OWNS (driver-id#:string, Regno:String)
PARTICIPATED (driver-id#:string, regno:string,report-number:int,
damageamount:int)
1. Create the above tables by properly specifying the primary keys and the foreign
keys.
2. Enter atleast five tuples for each relation.
3. Demonstrate how you
a. Update the damage amount for the car with a specific Regno in the accident
with report number 12 to 25000.
b. Add a new accident to the database
4. Find the total number of people who owned cars that were involved in accidents in
2008.
5. Find the number of accidents in which cars belonging to a specific model were
involved.
6. Generate suitable reports.
7. Create suitable front ends for querying and displaying the results.
Solution :
1. To create the tables for the above database we have the statements –
Note:-
The values entered into the owns and participated relations should already
be present in their respective referenced tables.
Ex:
10 AVINASH BANGALORE
20 BOBBY BOMBAY
30 CLEO DELHI
40 DARYL CALCUTTA
50 ETON PUNE
1 01-FEB-04 BANGALORE
2 02-FEB-02 BANGALORE
3 03-FEB-04 BOMBAY
12 01-FEB-04 BANGALORE
4 02-FEB-02 DELHI
DRIVERID REGNO
10 200
20 100
30 300
40 400
50 500
20 600
40 400 1 1000
20 100 2 2000
40 400 3 5000
50 500 12 15000
40 400 4 15000
50 500 12 25000
OUTPUT: TOTAL_CARS_M800: 3
b) Text bar: used to display the contents of the tables in the database.
Enter the data source as adodc that appears automatically when you click
On data source. Enter the data field.
c) Before changing textbox properties , click on Project option of menu bar
and then select components.
Select controls – Microsoft ADO Data control 6.0 & click on ok. Now select
Adodc option from toolbox. Drag it to form. Right click to go properties.
Ex: 1) in the main menu , for the command button ‘TABLE’ corresponding
to the person table, the sql statement will be SELECT * from PERSON.
Exercise 2:
i. Create the above tables by properly specifying the primary keys and the
foreign keys.
ii. Enter atleast five tuples for each relation.
iii. Produce a listing : CUSTNAME, #oforders, AVG_ORDER_AMT, where the
middle column is the total numbers of orders by the customer and the last
column is the average order amount for that customer.
iv. List order# for orders that were shipped for all the warehouses that the
company has in a specific city.
v. Demonstrate how you delete item# 10 from the ITEM table and make that field
null in the ORDER_ITEM table.
vi. Generate suitable reports.
vii. Create suitable front ends for querying and displaying the results.
Note:-
ORDER is a reserve word – while creating the table create it in other name like
ordertab and the field in name orderno instead.
To implement the last part of the question while creating the item field in the
order_item table give the constraint ON DELETE SET NULL.
Ex:
1 ASHOK DELHI
2 KARAN BANGALORE
3 ROHIT DELHI
4 ROHAN MUMBAI
5 GURU GOA
6 AVIK DELHI
7 BHARAT DELHI
8 ANUJ DELHI
9 LAKSHMI BANGALORE
10 PRASAD BANGALORE
20 25-FEB-05 1 2000
30 15-DEC-06 3 30000
40 13-APR-05 3 25000
50 11-NOV-04 4 23000
10 6-MAY-08 1 10899
60 25-FEB-04 6 2000
70 15-DEC-03 8 30000
80 13-APR-04 8 25000
90 11-NOV-03 9 23000
100 6-MAY-07 6 19899
5 1000
10 1500
20 2300
25 500
30 876
40 1300
20 5 5
20 10 3
30 20 2
40 30 7
50 30 9
1 BANGALORE
2 GOA
3 UP
4 BIHAR
5 GOA
6 DELHI
7 DELHI
8 DELHI
20 1 28-FEB-05
30 4 25-DEC-06
50 4 28-FEB-05
30 3 27-FEB-05
10 1 16-MAY-08
50 5 11-NOV-04
Queries :
OUTPUT: EX:
ANUJ 2 27500
ASHOK 2 10949.5
AVIK 2 10949.5
LAKSHMI 1 23000
…………
OUTPUT: EX:
CITY=GOA
ORDER
50
v. /*If the constraint ON DELETE SET NULL is NOT given while creating the item
field in the order_item table - then we require to alter table order_item, insert
values and then have to delete constraint.
insert values
Exercise 3:
i. Create the above tables by properly specifying the primary keys and the
foreign keys
ii. Enter atleast five tuples for each relation.
iii. Demonstrate how you add a new text book to the database and make this book
be adopted by some department
iv. Produce a list of text books (include course#, book-ISBN, book_title) in the
alphabetical order for courses offered by the ‘CS’ department that use more
than two books.
v. List any department that has all its adopted books published by a specific
publisher.
vi. Generate suitable reports.
vii. Create suitable front ends for querying and displaying the results.
Create the database and populate with the data as shown below or with similar
data to ensure the proper output for the last queries instead of getting null values:-
select * from course;
Output:-
Enter value for publisher: mcgraw
old 5: (select bookisbn from text t where publisher like '&publisher'))
new 5: (select bookisbn from text t where publisher like 'mcgraw'))
DEP
---
cse
SQL> /
Enter value for publisher: harcourt
old 5: (select bookisbn from text t where publisher like '&publisher'))
new 5: (select bookisbn from text t where publisher like 'harcourt'))
DEP
---
mca
Exercise 4 :
CATID DESCRIPTION
--------- -------------------------
111 Text book
112 nice book
113 thing of value
or
Exercise 5 :
i. Create the above tables by properly specifying the primary keys and the
foreign keys.
ii. Enter atleast five tuples for each relation.
iii. Find all the customers who have atleast two accounts at the Main branch
iv. Find all the customers who have an account all the braches located in a specific
city.
v. Demonstrate how you delete all account tuples at every branch located in a
specific city.
vi. Generate suitable reports.
vii. Create suitable front ends for querying and displaying the results.
Solution for 5.1 and 5.2
select cname from depositor d, account a where d.accno = a.accno and bname = 'gstreet'
group by cname having count(d.accno)>=2;
o Create Suitable front-end to enter data, query the required information for the
exercise in Lab Session-2. The given exercise should be shown as a small
application by including as many queries as possible.
o Create Suitable front-end to enter data, query the required information for the
exercise in Lab Session-3. The given exercise should be shown as a small
application by including as many queries as possible.
o Create Suitable front-end to enter data, query the required information for the
exercise in Lab Session-4. The given exercise should be shown as a small
application by including as many queries as possible.
o Create Suitable front-end to enter data, query the required information for the
exercise in Lab Session-5. The given exercise should be shown as a small
application by including as many queries as possible.
o Create Suitable front-end to enter data, query the required information for the
exercise in Lab Session-6. The given exercise should be shown as a small
application by including as many queries as possible.
VIVA QUESTIONS : DBMS LAB