0% found this document useful (0 votes)
20 views9 pages

Adobe Scan Dec 02, 2022

Queries
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views9 pages

Adobe Scan Dec 02, 2022

Queries
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Ex No:8,a) TRIGGERS

Date:
AIM:

QUERIES:

ysql> CREATE DATABASE Paavai;


Query OK, 1 row affected (o.C1 sec)
mysq1> USE paavaij
Database changed
mysql> CREATE TABLE account (Acc_num INT, Amount DECIMAL (10,2))
Query OR, 0 rows affected (0.c2 sec)

mysql> CREATE TRIGGER ins_sum BEFORE INSERT ON account

FOREACH
Query OR,
ROW SET 8sum-0s um+NEW. amount;
0 rows affected (0.0l sec)

mysql SET 0sum0


Query OK, rows attected (0.00 sec)

yagNSERT INTO
Ouery OR, 3 rows
account
atfected VALUE
(C.0l (137,
sec)
14. 98), (141,1937.50), (97, -100.00) ;

Records: 3 Duplicates:

TOtalAmount Inserted';
ysq
+--
SELECTesum AS
TOtal Amount inSerted
* * * * * * *

LBS2, 48
row in set (C.00 sec)

mysq DROP DATABASE paavai;


row affected (o.01
guery oR, sec)
CURSOR
Ex No: 8, b)
Date:

AIM:

QUERIES:

mysqCREATE DATABASE paavaii


row attected (0.d0 sec)
Query OK,

mysq1> USE paavai


Database changed
TABLE t u t o r i a l s (ID INT PRIMARY REY,TITLE
mysqi CREATE
VARCHAR (50), AUTHOR VARCHAR (50) DATE VARCHAR (s0))
g u e r y OK EOWs a t t e c t e d td.G ec

"Cl-09-2019")
mysq
1nsert into tutorias
values (1, 'PHP", "Vinothkumar ',
row attected (0.cl sec)
Query OR,
"HTML', "Ram", '21-06-2019');
mysql insert into tutorials values (2,
Query OK EOW artected (o.u ec

mysql> insert into tutorials values (3, 'C+*', "krsihna ", '16-05-2019') ;

rOw atfected (0.c0 sec)


Query OR,
mysq1> insert into tutorials values (4, ' JAVA", 'Tamil', *02-11-2019");
Query OK, 1 row affected (0.00 sec)

CREATE backup (ID


TABLE INT, TITLE VARCHAR (50),AUTHOR
mysqi
VARCHAR(50), DATE VARCHAR (50))
artected sec)
uey OK rOws
( . C2
mysql DELIMITER //
PROCEDURE
mysql> CREATE Exampl eProc ()
DECLARE done INT DEFAULT 0
rialtD INTEGER:
DECLARE tutorialTitle, tutorialAuthor, tutorial Date VARCHAR (50);
->DECLARE Cur cURSOR FOR SELECT *FROM TOTORIALS;
->DECLARE CONTINUE HANDLER FOR NOT FOUND SET done-l;
> OPEN CUr?
> 1abel: LoOP
FETCH Cur INTO
tutorialID, tutorialTitle, tutorialAuthor, tutorialDate;
INSERT INTO backup VALUES
(tutorialID, tutorialTitle, tutorialAuthor, tutorial Date)
I F done-l THEN LEAVE label;
END

END
-> END//
Query OR, 0 rows affected (0.01 sec)

mysqi> DELIMITER

mysql> CALL ExampleProc


EOW artected (0.02 sec)
Query OK,
mysql> SELECT" FROM backup;

-- -+-******************
I ID TITLE | AUTHOR DA

Vinothkumar 0l-09-2019 |
I 1I PHP
I I
21-06-2019
ihna 16-05-2019
4 JAVA Tamil 02-11-2019 |
4I JAVA Tamil I02-1l-2019 T
t*-**-*-*-*---*--

9 rows in set (0.00 sec)

mysql> DROP DATABASE paavai


OK, 2 roWs artected (C.c2 sec)
Query
| Ex No: 9 MENU
Date:

AIM

QUERIES:

1) MENU DATABASE TABLE


My5QL
CREATE TABLE menu_items item id bigint (20) NoT NOLL,
NOT
parent1d b1gint (20) DEPAULT NOLL, 1tem_text varchar (255)
varchar (255) NOT NOL, 1tem_target varchar (255)
NULL, 1tem_1ink
DEFAULT NULL) ENGIND=InnoDB DEPAULT CHARSET=UttBmb 4
ALTER TABLE menu_items ADD PRIMARY R E Y 1 t e m _ i d ),ADD KEY

parent i d );
ALTER TABLE menu_items MODIPY item1d bigint (20) NOT NUL

