Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Definition Language (DDL) (Create, Truncate, Alter, Rename, Drop, Comment)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 38

1

Ex.No: 1.1
DATA DEFINITION LANGUAGE (DDL)
Date: (CREATE,TRUNCATE,ALTER, RENAME, DROP, COMMENT)

Aim:

To execute the various Data Definition Language commands in RDBMS.


Algorithm:

Step 1: Start the program


Step 2: Go to MySQL.
Step 3: Enter the user name and password.
Step 4: Connect to the database.
Step 5: Type the commands for creating tables and perform various operations on the tables.
Step 6: The output is displayed.
Step 7: Stop the program
DDL:
MySQL DDL (Data Definition Language) is used to define data in database server. DDL statements
explains, how the pattern of data should be there in the database schema table.
MySQL DDL statements are classified into the following types:

 Create
 Alter
 Drop
 Rename
 Truncate

CREATE:
Syntax:

Create table <table_name>(<column_name1>datatype (size),<column_name2>datatype(size)………….);


Example:
mysql> createtablestudent(stu_idint,stu_namevarchar(255),fee int);
Query OK, 0 rowsaffected (0.30 sec)

ALTER: ( Add)
Syntax:

ADD(<column_name1>datatype(size),<column_name2>datatype(size));
Example:
mysql> altertablestudent add(fee int);
Query OK, 0 rowsaffected (0.64 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
mysql>descstudent;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null| Key| Default| Extra |
+------------+--------------+------+-----+---------+-------+
| stu_id     | int(11)      | YES  |     | NULL    |       |
2

| stu_name   | varchar(255) | YES  |     | NULL    |       |


| stu_result | varchar(255) | YES  |     | NULL    |       |
| branch     | varchar(255) | YES  |     | NULL    |       |
| fee        | int(11)      | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
5 rowsinset(0.00 sec)

ALTER: (Modify)
Syntax:

modify(<column_name>datatype(size));
Example:
mysql>descstudent;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null| Key| Default| Extra |
+------------+--------------+------+-----+---------+-------+
| stu_id     | int(11)      | YES  |     | NULL    |       |
| stu_name   | varchar(255) | YES  |     | NULL    |       |
| stu_result | varchar(255) | YES  |     | NULL    |       |
| branch     | varchar(250) | YES  |     | NULL    |       |
| fee        | int(11)      | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
5 rowsinset(0.00 sec)
 
mysql> altertablestudent modifybranch varchar(255);
Query OK, 0 rowsaffected (0.50 sec)
Records: 0  Duplicates: 0  Warnings: 0
 
mysql>descstudent;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null| Key| Default| Extra |
+------------+--------------+------+-----+---------+-------+
| stu_id     | int(11)      | YES  |     | NULL    |       |
| stu_name   | varchar(255) | YES  |     | NULL    |       |
| stu_result | varchar(255) | YES  |     | NULL    |       |
| branch     | varchar(255) | YES  |     | NULL    |       |
| fee        | int(11)      | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
5 rowsinset(0.00 sec)

Drop:
Syntax:

drop table <table name>;


Example:
mysql>descstudent;
+------------+--------------+------+-----+---------+-------+
| Field      | Type         | Null| Key| Default| Extra |
+------------+--------------+------+-----+---------+-------+
| stu_id     | int(11)      | YES  |     | NULL    |       |
| stu_name   | varchar(255) | YES  |     | NULL    |       |
| stu_result | varchar(255) | YES  |     | NULL    |       |
| branch     | varchar(255) | YES  |     | NULL    |       |
+------------+--------------+------+-----+---------+-------+
4 rowsinset(0.00 sec)
 
3

mysql>droptablestudent;
Query OK, 0 rowsaffected (0.48 sec)
 
mysql> select* fromstudent;
ERROR 1146 (42S02): Table'employee.student'doesn't exist

RENAME:
SYNTAX:

Rename old <table_name> to new <table_name>;


EXAMPLE:
mysql> select* fromemployee;
+--------+--------+-----------+-------+------------+
| emp_id | ename  |   job     |salary | commission |
+--------+--------+-----------+-------+------------+
|   1001 | kate   | manager   | 14000 |         10 |
|   1002 | jack   | scaleman  | 13000 |         20 |
|   1003 | maddie | business  | 14000 |         30 |
|   1004 | madd   | business  | 14000 |       NULL|
|   1005 | winni  | marketing | 14500 |       NULL|
+--------+--------+-----------+-------+------------+
5 rowsinset(0.00 sec)
 
mysql> rename tableemployee toemployee1;
Query OK, 0 rowsaffected (0.24 sec)
 
mysql> select* fromemployee1;
+--------+--------+-----------+-------+------------+
| emp_id | ename  |   job     |salary | commission |
+--------+--------+-----------+-------+------------+
|   1001 | kate   | manager   | 14000 |         10 |
|   1002 | jack   | scaleman  | 13000 |         20 |
|   1003 | maddie | business  | 14000 |         30 |
|   1004 | madd   | business  | 14000 |       NULL|
|   1005 | winni  | marketing | 14500 |       NULL|
+--------+--------+-----------+-------+------------+
5 rowsinset(0.00 sec)

TRUNCATE:
Syntax:

Truncate table table_name;


Example:
mysql> select* fromemployee;
+--------+--------+-----------+-------+------------+
| emp_id | ename  |   job     |salary | commission |
+--------+--------+-----------+-------+------------+
|   1001 | kate   | manager   | 14000 |         10 |
|   1002 | jack   | scaleman  | 13000 |         20 |
|   1003 | maddie | business  | 14000 |         30 |
|   1004 | madd   | business  | 14000 |       NULL|
|   1005 | winni  | marketing | 14500 |       NULL|
+--------+--------+-----------+-------+------------+
5 rowsinset(0.00 sec)
 mysql> truncatetableemployee;
Query OK, 0 rowsaffected (0.37 sec)

Result:
The above query was executed successfully.
4

Ex.No:1.2
DATA MANIPULATION LANGUAGE (DML)
Date: (INSERT,UPDATE,DELETE,SELECT)

Aim:

To execute the various Data Manipulation Language commands in RDBMS.


Algorithm:

STEP 1: Start the DBMS.


STEP 2: Create the table with its essential attributes.
STEP 3: Insert the record into table
STEP 4: Update the existing records into the table
STEP 5: Delete the records in to the table
STEP 6: use save point if any changes occur in any portion of the record to undo its original
state.
STEP 7: use rollback for completely undo the records
STEP 8: use commit for permanently save the records

QUERY EXECUTION:

Following are the DML statements:

1. Insert
2. Update
3. Delete
4. select

1. INSERT
Syntax:

Insert into <table_name>values(<value1>,<value2>,<value3>…….);


Example:
mysql> createtableemployee(emp_idint,enamevarchar(255),salary int);
Query OK, 0 rowsaffected (0.32 sec)
 
mysql> insertintoemployee values(1001,'jack',12000);
ERROR 1146 (42S02): Table'employee.employee'doesnt exist
mysql> createtableemployee(emp_idint,enamevarchar(255),salint);
Query OK, 0 rowsaffected (0.63 sec)
 
mysql> insertintoemployee values(1001,'jack',12000);
Query OK, 1 row affected (0.10 sec)
 
mysql> insertintoemployee values(1002,'mack',13000);
Query OK, 1 row affected (0.39 sec)
 
mysql> select* fromemployee;
+--------+-------+-------+
| emp_id | ename |salary |
5

+--------+-------+-------+
|   1001 | jack  | 12000 |
|   1002 | mack  | 13000 |
+--------+-------+-------+
2 rowsinset(0.00 sec)
 
mysql> insertintoemployee values(1003,'kate',15000);
Query OK, 1 row affected (0.39 sec)
 
mysql> select* fromemployee;
+--------+-------+-------+
| emp_id | ename |salary |
+--------+-------+-------+
|   1001 | jack  | 12000 |
|   1002 | mack  | 13000 |
|   1003 | kate  | 15000 |
+--------+-------+-------+
3 rowsinset(0.00 sec)

2. UPDATE
Syntax:

Update<table_name> set <column_name>=value where <condition>;


Example:
mysql> select* fromemployee;
+--------+-------+-------+
| emp_id | ename |salary |
+--------+-------+-------+
|   1001 | maddi | 12000 |
|   1002 | jack  | 13000 |
+--------+-------+-------+
2 rowsinset(0.00 sec)
 
mysql> updateemployee setsal=sal+100 whereename='jack';
Query OK, 1 row affected (0.03 sec)
Rowsmatched: 1  Changed: 1  Warnings: 0
 
mysql> select* fromemployee;
+--------+-------+-------+
| emp_id | ename |salary |
+--------+-------+-------+
|   1001 | maddi | 12000 |
|   1002 | jack  | 13100 |
+--------+-------+-------+
2 rowsinset(0.00 sec)
 
mysql> updateemployee setename='mike'whereemp_id=1001;
Query OK, 1 row affected (0.03 sec)
Rowsmatched: 1  Changed: 1  Warnings: 0
 
mysql> select* fromemployee;
+--------+-------+-------+
| emp_id | ename |salary |
+--------+-------+-------+
|   1001 | mike  | 12000 |
|   1002 | jack  | 13100 |
+--------+-------+-------+
2 rowsinset(0.00 sec)
6

3. DELETE
Syntax:

Delete from <table_name> where <condition>;


Example:
mysql> select* fromemployee;
+--------+-------+-------+
| emp_id | ename |salary |
+--------+-------+-------+
|   1001 | maddi | 12000 |
|   1002 | jack  | 13100 |
+--------+-------+-------+
2 rowsinset(0.00 sec)
 
mysql> deletefromemployee whereename='maddi';
Query OK, 1 row affected (0.16 sec)
 
mysql> select* fromemployee;
+--------+-------+-------+
| emp_id | ename |salary |
+--------+-------+-------+
|   1002 | jack  | 13100 |
+--------+-------+-------+
1 row inset(0.00 sec)
 
mysql> deletefromemployee whereemp_id=1001;
Query OK, 1 row affected (0.16 sec)
 
mysql> select* fromemployee;
+--------+-------+-------+
| emp_id | ename |salary |
+--------+-------+-------+
1 row inset(0.00 sec)

4. SELECT
Syntax:
Select * from table_name;

Example:
mysql> select* fromemployee;
+--------+-------+-------+
| emp_id | ename |salary |
+--------+-------+-------+
|   1001 | jack  | 12000 |
|   1002 | mack  | 13000 |
|   1003 | kate  | 15000 |
+--------+-------+-------+
3 rowsinset(0.00 sec)

Result:
The above queries are executed successfully
7

Ex.No:1.3
DATA CONTROL LANGUAGE (DCL)
Date: (GRANT, REVOKE)

Aim:

To execute the various Data Control Language commands in RDBMS.

Algorithm:

STEP 1: Start the DBMS.


STEP 2: Create the table with its essential attributes.
STEP 3: to grant the table
STEP 4: revoke the table

QUERY EXECUTION:

MySQL Data Control Language is similar to SQL Data Control Language and these are classified into
two types:

 Grant
 Revoke

1. Grant
Syntax:

GRANT { ALL | statement [ ,…n ] } TO security_account [ ,…n ];

Example:
mysql>grantselectonsample.* toreader@localhost identified by'secret';
Query OK,0 rowsaffected
mysql>exit;
Bye
D:\MySQL\bin>mysql -u reader -p
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| employee           |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
7 rowsinset(0.00 sec)
mysql> select* fromemployee;
+--------+----------+-------+
| emp_id | emp_name |salary |
+--------+----------+-------+
|   1001 | mike     | 12000 |
|   1002 | maze     | 13000 |
|   1003 | jack     | 14000 |
+--------+----------+-------+
8

3 rowsinset(0.04 sec)

2. Revoke
Syntax:

REVOKE
<priv_type> [<column_list>]
[ priv_type [<column_list>]] …
ON [object_type] priv_level
FROM user [user] …
REVOKE ALL PRIVILEGES, GRANT OPTION
FROM user [ user] …;

Example:
mysql> SHOW GRANTS FOR'david'@'localhost';
+----------------------------------------------------------------------+
| Grants fordavid@localhost                                           |
+----------------------------------------------------------------------+
| GRANTALLPRIVILEGESON*.* TO'david'@'localhost'WITHGRANTOPTION|
+----------------------------------------------------------------------+
1 row inset(0.00 sec)mysql> REVOKEALLPRIVILEGES,
GRANTOPTIONFROM'david'@'localhost';
Query OK, 0 rowsaffected (0.00 sec)

mysql> show revokefor'david'@'localhost';


ERROR 1064 (42000): there isnolocalhost user

Result:
The above queries are executed successfully
9

Ex.No:1.4
TRANSACTION CONTROL LANGUAGE (TCL)
Date: (COMMIT, SAVEPOINT, ROLLBACK)

Aim:

To execute the various Transaction Control Language (TCL) commands in RDBMS.


Algorithm:

STEP 1: Start the DBMS.


STEP 2: Create the table with its essential attributes.
STEP 3: To permanently store the values by usin commit command
STEP 4: To using savepoint and rollback the previous action

QUERY EXECUTION:
 Commit
 Roll-back
 Save points

1. COMMIT
Syntax:
mysql>commit;
Example:
mysql> select* fromemployee;
+--------+----------+-------+
| emp_id | emp_name | salary|
+--------+----------+-------+
|   1001 | mike     | 12000 |
|   1002 | maze     | 13000 |
|   1003 | jack     | 14000 |
+--------+----------+-------+
3 rowsinset(0.00 sec)
 
mysql> commit;

2. ROLL-BACK
Syntax:
mysql>Rollback;
Example:
mysql> select* fromemployee;
+--------+----------+-------+
| emp_id | emp_name | salary|
+--------+----------+-------+
|   1001 | mad      | 15500 |
|   1002 | maddi    | 16000 |
|   1003 | maddie   | 16000 |
+--------+----------+-------+
3 rowsinset(0.00 sec)
 
mysql> updateemployee setsalary=salary+1000 whereemp_id=1001;
Query OK, 1 row affected (0.00 sec)
Rowsmatched: 1  Changed: 1  Warnings: 0
 
mysql> select* fromemployee;
+--------+----------+-------+
| emp_id | emp_name |salary |
10

+--------+----------+-------+
|   1001 | mad      | 16500 |
|   1002 | maddi    | 16000 |
|   1003 | maddie   | 16000 |
+--------+----------+-------+
3 rowsinset(0.00 sec)
 
mysql> rollback;
Query OK, 0 rowsaffected (0.39 sec)
 
mysql> select* fromemployee;
+--------+----------+-------+
| emp_id | emp_name | salary|
+--------+----------+-------+
|   1001 | mad      | 15500 |
|   1002 | maddi    | 16000 |
|   1003 | maddie   | 16000 |
+--------+----------+-------+
3 rowsinset(0.00 sec)

3. SAVE POINTS
Syntax:
mysql>savepointsavepointname;
Example:
mysql> CREATETABLEtest(test_id INTNOTNULLPRIMARYKEY) ENGINE=InnoDB;
Query OK, 0 rowsaffected (0.39 sec)
 
mysql> START TRANSACTION;
Query OK, 0 rowsaffected (0.00 sec)
 
mysql> INSERTINTOTEST VALUES(1);
Query OK, 1 row affected (0.00 sec)
 
mysql> SELECT* FROMTEST;
+---------+
| test_id |
+---------+
|       1 |
+---------+
1 row inset(0.00 sec)
 
mysql> SAVEPOINT TRAN2;
Query OK, 0 rowsaffected (0.00 sec)
 
mysql> INSERTINTOTEST VALUES(2);
Query OK, 1 row affected (0.00 sec)
 
mysql> SELECT* FROMTEST;
+---------+
| test_id |
+---------+
|       1 |
|       2 |
+---------+
2 rowsinset(0.00 sec)
 
mysql> ROLLBACKTOTRAN2;
Query OK, 0 rowsaffected (0.00 sec)
 
mysql> SELECT* FROMTEST;
+---------+
| test_id |
+---------+
11

|       1 |
+---------+
1 row inset(0.00 sec)
 
mysql> ROLLBACK;
Query OK, 0 rowsaffected (0.09 sec)
 
mysql> SELECT* FROMTEST;
Empty set(0.00 sec)

Result:
The above queries are executed successfully
12

Ex.No:2.1
DATABASE QUERYING –SIMPLE QUERIES
Date:
Aim:

To execute and verify the MySQL commands for Simple Queries.


Algorithm:

STEP 1: Start the DBMS.


STEP 2: Create the table with its essential attributes.
STEP 3: To execute the basic queries

QUERY EXECUTION:

mysql> database database_name

mysql>CREATE TABLE shop (

article INT(4) UNSIGNED ZEROFILL DEFAULT '0000' NOT NULL,

dealer CHAR(20) DEFAULT '' NOT NULL,

price DOUBLE(16,2) DEFAULT '0.00' NOT NULL,

PRIMARY KEY(article, dealer));

mysql>INSERT INTO shop VALUES

(1,'A',3.45),(1,'B',3.99),(2,'A',10.99),(3,'B',1.45),

(3,'C',1.69),(3,'D',1.25),(4,'D',19.95);
After issuing the statements, the table should have the following contents:

mysql>SELECT * FROM shop ORDER BY article;

+---------+--------+-------+

| article | dealer | price |

+---------+--------+-------+

| 0001 | A | 3.45 |

| 0001 | B | 3.99 |

| 0002 | A | 10.99 |

| 0003 | B | 1.45 |

| 0003 | C | 1.69 |

| 0003 | D | 1.25 |

| 0004 | D | 19.95 |

+---------+--------+-------+

Result:
The above queries are executed successfully
13

Ex.No:2.2
DATABASE QUERYING - NESTED QUERIES
Date:

Aim:

To execute and verify the MySQL commands for Nested Queries.

Algorithm:

STEP 1: Start the program.


STEP 2: Create two different tables with its essential attributes.
STEP 3: Insert attribute values into the table.
STEP 4: Create the Nested query from the above created table.
STEP 5: Execute Command and extract information from the tables.
STEP 6: Stop the program.
QUERY EXECUTION:
mysql>SELECT artist_name FROM

->artist INNER JOIN album USING (artist_id)

-> WHERE album_name = "In A Silent Way";

+-------------+

| artist_name |

+-------------+

| Miles Davis |

+-------------+

1 row in set (0.14 sec)

But there’s another way, using a nested query:


mysql>SELECT artist_name FROM artist WHERE artist_id =

-> (SELECT artist_id FROM album WHERE album_name = "In A Silent Way");

+-------------+

| artist_name |

+-------------+

| Miles Davis |

+-------------+

1 row in set (0.28 sec)

Result:
The above queries are executed successfully
14

Ex.No:2.3
DATABASE QUERYING –SUB QUERIES
Date:

Aim:

To execute and verify the MySQL commands for Sub Queries.


Algorithm:

STEP 1: Start the DBMS.


STEP 2: Create the table with its essential attributes.
STEP 3: To execute the sub queries

QUERY EXECUTION:

Syntax:
Select <column_name1>,<column_name2> from <table_name> where <column_name2><condition>
(select <column_name2> from <table_name> where condition);
table name => Any accurate table in the database.
column name =>The column names that are inserted in the table.
condition => Is a logic.

Example:
mysql> select * from employee;
+--------+-------+-------+--------+-----------+
| emp_id | ename |salary | deptno | job       |
+--------+-------+-------+--------+-----------+
|   1001 | mike  | 12000 |     10 | manager   |
|   1002 | rambo | 13000 |     20 | scalesman |
|   1003 | kate  | 14000 |     10 | manager   |
|   1003 | jeo   | 14000 |     20 | manager   |
|   1003 | finn  | 14000 |     30 | manager   |
+--------+-------+-------+--------+-----------+
5 rows in set (0.00 sec)
mysql> select emp_id,ename,sal from employee where salary >(select salary from
employee where ename='rambo');
+--------+-------+-------+
| emp_id | ename |salary |
+--------+-------+-------+
|   1003 | kate  | 14000 |
|   1003 | jeo   | 14000 |
|   1003 | finn  | 14000 |
+--------+-------+-------+
3 rows in set (0.08 sec)
 
mysql> select min(salary) from employee where salary=(select salary from employee
where ename='rambo');
+--------+-------+-------+--------+-----------+
| emp_id | ename |salary | deptno | job       |
+--------+-------+-------+--------+-----------+
|   1002 | rambo | 13000 |     20 | salesman  |
+--------+-------+-------+--------+-----------+
1 rows in set (0.08 sec)
Result:
The above queries are executed successfully
15

Ex.No:2.4
DATABASE QUERYING –JOIN
Date:

Aim:

To execute and verify the MySQL commands for Join Queries.

Algorithm:
STEP 1: Start the program.
STEP 2: Create two different tables with its essential attributes.
STEP 3: Insert attribute values into the table.
STEP 4: Create the table object for easy reference.
STEP 5: Join two tables by using JOIN operator.
STEP 6: Display the result of the result table.
STEP 7: Stop the program.

QUERY EXECUTION:

Join can be divided into

 Cartesian join
 Equi join
 Non equi join
 Outer join

1. CARTESIAN JOIN

Syntax:

Select * from <table_name1>,<table_name2>;

Example:

mysql> select* fromemployee;


+--------+--------+--------+
| emp_id | ename  |deptno |
+--------+--------+--------+
|   1001 | jhon   |     10 |
|   1002 | shah   |     20 |
|   1003 | maddi  |     10 |
|   1004 | maddie |     20 |
+--------+--------+--------+
4 rowsinset(0.00 sec)
mysql> select* fromdepartment;
+--------+-----------+-----------+
| deptno | dept_name | dept_city |
+--------+-----------+-----------+
|     10 | leo       | texas     |
|     20 | vin       | newyork   |
+--------+-----------+-----------+
16

2 rowsinset(0.00 sec)
mysql> select* fromemployee,department;
+--------+--------+--------+--------+-----------+-----------+
| emp_id | ename  |deptno | deptno | dept_name | dept_city |
+--------+--------+--------+--------+-----------+-----------+
|   1001 | jhon   |     10 |     10 | leo       | texas     |
|   1001 | jhon   |     10 |     20 | vin       | newyork   |
|   1002 | shah   |     20 |     10 | leo       | texas     |
|   1002 | shah   |     20 |     20 | vin       | newyork   |
|   1003 | maddi  |     10 |     10 | leo       | texas     |
|   1003 | maddi  |     10 |     20 | vin       | newyork   |
|   1004 | maddie |     20 |     10 | leo       | texas     |
+--------+--------+--------+--------+-----------+-----------+
7 rowsinset(0.00 sec)

2. EQUI JOIN

Syntax:

select<column_list> from <table1>,<table2>where table1.column_name = table2.column_name;

Example:

mysql> select* fromemployee;


+--------+-------+--------+--------+
| emp_id | ename | salary | deptno |
+--------+-------+--------+--------+
| 1001   | mike  | 12000  |   10   |
| 1002   | welli | 13000  |   20   |
| 1003   | jeo   | 14000  |   10   |
| 1004   | jorg  | 15000  |   20   |
+--------+-------+--------+--------+
4 rowsinset(0.00 sec)
 
mysql>select* fromdepartment;
+---------+-----------+---------+
| dept_no | dept_name | city |
+---------+-----------+---------+
| 10      | jelly     | texas   |
| 20      | oyak      | newyork |
+---------+-----------+---------+
2 rowsinset(0.00 sec)
 
mysql>selectemployee.emp_id, employee.ename, department.dept_no, department.city
fromemployee, department whereemployee.deptno=department.dept_no;
+--------+-------+---------+---------+
| emp_id | ename | dept_no | city    |
+--------+-------+---------+---------+
| 1001   | mike  | 10      | texas   |
| 1002   | welli | 20      | newyork |
| 1003   | jeo   | 10      | texas   |
| 1004   | jorge | 20      | newyork |
+--------+-------+---------+---------+

4 rowsinset(0.05 sec)

3. NON EQUI JOIN

Syntax:

SELECT * FROM <table_name1>, <table_name2> WHERE <table_name1>.column [> | = | <= ]


<table_name2>.column;
17

Example:

mysql> select* fromemployee;


+--------+-------+--------+--------+
| emp_id | ename | salary | deptno |
+--------+-------+--------+--------+
|   1001 | mike  |  12000 |     10 |
|   1002 | welli |  13000 |     20 |
|   1003 | jeo   |  14000 |     10 |
|   1004 | jorge |  15000 |     20 |
+--------+-------+--------+--------+
4 rowsinset(0.00 sec)
mysql> select* fromdepartment;
+---------+-----------+---------+
| dept_no | dept_name | city    |
+---------+-----------+---------+
|      10 | jelly     | texas   |
|      20 | oyak      | newyork |
+---------+-----------+---------+
2 rowsinset(0.00 sec)
mysql> selectemployee.emp_id,employee.ename,department.dept_name,department.city
fromemployee,department whereemployee.deptno=department.dept_no
andemployee.salary>13000;
+--------+-------+-----------+---------+
| emp_id | ename | dept_name | city    |
+--------+-------+-----------+---------+
|   1003 | jeo   | jelly     | texas   |
|   1004 | jorge | oyak      | newyork |
+--------+-------+-----------+---------+
2 rowsinset(0.00 sec)

4. OUTER JOIN

Outer join can be classified into two types:

i. Left outer join

Syntax:

SELECT *FROM <table1> left [ OUTER ] JOIN table2ON table1.column_name=table2.column_name;

Example:

mysql> select* fromemployee;


+--------+--------+---------+
| emp_id | ename  |dept_no |
+--------+--------+---------+
|   1001 | jack   |      10 |
|   1002 | maddi  |      20 |
|   1003 | maddie |      10 |
|   1004 | max    |      20 |
|   1004 | capi   |      30 |
+--------+--------+---------+
5 rowsinset(0.00 sec)
 
mysql> select* fromdepartment;
+---------+-----------+----------+
| dept_no | dept_name | city     |
+---------+-----------+----------+
|      10 | finance   | texas    |
|      20 | capital   | new york |
+---------+-----------+----------+
18

2 rowsinset(0.00 sec)
 
mysql>selectemp_id,ename,dept_name,city fromemployee leftjoindepartment
onemployee
    .dept_no=department.dept_no;
+--------+--------+-----------+----------+
| emp_id | ename  |dept_name | city     |
+--------+--------+-----------+----------+
|   1001 | jack   | finance   | texas    |
|   1003 | maddie | finance   | texas    |
|   1002 | maddi  | capital   | new york |
|   1004 | max    | capital   | new york |
|   1004 | capi   | NULL      | NULL     |
+--------+--------+-----------+----------+
5 rowsinset(0.00 sec)
6
ii. right outer join

Syntax:

SELECT * FROM table1 Right [ OUTER ] JOIN table2 ON table1.column_name=table2.column_name;

Example:

mysql> select* fromemployee;


+--------+--------+---------+
| emp_id | ename  |dept_no |
+--------+--------+---------+
|   1001 | jack   |      10 |
|   1002 | maddi  |      20 |
|   1003 | maddie |      10 |
|   1004 | max    |      20 |
|   1004 | capi   |      30 |
+--------+--------+---------+
5 rowsinset(0.00 sec)
 
mysql> select* fromdepartment;
+---------+--------------+----------+
| dept_no | dept_name    | city     |
+---------+--------------+----------+
|      10 | finance      | texas    |
|      20 | capital      | new york |
|      30 | manager      | ausralia |
|      40 | applications | usa      |
|      50 | technology   | canada   |
+---------+--------------+----------+
5 rowsinset(0.00 sec)
mysql> selectemployee.emp_id,employee.ename,department.dept_name,department.city
fromdepartment rightjoinemployee ondepartmant.dept_no=employee.dept_no;
+--------+--------+-----------+----------+
| emp_id | ename  |dept_name | city     |
+--------+--------+-----------+----------+
| 1001   | jack   | finance   | texas    |
| 1003   | maddie | finance   | texas    |
| 1002   | maddi  | capital   | new york |
| 1004   | max    | capital   | new york |
| 1004   | capi   | manager   | ausralia |
+--------+--------+-----------+----------+
5 rowsinset(0.00 sec)
Result:
The above queries are executed successfully
19

Ex.No:3.1
VIEWS
Date:

Aim:

To create the view, execute and verify the various operations on views.
Algorithm:

STEP 1: Start the DBMS.


STEP 2: Connect to the existing database(DB)
STEP 3: Create the table with its essential attributes.
STEP 4: Insert record values into the table.
STEP 5: Create the view from the above created table.
STEP 6: Display the data presented on the VIEW.
STEP 7: Insert the records into the VIEW,
STEP 8: Check the database object table and view the inserted values presented
STEP 9: Execute different Commands and extract information from the View.
STEP 10: Stop the DBMS.

QUERY EXECUTION
MySQL Views object is mainly classified into

 Create view
 Alter view
 Drop view

1. CREATE VIEW

Syntax:

create view <table_name>as select <column_name> FROM <table_name> where <condition>;

Example:

mysql> select* fromcars;


+--------+----------+--------+
| car_id | car_name | cost   |
+--------+----------+--------+
|      1 | audi     |  52642 |
|      2 | skoda    | 526400 |
|      3 | volva    |  52640 |
|      4 | volva    |  52000 |
|      5 | hummer   |  41400 |
+--------+----------+--------+
5 rowsinset(0.00 sec)
 
mysql> createviewcheapcars asselectnamefromcars wherecost&lt;52640;
20

ERROR 1054 (42S22): Unknown column'name'in'field list'mysql&gt;


CREATEVIEWCheapcars ASSELECTcar_name fromcars wherecost&lt;52640;
Query OK, 0 rowsaffected (0.13 sec)
mysql&gt; select* fromcheapcars;
+----------+
| car_name |
+----------+
| volva    |
| hummer   |
+----------+
2 rowsinset(0.16 sec)
 
mysql> createviewvehicles asselectnamefromcars;
+--------+----------+--------+
| car_id | car_name | cost   |
+--------+----------+--------+
|      1 | audi     |  52642 |
|      2 | skoda    | 526400 |
|      3 | volva    |  52640 |
|      4 | volva    |  52000 |
|      5 | hummer   |  41400 |
+--------+----------+--------+
5 rowsinset(0.00 sec)

2. ALTER VIEW

Syntax:

Alter view <table_name> as select <column_name> from <table_name> where condition;

Example:

mysql> select* fromcars;


+--------+----------+--------+
| car_id | car_name | cost   |
+--------+----------+--------+
|      1 | audi     |  52642 |
|      2 | skoda    | 526400 |
|      3 | volva    |  52640 |
|      4 | volva    |  52000 |
|      5 | hummer   |  41400 |
+--------+----------+--------+
5 rowsinset(0.00 sec)
 
mysql> alterviewcheapcars asselectcar_name fromcars wherecost select*
fromcheapcars;
+----------+
| car_name |
+----------+
| volva    |
| hummer   |
+----------+
2 rowsinset(0.00 sec)
 
mysql> alterviewcheapcars asrangeovers;
mysql> select* fromrangeovers;
+--------+----------+--------+
| car_id | car_name | cost   |
+--------+----------+--------+
|      1 | audi     |  52642 |
|      2 | skoda    | 526400 |
|      3 | volva    |  52640 |
|      4 | volva    |  52000 |
21

|      5 | hummer   |  41400 |


+--------+----------+--------+
5 rowsinset(0.00 sec)

3. DROP VIEW

Syntax:

Drop view <view_name>

Example:

mysql> dropviewcars;
Query OK, 0 rowsaffected (0.34 sec)
 
mysql> select* fromcheapcars;
ERROR 1356 (HY000): View'employee.cheapcars'referencesinvalid table(s)
orcolumn(s) orfunction(s) ordefiner/invoker ofviewlack rights touse them

RESULT:

Thus the MySQL commands for View has been verified and executed successfully.
22

Ex.No:3.2
SEQUENCES
Date:
Aim:

To create the Sequence and verify the various operations on Sequence to get the incremented
number.
Algorithm:

Step 1: Start the DMBS.


Step 2: Connect to the existing database (DB)
Step 3: Create the sequence with its essential optional parameter.
Step 4: Display the data presented on the sequence by using pseudo column.
Step 5: Alter the sequence with different optional parameter.
Step 6: Drop the sequence
Step 7: Stop the DBMS.

QUERY EXECUTION

 Sequence – Is a user defined datatype which is used to increment the values accordingly.

Syntax:
create table <table_name> (<column_name1>data type(size) constraint type, <column_name2>data
type(size),constraint(column_name1);
table_name =>Any accurate table.
column_name =>The operation that can be performed in the column of a table.
constraint_type =>constraint type will specify the type of constraint, that going to be executed in the
database.

Example:
mysql> createtableproduct(prod_idintnotnullauto_increment,prod_namevarchar
(255)notnull,primarykey(prod_id));
Query OK, 0 rowsaffected (0.52 sec)
 
mysql> insertintoproduct (prod_name) values('mobile');
Query OK, 1 row affected (0.05 sec)
 
mysql> insertintoproduct (prod_name) values('laptop');
Query OK, 1 row affected (0.09 sec)
 
mysql> insertintoproduct (prod_name) values('system');
Query OK, 1 row affected (0.09 sec)
 
mysql> select* fromproduct;
+---------+-----------+
| prod_id | prod_name |
+---------+-----------+
|       1 | mobile    |
|       2 | laptop    |
|       3 | system    |
+---------+-----------+
3 rowsinset(0.00 sec)

Result:
The above queries are executed successfully
23

Ex.No:3.3
SYNONYMS
Date:

Aim:
To create the Synonyms and verify the various operations on Synonyms
Algorithm:
STEP 1: Start the DMBS.
STEP 2: Connect to the existing database(DB)
STEP 3: Create the table with its essential attributes.
STEP 4: Insert record values into the table.
STEP 5: Create the synonyms from the above created table or any data object.
STEP 6: Display the data presented on the synonyms.
STEP 7: Insert the records into the synonyms,
STEP 8: Check the database object table and view the inserted values presented
STEP 9: Stop the DBMS.

QUERY EXECUTION:
mysql>SHOWDATABASES;
+--------------------+
| Database |
+--------------------+
|information_schema|
|mysql|
|performance_schema|
|sys|
|world|
+--------------------+

mysql>CALLsys.create_synonym_db('INFORMATION_SCHEMA','info');
+---------------------------------------+
|summary|
+---------------------------------------+
| Created 63 views in the info database |
+---------------------------------------+

mysql>SHOWDATABASES;
+--------------------+
| Database |
+--------------------+
|information_schema|
|info|
|mysql|
|performance_schema|
|sys|
|world|
+--------------------+

mysql>SHOWFULLTABLESFROM info;
+---------------------------------------+------------+
|Tables_in_info|Table_type|
+---------------------------------------+------------+
|character_sets| VIEW |
|collation_character_set_applicability| VIEW |
|collations| VIEW |
|column_privileges| VIEW |
|columns| VIEW |
...

Result:
The above queries are executed successfully
24

Ex.No:4
DATABASE PROGRAMMING
Date: IMPLICIT AND EXPLICIT CURSORS

Aim:

To create cursor for fetching records from the given table based on the requirement

A database cursor can be thought of as a pointer to a specific row within a query result.  The pointer can be
moved from one row to the next.  Depending on the type of cursor, you may be even able to move it to the
previous row.

Algorithm:

1. Implicit Cursor

 Implicit cursors are automatically generated by the Oracle engine. If the Oracle Engine opens a
cursor for its internal processing, it is known as implicit cursor.
 Implicit cursors are created by default to process the statements when DML statements (INSERT,
UPDATE, and DELETE) are executed.
2. Explicit Cursor

 If a cursor is opened for processing data through a MySQL block as per requirement like user
defined cursor, is known as an explicit cursor.
 Explicit cursor is created while executing a SELECT statement that returns more than one row.
 These cursor should be defined in the declaration section of the MySQL block and created on a
SELECT statement which returns more than one row.

QUERY EXECUTION:

mysql> CREATE TABLE Employee(


->idint,
->first_name VARCHAR(15),
->last_name VARCHAR(15),
->start_date DATE,
->end_date DATE,
->salary FLOAT(8,2),
->city VARCHAR(10),
->description VARCHAR(15)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> insert into Employee(id,first_name, last_name, start_date, end_Date, salary, City, Description)
->values (1,'Jason', 'Martin', '19960725', '20060725', 1234.56, 'Toronto', 'Programmer');
Query OK, 1 row affected (0.00 sec)

mysql> select * from Employee;


+------+------------+-----------+------------+------------+---------+-----------+-------------+
| id | first_name | last_name | start_date | end_date | salary | city | description |
+------+------------+-----------+------------+------------+---------+-----------+-------------+
| 1 | Jason | Martin | 1996-07-25 | 2006-07-25 | 1234.56 | Toronto | Programmer |
| 2 | Alison | Mathews | 1976-03-21 | 1986-02-21 | 6661.78 | Vancouver | Tester |
| 3 | James | Smith | 1978-12-12 | 1990-03-15 | 6544.78 | Vancouver | Tester |
| 4 | Celia | Rice | 1982-10-24 | 1999-04-21 | 2344.78 | Vancouver | Manager |
| 5 | Robert | Black | 1984-01-15 | 1998-08-08 | 2334.78 | Vancouver | Tester |
| 6 | Linda | Green | 1987-07-30 | 1996-01-04 | 4322.78 | New York | Tester |
| 7 | David | Larry | 1990-12-31 | 1998-02-12 | 7897.78 | New York | Manager |
| 8 | James | Cat | 1996-09-17 | 2002-04-15 | 1232.78 | Vancouver | Tester |
+------+------------+-----------+------------+------------+---------+-----------+-------------+
8 rows in set (0.00 sec)
25

mysql> delimiter $$
mysql>
mysql> CREATE PROCEDURE myProc
-> (p_first_namevarchar(30),
->p_last_name VARCHAR(30),
->p_city VARCHAR(30),
->p_descriptionvarchar(30),
->outp_sqlcodeint,
->outp_status_messagevarchar(100))
-> MODIFIES SQL DATA
-> BEGIN
->
-> DECLARE l_manager_id INT;
-> DECLARE csr_mgr_id cursor for
-> SELECT id
-> FROM employee
-> WHERE first_name=p_first_name AND last_name=p_last_name;
->
-> OPEN csr_mgr_id;
-> FETCH csr_mgr_id INTO l_manager_id;
->
-> INSERT INTO employee (first_name,id,city)
->VALUES(p_first_name,l_manager_id,p_city);
->
-> CLOSE csr_mgr_id;
-> END$$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql>
mysql> set @myCode = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> set @myMessage = 0;


Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> call myProc('Jason','Martin','NewCity','New Description',@myCode,@myMessage);
Query OK, 1 row affected (0.01 sec)

mysql> drop procedure myProc;


Query OK, 0 rows affected (0.00 sec)

Result:
The above queries are executed successfully
26

Ex.No:5.1
PROCEDURES
Date:

Aim:
To write the MySQL for the implementation of procedures and its application.
Algorithm:
STEP 1: Start the program.
STEP 2: Create the table with its essential attributes.
STEP 3: Insert attribute values into the table.
STEP 4: Create the procedure with necessary arguments and return data types.
STEP 5: create the MySQL block to call / use the procedure.
STEP 6: Execute the MySQL program.
STEP 7: Give the input values
STEP 8: Extract/process the information from the procedure.
STEP 9: Stop the program.

QUERY EXECUTION:
1. Create Procedure:
Syntax:
Create Procedure <proc_name> ([proc_parameter[……]]) proc_bodyproc_name : Name of the
procedureproc_parameter : [ IN | OUT|INOUT] param_name type
proc_body : statement in the syntax

Example:
mysql> select* fromemployee;
+--------+----------+-----------+-------------------+--------+-------+
| emp_id | emp_name | city      | designation       | salary | perks |
+--------+----------+-----------+-------------------+--------+-------+
|      1 | david    | delhi     | manager           |  12000 |   855 |
|      2 | shaha    | mumbai    | assistant manager |  13000 |   853 |
|      3 | sha      | puna      | scales manager    |  11000 |   850 |
|      4 | jack     | bangalore | designer          |  14000 |   854 |
|      5 | james    | mangalore | web designer      |  15000 |  1124 |
|      6 | mike     | chennai   | develpoer         |  15500 |   840 |
+--------+----------+-----------+-------------------+--------+-------+
6 rowsinset(0.00 sec)
mysql> delimiter $$
mysql> createprocedureproc3(outp1 int)
    ->selectcount(*) intop1 fromemployee;
    -> $$
Query OK, 0 rowsaffected (0.00 sec)
 
mysql> delimiter ;
mysql> call proc3(@a);
Query OK, 1 row affected (0.00 sec)
 mysql> select@a;
+------+
| @a   |
+------+
|    6 |
+------+
1 row inset(0.00 sec)
2. Drop procedure:
Syntax:
Drop {Procedure} [If Exits] {proc_name};
Example:
mysql> Dropprocedureif exits proc3;
Query OK, 0 rowsaffected (0.30 sec)
Result:
The above queries are executed successfully
27

Ex.No:5.2
FUNCTIONS
Date:

Aim:
To write the MySQL for the implementation of functions and its application.
Algorithm:
STEP 1: Start the program.
STEP 2: Create the table with its essential attributes.
STEP 3: Insert attribute values into the table.
STEP 4: Create the function with necessary arguments and return data types.
STEP 5: create the MySQL block to call / use the function.
STEP 6: Execute the MySQL program.
STEP 7: Give the input values
STEP 8: Extract/process the information from the function.
STEP 9: Stop the program.
QUERY EXECUTION:

1. Create Function:
Syntax:
Create Function <func_name>([func_parameter]) returns type function_body
Func_name : Name of the Function
Func_parameter : param_name type
Type : Any datatype
Function_body :statement in mysql 

Example:
mysql> CREATEFUNCTIONfunc1(str CHAR(20))
    -> RETURNSCHAR(50)
    -> RETURNCONCAT('WELCOME TO, ',str,'!');
Query OK, 0 rowsaffected (0.00 sec)
mysql> SELECTfunc('ITOOLSINFO.COM');
+-----------------------------+
| func('ITOOLSINFO.COM')      |
+-----------------------------+
| WELCOME TO, ITOOLSINFO.COM! |
+-----------------------------+
1 row inset(0.00 sec)

2. Drop Function:
Syntax:
Drop {Function} [If Exits] {func_name};
Example:
mysql> DROPFUNCTIONIF EXISTS func1;
Query OK, 0 rowsaffected (0.30 sec)

Result:
The above queries are executed successfully
28

Ex.No: 6
TRIGGERS
Date:

Aim:
To write the MySQL for the implementation of triggering process.
Algorithm:
STEP 1: Start the program.
STEP 2: Create the table with its essential attributes.
STEP 3: Insert attribute values into the table.
STEP 4: Create the trigger with necessary arguments.
STEP 5: Execute the MySQL program.
STEP 6: Give the input values
STEP 7: Extract/process the information from the trigger.
STEP 8: Stop the program.

QUERY EXECUTION:
MySQL Triggers are of 4 types:
 Before Insert Trigger
 Before Update Trigger
 After Insert Trigger
 After Update Trigger
1. Before Insert Trigger
Syntax:
create trigger <trigger_name>
before insert
on <table_name> for each row
BEGIN
— variable declarations
— trigger code
END;

Example:
mysql> select* fromemployee;
+--------+----------+-----------+-------------------+--------+-------+
| emp_id | emp_name | city      | designation       | salary | perks |
+--------+----------+-----------+-------------------+--------+-------+
|      1 | david    | delhi     | manager           |  12000 |   855 |
|      2 | shaha    | mumbai    | assistant manager |  13000 |   853 |
|      3 | sha      | puna      | scales manager    |  11000 |   850 |
|      4 | jack     | bangalore | designer          |  14000 |   854 |
|      5 | james    | mangalore | web designer      |  15000 |  1124 |
+--------+----------+-----------+-------------------+--------+-------+
5 rowsinset(0.00 sec)
 
mysql delimiter $$
mysql CREATETRIGGERinst_trigger BEFORE INSERTONEmployee
     -> FOREACH ROW
     -> BEGIN
     -> UPDATEEmployee SETSalary=Salary-400 whereperks>500;
     ->end;
     -> $$
Query OK, 0 rowsaffected (0.42 sec)
 
mysql> delimiter ;
mysql> insertintoemployee values(6,'mike','chennai','develpoer',15500,840);
Query OK, 1 row affected (0.10 sec)
 
mysql> select* fromemployee;
29

+--------+----------+-----------+-------------------+--------+-------+
| emp_id | emp_name | city      | designation       | salary | perks |
+--------+----------+-----------+-------------------+--------+-------+
|      1 | david    | delhi     | manager           |  11600 |   855 |
|      2 | shaha    | mumbai    | assistant manager |  12600 |   853 |
|      3 | sha      | puna      | scales manager    |  10600 |   850 |
|      4 | jack     | bangalore | designer          |  13600 |   854 |
|      5 | james    | mangalore | web designer      |  14600 |  1124 |
|      6 | mike     | chennai   | developer         |  15100 |   840 |
+--------+----------+-----------+-------------------+--------+-------+
6 rowsinset(0.00 sec)
2. Before Update Trigger
Syntax:
create trigger <trigger_name>
before update
on <table_name> for each row
BEGIN
— variable declarations
— trigger code
END;

Example:
mysql> select* fromemployee;
+--------+----------+-----------+-------------------+--------+-------+
| emp_id | emp_name | city      | designation       | salary | perks |
+--------+----------+-----------+-------------------+--------+-------+
|      1 | david    | delhi     | manager           |  12000 |   855 |
|      2 | shaha    | mumbai    | assistant manager |  13000 |   853 |
|      3 | sha      | puna      | scales manager    |  11000 |   850 |
|      4 | jack     | bangalore | designer          |  14000 |   854 |
|      5 | james    | mangalore | web designer      |  15000 |  1124 |
|      6 | mike     | chennai   | develpoer         |  15500 |   840 |
+--------+----------+-----------+-------------------+--------+-------+
6 rowsinset(0.00 sec)
mysql> delimiter //
mysql> CREATETRIGGERupdtrigger BEFORE UPDATEONEmployee
    -> FOREACH ROW
    -> BEGIN
    -> IF NEW.Salary=500 THEN-> SETNEW.Salary=10000;
    -> ELSEIF NEW.Salary>500 THEN
    -> SETNEW.Salary=15000;
    -> ENDIF;
    -> END
    -> //
Query OK, 0 rowsaffected (0.01 sec)
mysql> delimiter ;
mysql> UPDATEEmployee
    -> SETSalary=500;
Query OK, 5 rowsaffected (0.04 sec)
Rowsmatched: 7  Changed: 5  Warnings: 0
mysql> select* fromemployee;
+--------+----------+-----------+-------------------+--------+-------+
| emp_id | emp_name | city      | designation       | salary | perks |
+--------+----------+-----------+-------------------+--------+-------+
|      1 | david    | delhi     | manager           |  10000 |   855 |
|      2 | shaha    | mumbai    | assistant manager |  10000 |   853 |
|      3 | sha      | puna      | scales manager    |  10000 |   850 |
|      4 | jack     | bangalore | designer          |  10000 |   854 |
|      5 | james    | mangalore | web designer      |  10000 |  1124 |
|      6 | mike     | chennai   | develpoer         |  10500 |   840 |
+--------+----------+-----------+-------------------+--------+-------+
7 rowsinset(0.00 sec)

3. After Insert Trigger
30

Syntax:
create trigger <trigger_name>
AFTER INSERT
ON <table_name> FOR EACH ROW
BEGIN
— variable declarations
— trigger code
END;

Example:
mysql createtablestudent_table(stu_idint,stu_namevarchar(255),stu_classint);
Query OK, 0 rowsaffected (0.38 sec)
 
mysql createtablestudent_log1(user_idvarchar(255),description varchar(255));
 
Query OK, 0 rowsaffected (0.48 sec)
mysql CREATETRIGGERstudent_insert AFTERinsertONstudent_table FOREACH ROW
BEGININSERTintostudent_log1(user_id,description) VALUES(user(),CONCAT('INSER
T student records',new.stu_id,' ',new.stu_name,' ',new.stu_class));
END$$
Query OK, 0 rowsaffected (0.15 sec)
 
mysql delimiter ;
mysql insertintostudent_table values(1,'mike',10);
Query OK, 1 row affected (0.10 sec)
 
mysql insertintostudent_table values(2,'mad',20);
Query OK, 1 row affected (0.11 sec)
 
mysql insertintostudent_table values(3,'mack',30);
Query OK, 1 row affected (0.10 sec)
 
mysql select* fromstudent_log1;
+----------------+---------------------------------+
| user_id        | description                     |
+----------------+---------------------------------+
| root@localhost | INSERTstudent records1 mike 10 |
| root@localhost | INSERTstudent records2 mad 20  |
| root@localhost | INSERTstudent records3 mack 30 |
+----------------+---------------------------------+
3 rowsinset(0.00 sec)
4. After Update Trigger
Syntax:
create trigger <trigger_name>
AFTER UPDATE
ON <table_name> FOR EACH ROW
BEGIN
— variable declarations
— trigger code
END;

Example:
CreateTableStudent_Table02(Stu_Idint, Stu_Namevarchar(255),Stu_Classint);
 
createtablestu_log02( user_id VARCHAR(255), description VARCHAR(255));
delimiter $$
mysql>CREATETRIGGERstu_update
->AFTERUPDATEONstu_table FOREACH ROW
->BEGIN
    ->INSERTintostu_log(user_id, description)
       -> VALUES(user(), CONCAT('Update Student Record
31

        ->(',old.stu_id,' ',old.stu_name,' ',old.stu_class,


    ') to (',new.stu_id,' ',new.stu_name,' ',new.stu_class,')'));
->END$$
mysql>delimiter ;
mysql> select* fromstudent_table02;
+--------+----------+-----------+
| stu_id | stu_name | stu_class |
+--------+----------+-----------+
|      1 | david    |         9 |
|      2 | shah     |         9 |
|      3 | mike     |         9 |
|      4 | james    |         9 |
+--------+----------+-----------+
4 rowsinset(0.00 sec)
 
mysql> updatestudent_table02 setstu_class=stu_class+1;
Query OK, 4 rowsaffected (0.20 sec)
Rowsmatched: 4  Changed: 4  Warnings: 0
 
mysql> select* fromstudent_table02;
+--------+----------+-----------+
| stu_id | stu_name | stu_class |
+--------+----------+-----------+
|      1 | david    |        10 |
|      2 | shah     |        10 |
|      3 | mike     |        10 |
|      4 | james    |        10 |
+--------+----------+-----------+
4 rowsinset(0.00 sec)
mysql>select* fromstu_log02;
+----------------+--------------------------------------------------------+
| user_id        | description                                            |
+----------------+--------------------------------------------------------+
| root@localhost | UpdateStudent Record (1 david 9) to(1 david 10)      |
| root@localhost | UpdateStudent Record (2 shah  9) to(2 shah 10)       |
| root@localhost | UpdateStudent Record (3 mike  9) to(3 mike 10)       |
| root@localhost | UpdateStudent Record (4 james 9) to(4 james 10)      |
+----------------+------------------------------------------------------+  

Result:
32

The above queries are executed successfully

Ex.No: 7
EXCEPTION HANDLING
Date:

Aim:
To Write a MySQL that handles all types of exceptions.
Algorithm:
STEP1: Start the program.
STEP2: Create a table with some valid data.
STEP3: write the MySQL program that to handle the exception on exception block
STEP4: Execute the MySQL program and give the input values or make the error on the
table data.
STEP5: Display the MySQL program error message.
STEP6: Stop the program.

QUERY EXECUTION:
mysql>DELIMITER $$
mysql>CREATE FUNCTION f_auth_ins(p_author_name VARCHAR(255))
RETURNS INTEGER(11)
LANGUAGE SQL
BEGIN
DECLARE v_auth_id INTEGER(11);
DECLARE v_author_name VARCHAR(255);
DECLARE v_counter INTEGER(11);
SET v_author_name := p_author_name;
SELECT COUNT(id) INTO v_counter FROM author WHERE
UPPER(author_name) = UPPER(v_author_name);
IF v_counter<> 1 THEN
SIGNAL SQLSTATE ‘42000’
SET MESSAGE_TEXT = ‘Author name not found in author table.’;
ELSE
SELECT id INTO v_auth_id FROM author WHERE UPPER(author_name) =
UPPER(v_author_name);
RETURN v_auth_id;
END IF;
END $$
mysql>DELIMITER;
mysql> INSERT INTO joke(joke_text, joke_date, author_id)
-> VALUES (‘Hurry up we are a freezin’, CURRENT_DATE, (SELECT
f_auth_ins(‘Casey Jones’)));
Query OK, 1 row affected (0.02 sec)
mysql> INSERT INTO joke(joke_text, joke_date, author_id)
-> VALUES(‘Hurry up we are a freezin’, CURRENT_DATE, (SELECT
f_auth_ins(‘Dirty Harry Jones’)));

ERROR 1644 (42000): Author name not found in author table.

Result:
The above queries are executed successfully.
33

Ex.No: 9
DATABASE CONNECTIVITY WITH FRONT END TOOLS
Date:

Aim:
To connect front end tool from MySQL database.
QUERY EXECUTION:
Java Database Connectivity with MySQL
To connect Java application with the MySQL database, we need to follow 5 following steps.
In this example we are using MySql as the database. So we need to know following informations for the mysql
database:
Driver class: The driver class for the mysql database is com.mysql.jdbc.Driver.
Connection URL: The connection URL for the mysql database is jdbc:mysql://localhost:3306/sonoo where jdbc is the
API, mysql is the database, localhost is the server name on which mysql is running, we may also use IP address, 3306 is
the port number and sonoo is the database name. We may use any database, in such case, we need to replace the
sonoo with our database name.
Username: The default username for the mysql database is root.
Password: It is the password given by the user at the time of installing the mysql database. In this example, we are
going to use root as the password.
Let's first create a table in the mysql database, but before creating table, we need to create database first.
create database sonoo;  
use sonoo;  
create table emp(id int(10),name varchar(40),age int(3));  

Connect Java Application with mysql database


In this example, sonoo is the database name, root is the username and password both.
import java.sql.*;  
class MysqlCon{  
public static void main(String args[]){  
try{  
Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
"jdbc:mysql://localhost:3306/sonoo","root","root");  
//here sonoo is database name, root is username and password  
Statement stmt=con.createStatement();  
ResultSet rs=stmt.executeQuery("select * from emp");  
while(rs.next())  
System.out.println(rs.getInt(1)+"  "+rs.getString(2)+"  "+rs.getString(3));  
con.close();  
}catch(Exception e){ System.out.println(e);}  
}  
}  
download this example
The above example will fetch all the records of emp table.
To connect java application with the mysql database, mysqlconnector.jar file is required to be loaded.
download the jar file mysql-connector.jar

Two ways to load the jar file:


Paste the mysqlconnector.jar file in jre/lib/ext folder
Set classpath
1) Paste the mysqlconnector.jar file in JRE/lib/ext folder:
Download the mysqlconnector.jar file. Go to jre/lib/ext folder and paste the jar file here.
2) Set classpath:
34

There are two ways to set the classpath:


temporary
permanent
How to set the temporary classpath
open command prompt and write:
C:>set classpath=c:\folder\mysql-connector-java-5.0.8-bin.jar;.;  
How to set the permanent classpath
Go to environment variable then click on new tab. In variable name write classpath and in variable value paste the path
to the mysqlconnector.jar file by appending mysqlconnector.jar;.; as C:\folder\mysql-connector-java-5.0.8-bin.jar;.;

Result:
The above queries are executed successfully
35

Ex.No: 10 MINI PROJECT - INVENTORY CONTROL


Date: SYSTEM
AIM:
To develop a Inventory Control System using MySQL as s back end(data base) and Microsoft Visual basic as a
front end.
TABLE CREATION:
TABLE NAME:SUPPLIER
MySQL> create table supplier(supno number(10),supname varchar2(20),supdatedate,price
number(20),quantity number(10),ITEM_NAME VARCHAR2(20));
MySQL> insert into supplier values(1,'pit','12-jan-2014',8000,2,'MONITOR');
MySQL> insert into supplier values(2,'PEC','6-MAR-2014',4000,1,'KEYBOARD');

TABLE NAME:ITEM
MySQL> CREATE TABLE ITEM(ITEMNO NUMBER(10),ITEM_NAME VARCHAR2(10),PRICE
NUMBER(10),QUAT_AVAILABLE NUMBER(10));
MySQL> INSERT INTO ITEM VALUES(101,'MONITOR',80000,3);
MySQL> insert into ITEM VALUES(102,'MOUSE',70000,10);
MySQL> COMMIT;

CODING
FORM1:
Private Sub Item_Click()
Form3.Show
End Sub

Private Sub Supplier_Click()


Form2.Show
End Sub

FORM2:
Private Sub Command1_Click()
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("supno") = Text1.Text
Adodc1.Recordset.Fields("supname") = Text2.Text
Adodc1.Recordset.Fields("supdate") = Text3.Text
Adodc1.Recordset.Fields("price") = Text4.Text
Adodc1.Recordset.Fields("quantity") = Text5.Text
Adodc1.Recordset.Fields("item_name") = Text6Text
Adodc1.Recordset.Update
MsgBox "Data Added"
End Sub

Private Sub Command10_Click()


Form3.Show
End Sub

Private Sub Command3_Click()


Adodc1.Recordset.Delete
Adodc1.Recordset.Update
End Sub

Private Sub Command4_Click()


36

Unload Me
End Sub

Private Sub Command5_Click()


If Adodc1.Recordset.EOF = False Then
Adodc1.Recordset.MoveNext
Else
MsgBox "END OF FILE!",vbOKOnly, "Warning"
End If
End Sub

Private Sub Command6_Click()


Adodc1.Recordset.MoveFirst
End Sub

Private Sub Command7_Click()


Adodc1.Recordset.MoveLast
End Sub

Private Sub Command8_Click()


If Adodc1.Recordset.BOF = False Then
Adodc1.Recordset.MovePrevious
Else
MsgBox "BEGIN OF FILE!!",vbOKOnly, "Warning"
End If
End Sub

Private Sub Command9_Click()


Form1.Show
End Sub

FORM3:
Private Sub Command1_Click()
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("itemno") = Text1.Text
Adodc1.Recordset.Fields("item_name") = Text2.Text
Adodc1.Recordset.Fields("price") = Text4.Text
Adodc1.Recordset.Fields("quat_available") = Text5.Text
Adodc1.Recordset.Update
MsgBox "Data Added"
End Sub

Private Sub Command10_Click()


Form2.Show
End Sub

Private Sub Command3_Click()


Adodc1.Recordset.Delete
Adodc1.Recordset.Update
End Sub

Private Sub Command4_Click()


Unload Me
End Sub
37

Private Sub Command5_Click()


If Adodc1.Recordset.EOF = False Then
Adodc1.Recordset.MoveNext
Else
MsgBox "END OF FILE!",vbOKOnly, "Warning"
End If
End Sub

Private Sub Command6_Click()


Adodc1.Recordset.MoveFirst
End Sub

Private Sub Command7_Click()


Adodc1.Recordset.MoveLast
End Sub

Private Sub Command8_Click()


If Adodc1.Recordset.BOF = False Then
Adodc1.Recordset.MovePrevious
Else
MsgBox "BEGIN OF FILE!!",vbOKOnly, "Warning"
End If
End Sub

Private Sub Command9_Click()


Form1.Show
End Sub

SCREEN SHOTS:

HOME PAGE
38

SUPPLIER FORM

ITEM FORM

Result:
Thus the Inventory Control System Mini project has been implemented and executed successfully.\

You might also like