DBMS Lab File-BCA-507P
DBMS Lab File-BCA-507P
SUBMITTED BY:
Name: ____________________
Roll No. ___________________
BACHELOR OF COMPUTER APPLICATION
SUBMITTED TO:
Mr. KRISHNA KUMAR
Assistant Professor
Department of Computer Application
Sign: ___________________
`
INDEX
1.2 THEORY & CONCEPTS: SQL (Structured Query Language) is a nonprocedural language,
you specify what you want, not how to get it. A block structured format of English key words is
used in this query language. It has the following components.
DDL (Data Definition Language): The SQL DDL provides command for defining relation
schemas, deleting relations and modifying relation schema.
DML (DATA Manipulation Language): It includes commands to insert tuples into, delete tuples
from and modify tuples in the database.
View definition: The SQL DDL includes commands for defining views. Transaction Control- SQL
includes for specifying the beginning and ending of transactions.
Embedded SQL and Dynamic SQL: Embedded and Dynamic SQL define how SQL statements
can be embedded with in general purpose programming languages, such as C, C++, JAVA,
COBOL, Pascal and Fortran.
Integrity: The SQL DDL includes commands for specifying integrity constraints that the data
stored in the database must specify. Updates that violate integrity constraints are allowed.
Authorization: The SQL DDL includes commands for specifying access rights to relations and
views.
Domain types in SQL: The SQL standard supports a variety of built in domain types, including:
Char (n)- A fixed length character length string with user specified length .
Varchar (n)- A variable character length string with user specified maximum length n.
Int- An integer.
Small integer- A small integer.
Numeric (p, d)-A Fixed point number with user defined precision.
Real, double precision- Floating point and double precision floating point numbers with
machine dependent precision.
Float (n)- A floating point number, with precision of at least n digits.
Date- A calendar date containing a (four digit) year, month and day of the month.
Time- The time of day, in hours, minutes and seconds e.g. Time ’09:30:00’.
Number- Number is used to store numbers (fixed or floating point).
NUMBER(<p>, <s>): datatype stores numbers with a precision of p digits and a scale of s digits.
The precision and scale values are optional. The precision can be between 1 and 38, and the scale
DBMS Lab (BCA-507(P))
has range between -82 and 127.
DATE: datatype is used to store date and time information, the DATE datatype occupies a storage
space between seven bytes. The following information is contained within each DATE datatype:
Century
Year
Month
Day
Hour
Minute
Second
The default format is DD-MON-YY. The SYSDATE function returns the current system date and
time from the database server to which you’re currently connected.
Operators:
Arithmetic Operators: +, -, *, /
Concatenation Operators: ||
Set Operators:
UNION: returns all rows from either queries; no duplicate rows
UNION ALL: returns all rows from either queries; including duplicates
INTERSECT: returns distinct rows that are returned by both queries
MINUS: returns distinct rows that are returned by the first query but not returned by the
second
Comparison Operators:
= (Equality)
!=, <>, or ^= (Inequality)
< (Less Than)
> (More Than)
<= (Less Than or Equal To)
>= (Greater Than or Equal To)
ANY or SOME: operators are used to compare a value to each value in a list or subquery. These
operators must be preceeded by the comparison operators.
ALL: operator is used to compare a value to every value in a list or subquery. These operators must
be preceeded by the comparison operators.
Logical Operators: NOT, AND, OR
Writing Simple Queries: A query is a request for information from the database tables. Simple
queries are those that retrieve data from a single table. The basis of a query is the SELECT
statement.
Using the SELECT statement:
Syntax:
SELECT <columnname1>,…., <columnname n>
FROM <tablename>;
Column Alias Names: Column alias name is defined next to the column name with a space or by
Sing the keyord AS.
Syntax:
SELECT <columnname1> AS <aliasname1>, ….,<columnname n> AS <aliasname n>
FROM <tablename>;
Limiting Rows: A WHERE clause is used in SELECT statement to limit number of rows
processed.
Syntax-
SELECT <columnname1>,….,<columnname n>
FROM <tablename>
WHERE <searchcondition>;
Sorting Rows: ORDER BY clause is used to sort the rows in the resultset in ascending or
descending order. The default order is ascending.
Syntax:
For ascending order:
SELECT <columnname1>,….,<columnname n>
FROM <tablename>
WHERE <searchcondition>
ORDER BY <columnname>;
Other Operators:
IN and NOT IN: the IN and NOT In operators are used to test a membership condition. IN
evaluates to true if the value exists in the list or the resultset from the subquery. The NOT
IN evaluates to true if the value does not exist in the list or the resultset from a subquery.
BETWEEN: operator is used to test a range. BETWEEN A AND B evaluates to true if the
value is greater than or equal to A and less than or equal to B.
LIKE: operator is used to perform pattern matching. The pattern search character % is used
to match any character and any number of characters. _ is used to match any single
character. To search for the actual character % in the pattern search, you should include an
escape character (/) in the search string and notify using ESCAPE clause.
1.3 ASSIGNMENTS
Assignment 1
ii) dept
DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
1. Display all records from dept table where department no. is less than 30 or 40.
2. List all employees working as clerks and with salary greater than or equal to 1000.
3. List the names and department no. of all employees who do not belong to department no.
30 and 40.
4. List the names and department no. of all employees who belong to department no. 10 and
30.
5. List the names and department no. of all employees whose salary is greater than 1000 and
less than 2000.
6. List all employees with names starting with “A”.
7. Display all records from emp table in descending order of the salary of the employees.
1.2 THEORY AND CONCEPTS: There are many types of single-row functions built into SQL.
These include character, numeric, date, etc. All single-row functions can be incorporated into SQL.
These can be used in the SELECT, WHERE, and ORDER BY clauses of SELECT statements.
1.3 ASSIGNMENTS
Assignment 3
1.2 THEORY AND CONCEPTS: Group functions differ from single-row functions in how they
are evaluated. Single-row functions are evaluated once for each row retrieved. Group functions are
evaluated on groups of one or more rows at a time. Group functions are sometimes called aggregate
functions and return a value based on a number of inputs. The exact number of inputs are not
determined until the query is executed and all rows are fetched. This differs from single-row
functions in which the number of inputs is knows at parse time-before the query is executed. These
are as follows:
Function Description
AVG Returns the statistical mean
COUNT Returns the number of non-NULL rows
MAX Returns the largest value
MIN Returns the smallest value
SUM Adds all values and returns the result
Grouping date with GROUP BY: Group functions work on data that is grouped. We tell the
database how to group or categorize data with a GROUP BY clause. Whenever we use a group
function in SELECT clause of a SELECT statement, we must place all non-grouping/non-constant
columns in the GROUP BY clause. If no GROUP BY clause id specified, the default grouping
becomes the entire result set.
Syntax:
SELECT <columnname1>,….,<columnname n>, <aggregate function(columnname)>
FROM <tablename>
GROUP BY <columnname>;
Limiting grouped data with HAVING: Group functions cannot be used in WHERE clause.
Grouping is done after all rows have been fetched.
Syntax:
SELECT <columnname1>,….,<columnname n>, <aggregate function(columnname)>
FROM <tablename>
GROUP BY <columnname>
HAVING <condition>;
Assignment 6
Following table is created:
emp
1. Calculate the maximum, minimum, and average salary given to each department.
2. Display the total salary given to various designations in the organization.
3. Display the total number of employees working on different designations in the
organization.
4. Display the department numbers where the employees have average salary greater
than 2000.
5. Display the names of all employees belonging to department’s having average
salary greater than 2000.
1.2 THEORY AND CONCEPTS: A table is the main database object. A table is defines with
columns and stores rows of data. A table should have atleast one column.
Creating tables:
Syntax:
CREATE TABLE <table name>
( <columnname1> datatype(size),
….
<columnname n> datatype(size)
);
Describing a table:
Syntax
DESCRIBE <table name> or DESC <table name>
Adding columns:
Syntax
ALTER TABLE <table name> ADD <columnname> <datatype(size)>;
Modifying columns:
Syntax
ALTER TABLE <table name> MODIFY (<columnname> <new datatype(size)>);
Updating tables:
Syntax
UPDATE TABLE <table name> SET <columnname>=’value’
WHERE <condition>;
Dropping tables:
Syntax
DROP TABLE <table name>;
Renaming tables:
Syntax
RENAME <old table name> TO <new table name>;
Creating constraints: Integrity constraints prevent bad data from being entered into the
database. These constraints can be specified at the column level or at the table level by
CREATE TABLE or ALTER TABLE command. These are as follows:
NOT NULL: prevents NULL values from being entered into the column. It is defined at the
column level. It cannot be defined at the table level.
Syntax
CREATE TABLE <table name>
(<columnname> <datatype(size)> CONSTRAINT <constraint name > NOT NULL);
Or
ALTER TABLE <table name> MODIFY <columnname> NOT NULL;
CHECK: constraint can be defined at the column level as well at the table level. The
condition specified in the CHECK clause should evaluate to a Boolean result and can refer
to values in other columns of the same row; the condition cannot use queries.
Syntax
CREATE TABLE <table name>
(<columnname> <datatype(size)>,
CONSTRAINT CHECK <constraint name> (<condition>));
or
ALTER TABLE <table name> ADD CONSTRAINT <constraint name> CHECK
(<condition>);
UNIQUE: constraint protects one or more columns in a table, ensuring that no two rows
contain duplicate data in the protected columns. It can be specified at the column level as
wella s at the table level.
Syntax
Columns level: CONSTRAINT <constraint name> UNIQUE
Table level: CONSTRAINT <constraint name>
UNIQUE(<columnname1>,….,<columnname n>);
Or
ALTER TABLE <table name> ADD CONSTRAINT <constraint name>
UNIQUE(<columnname1>,….,<columnname n>);
PRIMARY KEY: All characteristics of the unique key are applicable to the primary key
constraint, except the NULL values are not allowed in the primary key columns. A table
can have only one primary key.
Syntax
DBMS Lab (BCA-507(P))
Column level: CONSTRAINT <constraint name> PRIMARY KEY
Table level: CONSTRAINT <constraint name>
PRIMARY KEY (<columnname1>,….,<columnname n>);
Or
ALTER TABLE <table name> ADD CONSTRAINT <constraint name> PRIMARY KEY
(<columnname1>,….,<columnname n>);
FOREIGN KEY: constraint protects one or more columns in a table by ensuring that for
each non-NULL value there is data available elsewhere in the database with a primary or
unique key.
Syntax
Column level: CONSTRAINT <constraint name> REERENCES <table
name>(<columnname1>,….,<columnname n>);
Table level: CONSTRAINT <constraint name>
FOREIGN KEY (<columnname1>,….,<columnname n>)
REERENCES <table name>(<columnname1>,….,<columnname n>);
Or
ALTER TABLE <table name> ADD CONSTRAINT <constraint name>
FOREIGN KEY (<columnname1>,….,<columnname n>)
REERENCES <table name>(<columnname1>,….,<columnname n>);
Dropping constraints:
Syntax
ALTER TABLE <table name> DROP <constraint name>
ASSIGNMENTS:
Assignment 8
Que 1. Create the following tables:
Student
Column Name Data Type Size Constraint(s)
Rollno Number 10 Primary Key
Sname Varchar2 30 Not Null
Status Varchar2 10 Reg/Ex/Readmitted
Sem Number 1 Not Null
DateofReg Date Not Null
Rank Number 10 Unique
Scholarship Number 10 --
BranchNo Varchar2 5 Foreign Key
Department
Column Name Data Type Size Constraint(s)
BranchNo Varchar2 5 Primary Key
BranchName Varchar2 30 Not Null
HOD Varchar2 30 Not Null
Student
Branch
1.2 THEORY AND CONCEPTS: Joint Multiple Table (Equi Join): Sometimes we require to
treat more than one table as though manipulate data from all the tables as though the tables were not
separate object but one single entity. To achieve this we have to join tables. Tables are joined on
column that has dame data type and data with in tables.
The tables that have to be joined are specified in the FROM clause and the joining
attributes in the WHERE clause.
1. Cartesian product:-
2. INNER JOIN:
Cartesian product followed by selection
Select B.*,P.*
FROM student B, Course P
WHERE B.course # P.course # ;
LEFT OUTER JOIN = Cartesian product + selection but include rows from the left
table which are unmatched pat nulls in the values of attributes belonging to th e second
table
Exam:
Select B.*,P*
FROM student B left join course p
ON B.course # P.course #;
Exam:
Select B.*,P.*
From student B RIGHT JOIN course P
B.course# = P course # ;
1.3 ASSIGNMENTS
Assignment 9
1. Find out the product which has been sold to ‘Ivan Sayross.’
2. Find out the product and their quantities that will have do delivered.
3. Find the product no and description of moving products.
4. Find out the names of clients who have purchased ‘CD DRIVE’
5. List the product_no and s_order_no of customers haaving qty ordered less than 5 from the
order details table for the product “1.44 floppies”.
1. Find the products and their quantities for the orders placed by ‘Vandan Saitwal ’ and “Ivan
Bayross”.
2. Find the products and their quantities for the orders placed by client_no “ C00001” and
“C00002”
3. Find the order No,, Client No and salesman No. where a client has been received by more
than one salesman.
4. Display the s_order_date in the format “dd-mm-yy” e.g. “12- feb-96”
5. Find the date , 15 days after date.