Database Lab Assignment 1: Study of M SQL & Oracle
Database Lab Assignment 1: Study of M SQL & Oracle
DATABASE LAB
ASSIGNMENT 1
Study of MYSQL & ORACLE
Database lab Manual
Computer Department, SCOE.
A. INTRODUCTION TO MySQL
MYSQL
It is freely available open source Relational Database Management System (RDBMS) that uses Structured Query
Language(SQL). In MySQL database , information is stored in Tables. A single MySQL database can contain many
tables at once and store thousands of individual records.
FEATURES OF MySQL
SQL is a language that enables you to create and operate on relational databases, which are sets of related
information stored in tables.
A data model refers to a set of concepts to describe the structure of a database , and certain constraints (restrictions)
that the database should obey. The four data model that are used for database management are :
1. Relational data model: In this data model, the data is organized into tables (i.e. rows and columns). These tables
are called relations.
2. Hierarchical data model 3. Network data model 4. Object oriented data model
6. Primary Key : This refers to a set of one or more attributes that can uniquely identify tuples within the relation.
7. Candidate Key : All attribute combinations inside a relation that can serve as primary key are candidate keys as
these are candidates for primary key position.
8. Alternate Key : A candidate key that is not primary key, is called an alternate key.
9. Foreign Key : A non-key attribute, whose values are derived from the primary key of some other table, is known
as foreign key in its current table.
REFERENTIAL INTEGRITY
- A referential integrity is a system of rules that a DBMS uses to ensure that relationships between records in
related tables are valid, and that users don’t accidentally delete or change related data. This integrity is
ensured by foreign key.
Database lab Manual
Computer Department, SCOE.
CLASSIFICATION OF SQL STATEMENTS
PROCEDURAL DML
It requires a user to specify what data is needed and how to get it.
NON PROCEDURAL DML
It require a user to specify what data is needed without specifying how to get it.
MySQL ELEMENTS
LITERALS
It refer to a fixed data value. This fixed data value may be of character type or numeric type. For example, ‘replay’ ,
‘Raj’, ‘8’ , ‘306’ are all character literals.
Numbers not enclosed in quotation marks are numeric literals. E.g. 22 , 18 , 1997 are all numeric literals.
Numeric literals can either be integer literals i.e., without any decimal or be real literals i.e. with a decimal point
e.g. 17 is an integer literal but 17.0 and 17.5 are real literals.
DATA TYPES
Data types are means to identify the type of data and associated operations for handling it. MySQL data types are
divided into three categories:
Numeric
Date and time
String types
NULL VALUE
If a column in a row has no value, then column
is said to be null , or to contain a null. You
should use a null value when the actual value is
not known or when a value would not be
meaningful.
Database lab Manual
Computer Department, SCOE.
DATABASE COMMNADS
IN MYSQL
- Tables are created with the CREATE TABLE command. When a table is created, its columns are named, data
types and sizes are supplied for each column.
Syntax of CREATE TABLE command
is : CREATE TABLE <table-name>
( <column name> <data type> ,
<column name> <data type> ,
……… ) ;
e.g. to enter a row into EMPLOYEE table (created above), we write command as :
INSERT INTO employee VALUES(1001 , ‘Ravi’ , ‘M’ , ‘E4’ , 50000);
OR
INSERT INTO employee (ECODE , ENAME , GENDER , GRADE , GROSS) VALUES(1001 , ‘Ravi’ , ‘M’ , ‘E4’ , 50000);
In order to insert another row in EMPLOYEE table , we write again INSERT command :
INSERT INTO employee VALUES(1002 , ‘Akash’ , ‘M’ , ‘A1’ , 35000);
- To insert value NULL in a specific column, we can type NULL without quotes and NULL will be inserted in that
column. E.g. in order to insert NULL value in ENAME column of above table, we write INSERT command as :
you can modify data in tables using UPDATE command of SQL. The UPDATE command specifies the rows to be
changed using the WHERE clause, and the new data using the SET keyword. Syntax of update command is :
UPDATE <tablename> SET <columnname>=value , <columnname>=value WHERE <condition> ;
e.g. to change the salary of employee of those in EMPLOYEE table having employee code 1009 to 55000. UPDATE
EMPLOYEE SET GROSS = 55000 WHERE ECODE = 1009 ;
So if we do not specify any condition with WHERE clause, then all the rows of the table will be deleted. Thus above
line will delete all rows from employee table.
DROPPING TABLES
The DROP TABLE command lets you drop a table from the database. The syntax of DROP TABLE command is :
DROP TABLE <tablename> ;
e.g. to drop a table employee, we need to write :
DROP TABLE employee ;
EMPLOYEE
ECODE ENAME GENDER GRADE GROSS
1001 Ravi M E4 50000
1002 Akash M A1 35000
1004 Neela F B2 38965
1005 Sunny M A2 30000
1006 Ruby F A1 45000
1009 Neema F A2 52000
Database lab Manual
Computer Department, SCOE.
SELECTING ALL DATA
- In order to retrieve everything (all columns) from a table, SELECT command is used
as : SELECT * FROM <tablename> ;
e.g. In order to retrieve everything from Employee table, we write SELECT command as :
SELECT * FROM Employee ;
RELATIONAL OPERATORS
- To compare two values , a relational operator is used. The result of the comparison is true or false. The
SQL recognizes following relational operators:
= < > <= >= < > (Not equal to).
LOGICAL OPERATORS
- The logical operator OR ( || ), AND (&&) and NOT (!) are used to connect search conditions in the WHERE clause.e.g.
1. To list the employee details having grade ‘A1’ or ‘A2’ from table employee, logical operator OR will be
used as : SELECT * FROM EMPLOYEE WHERE GRADE=’A1’ OR GRADE =’A2’ ;
2. To list the employee details having grades as A1 and salary greater than 40000, logical operator AND will be
used as: SELECT * FROM EMPLOYEE WHERE GRADE=’A1’ AND GROSS > 40000;
3. To list the employee details whose grades are other than ‘A1’, logical operator NOT will be used as :
GENDER
M
M
Database lab Manual
Computer Department, SCOE.
F
M
F
F
DISTINCT(GENDER)
M
F
e.g. to display ECODE, ENAME and GRADE of those employees whose salary is between 40000 and 50000,
command is:
SELECT ECODE , ENAME ,GRADE FROM EMPLOYEE WHERE GROSS BETWEEN 40000 AND 50000 ;
Output will be :
Output will be :
- The NOT IN operator finds rows that do not match in the list. E.g.
SELECT * FROM EMPLOYEE WHERE GRADE NOT IN (‘A1’ , ‘A2’);
Output will be :
ECODE ENAME GENDER GRADE GROSS
1001 Ravi M E4 50000
1004 Neela F B2 38965
Output will be :
Output will be :
--to display the names of those students whose marks is NULL, we use the command :
SELECT Name FROM EMPLOYEE WHERE Marks IS NULL ;
Output will be :
Name
ARUN
SANJAY
SORTING RESULTS
Whenever the SELECT query is executed , the resulting rows appear in a predefined order. The ORDER BY clause allow
sorting of query result. The sorting can be done either in ascending or descending order, the default is ascending.
e.g. to display the details of employees in EMPLOYEE table in alphabetical order, we use command :
SELECT * FROM EMPLOYEE ORDER BY ENAME ;
Output will be :
ECODE ENAME GENDER GRADE GROSS
1002 Akash M A1 35000
1004 Neela F B2 38965
1009 Neema F A2 52000
1001 Ravi M E4 50000
1006 Ruby F A1 45000
1005 Sunny M A2 30000
e.g. display list of employee in descending alphabetical order whose salary is greater than 40000. SELECT
ENAME FROM EMPLOYEE WHERE GROSS > 40000 ORDER BY ENAME desc ;
Output will be :
ENAME
Ravi
Ruby
Neema
Database lab Manual
Computer Department, SCOE.
B. INTRODUCTION TO ORACLE.
ORACLE
Oracle is one of the powerful RDBMS products that provide efficient solutions for
database applications. Oracle is the product of Oracle Corporation which was founded by
LAWRENCE ELLISION in 1977. The first commercial product of oracle was delivered in 1970.
The first version of oracle 2.0 was written in assembly language. Nowadays commonly used
versions of oracle are ORACLE 8, 8i & 9i Oracle 8 and onwards provide tremendous increase in
performance, features and functionality.
FEATURES OF ORACLE
Client/Server Architecture
Large database and Space Management
Concurrent Processing
High transaction processing performance
High Availability
Many concurrent database users
Controlled availability
Openness industry standards
Manageable security
Database enforced integrity
Distributed systems
Portability
Compatibility
ORACLE SERVER TOOL
Oracle is a company that produces most widely used server based multi-user RDBMS.
Oracle server is a program installed on server hard-disk drive. This program must be loaded in
RAM to that it can process the user requests. Oracle server takes care of following functions.
Oracle server tools are also called as back end. Functions of server tool:
updates the data
SQL*Plus - A command line tool used to manipulate tables and other database objects in
an Oracle database.
DeveloperA suite of application development tools including Forms, Reports and Graphics.
Oracle Forms - A screen based tool used to develop data entry forms and menus that
access tables in an Oracle database.
Oracle Reports - A screen based tool used to develop reports that access tables in an
Oracle database.
Oracle Graphics - A graphical tool used to develop charts and reports that access tables in an
Oracle database.
Oracle JDeveloper - A general purpose Java Integrated Development Environment that has been
pre-loaded with classes and methods used to connect to and manipulate schemas in Oracle
databases. A collection of code development wizards allow the developer to quickly create data
entry forms as Java applications or applets as well as reports using Java Server Pages (JSP).
Oracle Designer - A graphical tool used to create and display models contained in the
CASE*Dictionary. The CASE*Dictionary is a repository for business rules, functional
models and data models used for organizing and documenting an application development
effort. CASE*Generator is a code generating tool that uses information stored in
CASE*Dictionary to develop data entry forms, reports and graphics.
Programmer - Including the Pro* precompilers - Libraries of routines and utilities that can
be linked with “C”, C++, FORTRAN, Java, ADA, COBOL or other host languages to
allow access to Oracle databases.
RUNNING SQL*PLUS
In this section, we give some general directions on how to get into the SQL*Plus program and
connect to an Oracle database. Specific instructions for your installation may vary depending on
the version of SQL*Plus being used, whether or not SQL*Net or Net8 is in use, etc.
Some tips on Obtaining and Installing Oracle Software can be found on this link.
If you have the Oracle Express Edition installed, this link. will describe how to run SQL
Commands in Oracle Application Express.
Before using the SQL*Plus tool or any other development tool or utility, the user must obtain an
Oracle account for the DBMS. This account will include a username, a password and, optionally,
a host string indicating the database to connect to. This information can typically be obtained
from the database administrator.
Database lab Manual
Computer Department, SCOE.
The following directions apply to two commonly found installations: Windows XP or Windows
7 client (from here on, referred to simply as a Windows client) with an Oracle server, and a
UNIX/LINUX installation.
In the Password: field, type your Oracle password. Press the TAB key to move to the next field.
In the Host String: field, type in the SERVICE NAME of the Oracle host to connect to.
If the DBMS is Personal Oracle lite then this string might be ODBC:POLITE. If the DBMS is a
local Personal Oracle8, 8i or 9i database, then the host string might be either beq-local or in some
cases, you can leave this field blank to connect to your local database instance. Make certain
your local instance is started. For Client/Server installations with SQL*Net or Net8, this string
will be the service name set up by the SQL*Net or Net8 assistant software.
Finally, click on the OK button to complete the Oracle log in process. SQL*Plus will then
establish a SESSION with the Oracle DBMS and the SQL*Plus prompt (SQL> ) will appear. The
following figure shows the results of logging into Oracle using SQL*Plus:
CHAR: 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
characters (i.e. the size) this data type can hold is 255 characters. Syntax is CHAR(SIZE)
Example is CHAR (20)
Database lab Manual
Computer Department, SCOE.
VARCHAR: This data type is used to store variable length alphanumeric data. The
maximum this data type can hold is 4000 characters. One difference between this data type and
the CHAR data type is ORACLE compares VARCHAR values using non-padded comparison
semantics i.e. the inserted values will not be padded with spaces. Syntax is VARCHAR(SIZE)
Example is VARCHAR (20) OR VARCHAR2 (20)
NUMBER: The NUMBER data type is used to store numbers (fixed or floating point).
Numbers of virtually any magnitude maybe stored up to 38 digits of precision. Numbers as
large as 9.99 * 10 to the power of 124, i.e. followed by 125 zeros can be stored. The precision,
(P), determines the maximum length of the data, whereas the scale, (S), determines the number
of places to the right of the decimal. If scale is omitted then the default is zero. If precision is
omitted values are stored with their original precision up to the maximum of 38 digits. Syntax
is NUMBER (P, S) Example is NUMBER (10, 2)
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 to
LONG values Syntax is LONG (SIZE) Example is LONG (20)
DATE: This data type is used to represent data and time. The standard format id DD-
MM-YY as in 13-JUL-85. To enter dates other than the standard format, use the appropriate
functions. Date
Time stores date in the 24-hour 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 of the current
month. Syntax is DATE
LONG RAW: LONG RAW data types are 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. LONG RAW data type can contain up to 2GB. Values stored in columns having
LONG RAW data type cannot be indexed. Syntax is LONGRAW (SIZE)
RAW: It is used to hold strings of byte oriented data. Data type can have a maximum length
of 255 bytes. Syntax is RAW(SIZE)
Database lab Manual
Computer Department, SCOE.
SQL COMMANDS
CREATE TABLE: A table is basic unit of storage. It is composed of rows and columns.
To create a table we will name the table and the columns of the table. We follow the rules to
name tables and columns:-
O It must begin with a letter and can be up to 30 characters long.
Example is
O CREATE TABLE student (rollno number (4), name varchar2 (15));
Database lab Manual
Computer Department, SCOE.
SELECT : The select command of sql lets you make queries on the database. A query is a
command that is given certain specified information from the database tables. It can be used to
retrieve a subset of rows or columns from one or more tables.
Syntax to create a table is
SELECT <column_name1>,<column_name2> FROM <tablename>;
Example is SELECT empno, ename, sal from emp;
Database lab Manual
Computer Department, SCOE.
DESCRIBE : To find information about columns like column name, their data types
and other attributes of a table we can use DESCRIBE command. Syntax to describe table
is DESCRIBE tablename;
ALTER TABLE: After creating a table one may have need to change the table either by
add new columns or by modify existing columns. One can do so by using alter table command.
Syntax to add a column is
ALTER TABLE tablename ADD(col1 datatype,col2 datatype);
Syntax to modify a column is
ALTER TABLE tablename MODIFY(col1 datatype,col2 datatype);
Database lab Manual
Computer Department, SCOE.
DELETE: One can delete data fron table by using delete from statement. The delete
statement removes rows from table but it doesn’t release storage space. Syntax of delete rows
from table is DELETE FROM tablename WHERE <condition>;
INSERT: To add new rows in an existing oracle table the insert command is used. Syntax to
add new fields is
INSERT INTO tablename(col1,col2,col3,..)
VALUES(value1,value2,value3);
Example is
INSERT INTO employee(emp_id,ename,desg,basic_pay)
VALUES(100001,’MOHIT’,’MANAGER’,55000);
Database lab Manual
Computer Department, SCOE.
UPDATE: The update command enables user to change the values of existing
rows. Syntax to
update value of table is
UPDATE tablename SET col1=value1,col2=value2;
Example is
UPDATE emp_info SET salary =salary +100;
DROP TABLE: To remove the definition of oracle table, the drop table statement is
used.
Syntax to drop table is
DROP TABLE tablename
RENAME : One can change the name of a table by rename command Syntax to
rename table is
====================================================================