Data Definition Language (DDL) (Create, Truncate, Alter, Rename, Drop, Comment)
Data Definition Language (DDL) (Create, Truncate, Alter, Rename, Drop, Comment)
Data Definition Language (DDL) (Create, Truncate, Alter, Rename, Drop, Comment)
Ex.No: 1.1
DATA DEFINITION LANGUAGE (DDL)
Date: (CREATE,TRUNCATE,ALTER, RENAME, DROP, COMMENT)
Aim:
Create
Alter
Drop
Rename
Truncate
CREATE:
Syntax:
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
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:
mysql>droptablestudent;
Query OK, 0 rowsaffected (0.48 sec)
mysql> select* fromstudent;
ERROR 1146 (42S02): Table'employee.student'doesn't exist
RENAME:
SYNTAX:
TRUNCATE:
Syntax:
Result:
The above query was executed successfully.
4
Ex.No:1.2
DATA MANIPULATION LANGUAGE (DML)
Date: (INSERT,UPDATE,DELETE,SELECT)
Aim:
QUERY EXECUTION:
1. Insert
2. Update
3. Delete
4. select
1. INSERT
Syntax:
+--------+-------+-------+
| 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:
3. DELETE
Syntax:
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:
Algorithm:
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:
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)
Result:
The above queries are executed successfully
9
Ex.No:1.4
TRANSACTION CONTROL LANGUAGE (TCL)
Date: (COMMIT, SAVEPOINT, ROLLBACK)
Aim:
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:
QUERY EXECUTION:
(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:
+---------+--------+-------+
+---------+--------+-------+
| 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:
Algorithm:
+-------------+
| artist_name |
+-------------+
| Miles Davis |
+-------------+
-> (SELECT artist_id FROM album WHERE album_name = "In A Silent Way");
+-------------+
| artist_name |
+-------------+
| Miles Davis |
+-------------+
Result:
The above queries are executed successfully
14
Ex.No:2.3
DATABASE QUERYING –SUB QUERIES
Date:
Aim:
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:
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:
Cartesian join
Equi join
Non equi join
Outer join
1. CARTESIAN JOIN
Syntax:
Example:
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:
Example:
4 rowsinset(0.05 sec)
Syntax:
Example:
4. OUTER JOIN
Syntax:
Example:
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:
Example:
Ex.No:3.1
VIEWS
Date:
Aim:
To create the view, execute and verify the various operations on views.
Algorithm:
QUERY EXECUTION
MySQL Views object is mainly classified into
Create view
Alter view
Drop view
1. CREATE VIEW
Syntax:
Example:
2. ALTER VIEW
Syntax:
Example:
3. DROP VIEW
Syntax:
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:
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> 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>
mysql> call myProc('Jason','Martin','NewCity','New Description',@myCode,@myMessage);
Query OK, 1 row affected (0.01 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
Result:
32
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’)));
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));
Result:
The above queries are executed successfully
35
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
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
Unload Me
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
SCREEN SHOTS:
HOME PAGE
38
SUPPLIER FORM
ITEM FORM
Result:
Thus the Inventory Control System Mini project has been implemented and executed successfully.\