2. INDEX
SR NO. EXPERIMENT SIGNATURE
1 DATABASE LANGUAGES
2 Installation Procedure
3 DATA TYPES OF SQL
5 SET OPERATIONS
7 JOINS
8 QUERY PROCESSING
9 CONSTRAINTS
10 FUNCTIONS
11 Table operations
***** ! *****2
3. DATABASE LANGUAGES
DDL – DATA DEFINATION LANGUAGE
The SQL sentences that are used to create these objects are called
DDL’s or Data Definition Language. The sql provides various commands for defining relation schemas,
deleting relations, creating indexes and modify relation schemas. DDL is part of sql which helps a user
in defining the data structures into the database. Following are the various DDL commands are
✓ Alter table & Create table & drop table
✓ Create index & drop index
✓ Create view & drop view
DML – DATA MANIPULATION LANGUAGE
The SQL sentences used to manipulate data within these objects are called DML’s or
Data Manipulation Language. It is language that enables users to
access or manipulate data as organized by appropriate data model. By data manipulation we have
➢ Retrieval of information stored in database.
➢ Insertion of new information into database.
➢ Deletion of information from database.
➢ Modification of data stored in database.
Two types of DML are
✓ Procedural DML
✓ Non-procedural DML
Following are DML commands are
➢Select
➢Update
➢Delete
➢Insert
DCL – DATA CONTROL LANGUAGE
The SQL sentences, which are used to control the behavior of
these objects, are called DCL’s or Data Control Language. It is language used to control data and access
to the database. Following are some DCL commands are
➢ Commit
➢ Rollback
***** ! *****3
4. ➢ DOWNLOADING MySQL [Windows Environment]
Installation file for MySQL may be downloaded from the link:
http://dev.mysql.com/downloads/mysql/5.5.html#downloads
INSTALLING MySQL
Unzip the setup file and execute the download MSI file. Follow the instructions
below exactly when installing MySQL:
Click on the “Next”.
Perform a Typical Installation.
***** ! *****4
6. If you checked the Configure the MySQL Server now check box on the final
dialog of the MySQL Server installation, then the MySQL Server Instance
Configuration Wizard will automatically start. Follow the instructions below
carefully to configure your MySQL Server to run correctly.
***** ! *****6
9. Specify a secure root password, you may want to check the box Enable root access
from remote machines if you plan on administering your MySQL server from your
workstation or other servers.
Done!!!
***** ! *****9
10. DATA TYPES OF SQL
✓ CHAR : This data type is used to store character strings values of fixed length. The size in brack-
ets 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)
✓ 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 val-
ues.
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
***** ! *****10
11. time portion is specified. The default date for a date field is the first day of the current month. Syn-
tax 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)
TRANSACTION STATEMENTS
✓ COMMIT: A COMMIT ends the current transaction and makes permanent any changes made dur-
ing the transaction. All transactional locks acquired on tables are released. Syntax is
COMMIT [work] [comment text];
Where
Work is optional and comment text is used to specify comment
✓ ROLLBACK : A ROLLBACK does exactly the opposite of COMMIT. It ends the transaction but
undoes any changes made during the transaction. All transactional locks acquired on tables are re-
leased. Syntax is
ROLLBACK [WORK] [TO [SAVEPOINT] save point]
Where
WORK is optional and is provided for ANSI compatibility. SAVEPOINT is optional and is used
to rollback a partial transaction, as far as the specified save point. SAVEPOINT is a save point
created during the current transaction.
***** ! *****11
12. SET OPERATIONS
Set keywords are used to combine information of similar type from one or more than one table. Set op-
erations and the operators are based on set theory. It consumes two or more queries into one result. The
types of set operators are:-
➢ UNION : The union clause merges the outputs of multiple queries into a single set of rows and
columns. It combines rows returned by two select statements by eliminating duplicate rows. Syn-
tax is
SELECT <statement> UNION SELECT <statements>;
Example is
SELECT designation FROM emp_info WHERE deptt=’comp’
UNION
SELECT designation FROM emp_info WHERE deptt=’eco’;
➢ INTERSECT : The intersect operator combines two select statements and return only those rows
that are returned by both queries. Syntax is
SELECT <statement> INTERSECT SELECT <statements>;
Example is
SELECT designation FROM emp_info WHERE deptt=’comp’
INTERSECT
SELECT designation FROM emp_info WHERE deptt=’eco’;
➢ MINUS : It combines the result of two queries and returns only those values that are selected by
first query but not in second query. Syntax is
SELECT <statement> MINUS SELECT <statements>;
Example is
SELECT desgination FROM emp_info WHERE deptt=’comp’
MINUS
SELECT desgination FROM emp_info WHERE deptt=’eco’;
***** ! *****12
13. JOINS
A JOIN can be recognized in sql select statement if its has more than one table after from
keyword. This join condition is based on primary keys and foreign keys. There must be n-1 join condi-
tions for n joins to tables. If join condition is omitted then the result is Cartesian product.
Syntax is
SELECT “list of columns” FROM table1, table2 WHERE “condition”;
Types of joins are
✓ Equi join : It returns all rows from tables where there is a match. Tables are joined on columns
that have the same datatype & size in table. It is also known as equality join or simple join or
inner join. Syntax is
SELECTfield1,field2 FROM table1,table2 WHERE table1.field=table2.field;
Example is
SELECTename, dname FROM emp, dept WHERE emp.deptno=dept.deptno;
✓ Cartesion join : When the join condition is omitted the result is Cartesian join of two or more
tables in which all the combinations of rows will be displayed. All the rows are joined to all rows
of the second table. Syntax is
SELECT field1, field2 FROM table1, table2;
Example is SELECTename, dname FROM emp, dept;
✓ Outer join : While using equi join we see that if there exists certain rows in one table which
don’t have corresponding values in the second table thn those rows will not be selected. We can
forcefully select those rows by outer join. The rows for those columns will have NULL values.
Syntax is
SELECT table1.col, table2.col FROM table1, table2
WHERE table1.col (+) = table2.col;
Example is
SELECT empno, ename, emp.deptno, dname FROM emp, dept
WHERE emp.deptno (+) = dept.deptno;
***** ! *****13
14. QUERY PROCESSING
A sub query is a form of an SQL statement that appears inside another SQL
statement. It is also termed as nested query. The statement containing a sub query is called a parent
statement. The parent statement uses the rows returned by the sub query. It can be used by the following
commands:
➢ To insert records in a target table.
➢ To create tables and insert records in the table created.
➢ To update records in a target table.
➢ To create views.
➢ To provide values for conditions in WHERE, HAVING, IN etc. used with SELECT, UP-
DATE, and DELETE statements.
Types of sub queries are
✓ Single row : It returns one row from inner nested query. Example is
SELECT deptno FROM emp WHERE ename =’MILLER’;
✓ Multiple row : Subqueries that return more than one row called multiple row queries. Operators
like IN,ALL,ANY are used. Example is
SELECT ename,sal,deptno FROM emp WHERE sal IN
(SELECT min(sal) FROM emp GROUP BY deptno);
✓ Multiple column : It returns more than one column .Example is
SELECT ordid , prodid , qty FROM item WHERE (qty,prodid) IN
(SELECT prodid,qty FROM item WHERE ordid = 605 AND ordid <> 605);
***** ! *****14
15. CONSTRAINTS
Constraints are the rules that restrict the data values inserted in columns
of a table. It helps in maintaining integrity of table at database level instead of application level. All ap-
plication can take advantage of these global constraints, rather than building similar logic in individual
application. Constraints can constrain single column or group of columns in a table. Constraints can be
specified as two types:
✓ Column level constraint
✓ Table level constraint
Column level constraint : Column constraints are specified as part of column
definition to constrain single column.
Table level constraint : Table constraints is specified at end of create table
statement to constraint more than one column.
Following are the constraints used in tables are
✓ NULL/NOT NULL : Specifies if the column must contain value or might not contain any. By de-
fault all columns in a table allows nulls, ie. Absence of a value in a column. NOT NULL specifies
that all rows in the table to have value for specified column. All NOT NULL columns are mandato-
ry fields. Syntax is
COLUMN_NAME DATA_TYPE(SIZE) NOT NULL
Example is
CREATE TABLE employee(empname varchar2(25) NOT NULL) ;
✓ UNIQUE : Specifies that all values in a column must be unique. Values in field cannot be duplicat-
ed. More than one Null value can be inserted in the column. Unique constraint can be placed on
multiple columns. Syntax is
COLUMN_NAME DATA_TYPE(SIZE) UNIQUE -- at column level
UNIQUE COLUMN_NAME DATA_TYPE(SIZE) -- at table level
Example is
CREATE TABLE dept(deptt varchar2(25) UNIQUE);
CREATE TABLE dept(deptt varchar2(25),UNIQUE(dept));
***** ! *****15
16. ✓ PRIMARY KEY : Primary key refers to one or more column values in a table that can uniquely
identify each row in a table. A primary key column cannot contain nulls and is unique by default.
Syntax is
COLUMN DATA_TYPE(SIZE) PRIMARY KEY -- at column level
PRIMARY KEY COLUMN DATA_TYPE(SIZE) -- at table level
Example is
CREATE TABLE dept(deptt varchar2(25) PRIMARY KEY);
CREATE TABLE dept(deptt varchar2(25),PRIMARY KEY(dept));
✓ FOREIGN KEY : A foreign key is a one or more column with values based on the primary key
values from another table. A foreign key constraint specifies that the values stored in foreign col-
umn correspond to values of the primary key in the other table. Foreign key is also known as refer-
ential integrity constraint. The table containing foreign key is known as Dependent table and the
table that is referenced by foreign key is called Referenced table. Syntax is
COLUMN DATA_TYPE(SIZE) REFERENCES TABLE -- at column level
FOREIGN KEY COLUMN DATA_TYPE(SIZE) REFERENCES TABLE
Example is
CREATE TABLE dept(deptt varchar2(25) REFERENCES emp_info);
CREATE TABLE dept(deptt varchar2(25),PRIMARY KEY(dept) FOREIGN KEY(dept) REF-
ERENCES emp_info);
***** ! *****16
17. FUNCTIONS
➢ Count : This function returns the number of rows or non-null values for column x. Syntax is
count([distinct|all]column name)
Example is select count(empno)from emp;
➢ Sum : This function ireturns the sum of values for the column x. This function is applied on col-
umns having numeric datatype and it returns the numeric value.
Syntax is sum([distinct|all]column name)
Example is select sum(sal) from emp;
➢ Avg : Ths function returns the average of values for the column x. It ignores the null values in the
column x. Syntax is avg([distinct|all]column name)
Example is select avg(sal),count(sal) from emp;
➢ Min : This function returns the minimum of values for the column x for all the rows. Syntax is
min([distinct|all]column name)
Example is select min(sal) from emp;
➢ Max : This function returns the maximum of values for the column x for all the rows. Syntax is
max([distinct|all]column name)
Example is select min(sal),max(sal) from emp;
Fiction Examples
***** ! *****17
18. Select avg (Price) , count(ProductID),MAX(Price) AS HighestPrice from Products ;
***** ! *****18
19. General Commands for tables
Show Table
SELECT * FROM Customers;
Creation of Table
✓ CREATE TABLE: A table is basic unit of storage. It is composed of rows and columns. To cre-
ate a table we will name the table and the columns of the table. We follow the rules to name ta-
bles and columns:-
Syntax to create a table
is
CREATE TABLE tablename (column_name1 datatype (size), column_name2 datatype
(size) …);
***** ! *****19
21. Insertion of Data
✓ 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 Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21’,'Stavanger','4006','Norway');
SQL DELETE Syntax
✓ 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 Customers
WHERE CustomerName = ‘Bergulunds';
***** ! *****21
22. SQL UPDATE Syntax
✓ 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;
UPDATE Customers SET City=‘Hamburg’
WHERE ContactName = 'Alfreds Futterkiste’;
***** ! *****22