Dbms Lab
Dbms Lab
UGCA 1925
DBMS Lab
Currently, its database comes in five different editions based on the features available.
Features of Oracle:
An Oracle database offers the following features to meet the requirements of powerful
database management:
Scalability and Performance: Features like Real Application Clustering and Portability make an
Oracle database scalable according to the usage. In a multiuser database, it is required to
control data consistency and concurrency which are contemplated by Oracle.
Importance of Oracle:
It is among the oldest companies which provide database management solutions. The company
has always focused on Enterprise requirements and acknowledged the latest technology trends.
That’s why its products are always embellished with new features. For instance, the latest
Oracle database 19C is available on Oracle Cloud as well. Oracle offers users to choose from the
different database editions which suit their needs in order to provide a cost-effective solution.
History Of Oracle:
Oracle is most widely used Database Management System in most of the Companies.Oracle uses
SQL pronounced as ‘Sequel’ language to data manipulation and operations on data.I am
explaining History of Oracle in this article. ‘SEQUEL’ stands for ‘Structured English Query
Language’ which is developed by IBM Corporation limited to use codd’s model in 1979.In 1979
Oracle introduced first commercially available implementation of SQL.Now SQL is called as
Standard RDBMS language.Oracle Corporation was started by two computer programmers,
Larry Ellison and Bob Miner in 1977. Both Larry and Bob had prior experience in building
database programs for different companies. Their first project was building a special database
program for the CIA (Central Intelligence Agency).
SQL is the standard language for Relational Database System. All the Relational Database
Management Systems (RDMS) like MySQL, MS Access, Oracle, Sybase, Informix, Postgres and
SQL Server use SQL as their standard database language.
In the opened page you can see the “Sign In” option. Click on it and you will get the following
drop down
Click on the “Sign up for an Oracle Account” and you will get this form
Fill this form with all your details. Be sure to fill those with *. Once you complete this form click
on “Create Account” button and your account will be created. You will receive a mail from
Oracle in your mentioned email account.
Open the mail box and click on “Confirm Email” link and you will be taken back to Oracle page.
Now in your browser open link https://livesql.oracle.com/ and log in with your user name and
password you just used to create your account.
You will log in your Oracle Account and see this interface
Click on SQL Worksheet and you will open the editor interface. Here you can write your
assignment queries and run them by clicking on the run button.
SQL Commands
The standard SQL commands to interact with relational databases are CREATE, SELECT, INSERT,
UPDATE, DELETE and DROP. These commands can be classified into the following groups based
on their nature −
1. Numeric data types such as int, tinyint, bigint, float, real, etc.
2. Date and Time data types such as Date, Time, Datetime, etc.
3. Character and String data types such as char, varchar, text, etc.
4. Unicode character string data types, for example nchar, nvarchar, ntext, etc.
5. Binary data types such as binary, varbinary, etc.
6. Miscellaneous data types – clob, blob, xml, cursor, table, etc.
Not all data types are supported by every relational database vendor. For example,
Oracle database doesn’t support DATETIME and MySQL doesn’t support CLOB data type. So
while designing database schema and writing SQL queries, make sure to check if the data types
are supported or not.
Data types listed here doesn’t include all the data types, these are the most popularly
used data types. Some relational database vendors have their own data types that might be not
listed here. For example, Microsoft SQL Server has money and smallmoney data types but since
it’s not supported by other popular database vendors, it’s not listed here.
Every relational database vendor has its own maximum size limit for different data
types, you don’t need to remember the limit. Idea is to have the knowledge of what data type
to be used in a specific scenario.
Datatype From To
Bit 0 1
tinyint 0 255
Datatype Description
DATETIME Stores date and time information in the format YYYY-MM-DD HH:MI:SS
TIMESTAM Stores number of seconds passed since the Unix epoch (‘1970-01-01 00:00:00’
P UTC)
Stores year in 2 digits or 4 digit format. Range 1901 to 2155 in 4-digit format.
YEAR
Range 70 to 69, representing 1970 to 2069.
Datatype Description
Note that all the above data types are for character stream, they should not be used with
Unicode data.
NVARCHAR(max
Variable-length storage with provided max characters
)
Note that above data types are not supported in MySQL database.
Datatype Description
Datatype Description
CLOB Character large objects that can hold up to 2GB
Syntax:
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
....
);
The column parameters specify the names of the columns of the table.The datatype parameter
specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).
Example:
Output:
PERSONID - NUMBER
LASTNAME - VARCHAR2(255)
FIRSTNAME - VARCHAR2(255)
ADDRESS - VARCHAR2(255)
CITY - VARCHAR2(255)
Integrity Constraints in SQL
When designing the database schema, integrity constraints are added. It defines constraints in
SQL DDL commands such as 'CreateTable' and 'Alter Table.'
Integrity Constraints are the protocols that a table's data columns must follow. These are used
to restrict the types of information that can be entered into a table. This means that the data in
the database is accurate and reliable. You may apply integrity Constraints at the column or
table level. The table-level Integrity constraints apply to the entire table, while the column level
constraints are only applied to one column.
The NOT NULL constraint prevents a column from having a NULL value.
When no value is defined for a column, the DEFAULT Constraint provides a default
value.
The not null constraint tells a column that it can't have any null values in it. This is also a type of
integrity constraint. This forces a field to always have a value, meaning you can't create a new
record or change an existing one without adding a value to it.
Syntax:
(
Column_name2 datatype,
......
Column_namendatatype,
)
Example:
);
Output:
Example:
);
Output:
Primary keys must contain UNIQUE values, and cannot contain NULL values.
A table can have only ONE primary key; and in the table, this primary key can consist of single
or multiple columns (fields).
Example:
FirstName varchar(255),
Age int,
);
Output:
A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in
another table.
Example:
FirstName varchar(255),
Age int,
);
values(1,'Khatta','Ruby', 19 )
values(2,'Singh','Manmeet', 20 )
values(3,'Kaur','Jagdeep', 19 )
PersonID int,
PRIMARY KEY (OrderID),
);
values(1,4667,3)
values(2,4668,2)
output:
PERSONI
D LASTNAME FIRSTNAME AGE
1 Khatta Ruby 19
2 Singh Manmeet 20
3 Kaur Jagdeep 19
2 4668 2
1 4667 3
Check Constraints in MySQL
The Check Constraint is used to enforce domain integrity. Domain integrity means the values
that are going to store in a table column must be followed by some defined rules such as range,
type, and format. In other words, we can say that Check Constraint ensures the valid entries for
a given column value by restricting the type of the value, the format of the data, or the range of
possible values.
Syntax:
Column 1 datatype,
Column 2 datatype,
Example:
description VARCHAR(40),
);
insert into parts
values('AB1','ice cream',20,10)
values('AB2','kurkure',30,10)
values('AB3','chips ',40,50)
values('AB4','coffee',20,-10)
Output:
PART_N
O DESCRIPTION COST PRICE
The ALTER TABLE statement is also used to add and drop various constraints on an existing
table.
syntax:
Example:
City varchar2(20)
desc state;
Output:
NOT
SNO VARCHAR2(5)
NULL
SNAME - VARCHAR2(20)
CITY - VARCHAR2(20)
COUNTRY - VARCHAR2(30)
DROP TABLE Statement
The DROP TABLE statement allows you to remove or delete a table from the SQL database.
Syntax:
DROP TABLE table_name;
Example:
City varchar2(20)
output:
Table dropped.
Relational Algebra
Relational algebra is a procedural query language. It gives a step by step process to obtain the
result of the query. It uses operators to perform queries.
Notation: σ p(r)
Notation: σ p(r)
Where:
1. σ BRANCH_NAME="perryride" (LOAN)
σ BRANCH_NAME="perryride" (
Output:
2. Project Operation:
o This operation shows the list of those attributes that we wish to appear in the result.
Rest of the attributes are eliminated from the table.
o It is denoted by ∏.
1. Notation: ∏ A1, A2, An (r)
Notation: ? A1, A2, An (r)
Where
Input:
1. ∏ NAME, CITY (CUSTOMER)
? NAME, CITY (CUSTOMER)
Output:
NAME CITY
Jones Harrison
Smith Rye
Hays Harrison
Curry Rye
Johnson Brooklyn
Brooks Brooklyn
3. Union Operation:
o Suppose there are two tuples R and S. The union operation contains all the tuples that
are either in R or S or both in R & S.
o It eliminates the duplicate tuples. It is denoted by ∪.
?
1. Notation: R ∪ S
Notation: R S
Example:
DEPOSITOR RELATION
CUSTOMER_NAME ACCOUNT_NO
Johnson A-101
Smith A-121
Mayes A-321
Turner A-176
Johnson A-273
Jones A-472
Lindsay A-284
BORROW RELATION
CUSTOMER_NAME LOAN_NO
Jones L-17
Smith L-23
Hayes L-15
Jackson L-14
Curry L-93
Smith L-11
Williams L-17
Input:
?
1. ∏ CUSTOMER_NAME (BORROW) ∪ ∏ CUSTOMER_NAME (DEPOSITOR)
? CUSTOMER_NAME (BORROW) ? CUSTOMER_NAME (DEPOSITOR)
Output:
CUSTOMER_NAME
Johnson
Smith
Hayes
Turner
Jones
Lindsay
Jackson
Curry
Williams
Mayes
4. Set Intersection:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples
that are in both R & S.
o It is denoted by intersection ∩.
1. Notation: R ∩ S
Notation: R n S
Input:
1. ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
? CUSTOMER_NAME (BORROW
Output:
CUSTOMER_NAME
Smith
Jones
5. Set Difference:
o Suppose there are two tuples R and S. The set intersection operation contains all tuples
that are in R but not in S.
o It is denoted by intersection minus (-).
1. Notation: R - S
Notation: R - S
Input:
1. ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)
? CUSTOMER_NAME (BORROW
Output:
CUSTOMER_NAME
Jackson
Hayes
Willians
Curry
6. Cartesian product:
o The Cartesian product is used to combine each row in one table with each row in the
other table. It is also known as a cross product.
o It is denoted by X.
1. Notation: E X D
Notation: E X D
Example:
EMPLOYEE
1 Smith A
2 Harry C
3 John B
DEPARTMENT
DEPT_NO DEPT_NAME
A Marketing
B Sales
C Legal
Input:
1. EMPLOYEE X DEPARTMENT
EMPLOYEE X DEPARTMENT
Output:
1 Smith A A Marketing
1 Smith A B Sales
1 Smith A C Legal
2 Harry C A Marketing
2 Harry C B Sales
2 Harry C C Legal
3 John B A Marketing
3 John B B Sales
3 John B C Legal
7. Rename Operation:
The rename operation is used to rename the output relation. It is denoted by rho (ρ).
Example: We can use the rename operator to rename STUDENT relation to STUDENT1.
1. ρ(STUDENT1, STUDENT)
Join Operations
A Join operation combines related tuples from different relations, if and only if a given join
condition is satisfied. It is denoted by ⋈.
Example:
EMPLOYEE
EMP_CODE EMP_NAME
101 Stephan
102 Jack
103 Harry
SALARY
EMP_CODE SALARY
101 50000
102 30000
103 25000
?
1. Operation: (EMPLOYEE ⋈ SALARY)
Operation: (EMPLOYEE SALARY)
Result:
1. Natural Join:
o A natural join is the set of tuples of all combinations in R and S that are equal on their
common attribute names.
o It is denoted by ⋈.
Example: Let's use the above EMPLOYEE table and SALARY table:
Input:
?
1. ∏EMP_NAME, SALARY (EMPLOYEE ⋈ SALARY)
? EMP_NAME, SALARY (EMPLOYEE SALARY)
Output:
EMP_NAME SALARY
Stephan 50000
Jack 30000
Harry 25000
2. Outer Join:
The outer join operation is an extension of the join operation. It is used to deal with missing
information.
Example:
EMPLOYEE
FACT_WORKERS
Input:
?
1. (EMPLOYEE ⋈ FACT_WORKERS)
(EMPLOYEE FACT_WORKERS)
Output:
Input:
?
1. EMPLOYEE ⟕ FACT_WORKERS
EMPLOYEE FACT_WORKERS
Input:
?
1. EMPLOYEE ⟖ FACT_WORKERS
EMPLOYEE FACT_WORKERS
Output:
Input:
?
1. EMPLOYEE ⟗ FACT_WORKERS
EMPLOYEE FACT_WORKERS
Output:
3. Equi join:
It is also known as an inner join. It is the most common join. It is based on matched data as per
the equality condition. The equi join uses the comparison operator(=).
Example:
CUSTOMER RELATION
CLASS_ID NAME
1 John
2 Harry
3 Jackson
PRODUCT
PRODUCT_ID CITY
1 Delhi
2 Mumbai
3 Noida
Input:
?
1. CUSTOMER ⋈ PRODUCT
CUSTOMER PRODUCT
Output:
1 John 1 Delhi
2 Harry 2 Mumbai
3 Harry 3 Noida