AUTO_INCREMENT

2) GET MENU ITEMS FROM DATABASE


PHP:

pnp
cONNECT TO DATABASE -

CHANGE SETTINGS TO YOUR OWN!


I1 (A)

SdbHost "loca Lhost


SdbName "test";

sdbChar "utf8";
$dbUser "root "

SdbPass ""
pdo new PDO(

mysql:host=$dbHost; dbname-$ dbName; charset $dbChar",


SdbUser, sdbPass,
PDO: ATTR_ERRMODE => PD0: :ERRMODE _EXCEPTION,

PDO: :ATTR_DEFAUL"_FETCH_MODE => PDO: :FETCH_ASSOC

I/ (B) DRILL DOWN GET MENU ITEMS


1 ARRANGE BY [PARENT ID] => IMENU ITEMS|

Smenu li Snext (0];

while true)
$stmt $pdo->prepare (sprint
"SELECT FROM menu_items WHERE parent_id IN (a) ",

1mplode( " " , Snext)

$stmt->execute(;
Snext (1
while (5r Sstmt->tetch 0)
=

if (isset (Smenu [$r ["parent_id"]])) Smenu [Sr["parent_id"]]


Sr;
Smenur°parent_id"]] [Sr["item_id"]l
=

Snext [ - $r["item_id"] ;

if (count (Snext)=- 0) (break; )

CCLOSE DATABASE CONNECTION


$stmt null;

Spdo= null;

3) DRAW HTML MENU

MENU DESIGN:

nav class= "menu"><?php

11 (A) SUPPORT FUNCT ION TO DRAW AN <A>

function draw ($i)


printi("ca $shref-"*s*>*s</a>",
si[item_target"] !-** ? "target-"", $i("item_target"]

$i["item_link"], $i["item_text"]

1(B) DRAW MENU ITEMS


require "2-menu.php";
toreach (Smenu [O] as sid=>$i) {

(B1) WITH SUB-1TEMS


it (isset (Smenu [ $id] ) ) (?>

<div class="mGrp">

<div class-"nTitle"><?-$i ["item_text"]?></div>


<div class="mItems"><?php

foreach scid=>Sc)
(Smenu [$id] as
draw (Sc);
?></ div>

/div>
<?php
/7 (B2) SINGLE MENU ITEM

else draw ($i):}

?></nav>
ExNo:10 DATABASE CONNECTIVITY

Date:

AIM:

QUERIES:

a) CREATE TABLE:
Create table by using python:
import mysql.connector
(
nya mysq.Connector .connect
host"ocainost
user"root
password- "naveen"

database"paaval

mycursor mydb.cursor ()

mycursor.execute("CREATE TABLE Student (name VARCHAR (255),


address VARCHAR (255)) ")

b) INSERT VALUE:
Insert Value by using Python :

import mysql.connector

mydbmysq.connector.connect
hostocainost
user AVeen",
Oanaveen",
aatabase="paavai"

mycursor myab.cursor (
sql "INSERT INTO student (name, address) VALUES (s, s)*
val
Naveen',
'Srivilliput tur ')
(Sampath Kumar" "Villupuram"),
(SILambarasan' Perambalur")
Thamizharasan ' "Perambalur ")
Vinoth', "GOA')
('VinothKumar 'Thiruvanamala1"),

mycursor . executemany ( s q , val)

mydb.commit ()

print (mycursor .rowcount, "record was inserted. ")

c) UPDATE VALUE:
Update Value by using python:

mport mysql.connector

mydb mysql.connector.connect (
hostLocalhost"
u s e r " root"

passworanaveen
database="paavai"

mycursor mydb.cursor ()
sqlUPDATE Student SET address ts WHERE address- is"
val ("Ariyalur", "GOA")
mycursor . execute (sql, val

mydb. commit ()

print (mycursor . rOwcount, "record (s) affected")

d)SELECT:
Select Queries by usingg python :
mport mysqi.conhector

myad mysqconnector.Connect(
host-"iocalhost",
user "root",
password-"naveen",
database="paavai"

mycursor mydb.cursor ()
mycursor.execute ("SELECT FROM Student"))

myresult mycursor . fetchall ()

tor x in myresult:
t (x)

e) DELETE

Delete Queries by using python:


p o r t mysq.connector

mydb mysqi.connector.connect
host "ocalhost "
user to
databa Veen
database "paavai

mycursor mydb.cursor (

mycursor.execute ("SELECT* FROM Student ")


myresult mycursor . fetchall()
ror * in myresult:

print

33/ 33

You might also like