You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+278Lines changed: 278 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1332,6 +1332,284 @@ CREATE VIEW trackView AS
1332
1332
SELECT * FROM trackView;
1333
1333
```
1334
1334
1335
+
<div align="right">
1336
+
<b><a href="#table-of-contents">↥ back to top</a></b>
1337
+
</div>
1338
+
1339
+
## # 20. SQL Triggers
1340
+
1341
+
<br/>
1342
+
1343
+
## Q. What are the triggers in SQL?
1344
+
1345
+
A trigger is a stored procedure in database which automatically invokes whenever a special event in the database occurs. For example, a trigger can be invoked when a row is inserted into a specified table or when certain table columns are being updated.
1346
+
1347
+
**Syntax:**
1348
+
1349
+
```sql
1350
+
CREATE [OR REPLACE ] TRIGGER <trigger_name>
1351
+
{BEFORE | AFTER | INSTEAD OF }
1352
+
{INSERT [OR] | UPDATE [OR] | DELETE}
1353
+
ON <table_name>
1354
+
[FOR EACH ROW]
1355
+
WHEN (condition)
1356
+
[trigger_body]
1357
+
```
1358
+
1359
+
**Example - 01:**
1360
+
1361
+
```sql
1362
+
CREATE TRIGGER employee_name
1363
+
after INSERT
1364
+
on
1365
+
employee
1366
+
for each row
1367
+
BEGIN
1368
+
UPDATE employee set full_name = first_name || '' || last_name;
1369
+
END;
1370
+
```
1371
+
1372
+
<div align="right">
1373
+
<b><a href="#table-of-contents">↥ back to top</a></b>
1374
+
</div>
1375
+
1376
+
#### Q. Why and when to use a trigger?
1377
+
#### Q. What are the different types of triggers?
1378
+
#### Q. How many TRIGGERS are possible in MySql?
1379
+
1380
+
*ToDo*
1381
+
1382
+
<div align="right">
1383
+
<b><a href="#table-of-contents">↥ back to top</a></b>
1384
+
</div>
1385
+
1386
+
## # 21. SQL Cursors
1387
+
1388
+
<br/>
1389
+
1390
+
## Q. What is a cursor?
1391
+
1392
+
When we execute any SQL operations, SQL Server opens a work area in memory which is called Cursor. When it is required to perform the row by row operations which are not possible with the set-based operations then cursor is used.
1393
+
1394
+
There are two of cursors:
1395
+
1396
+
**1. Implicit Cursor:**
1397
+
1398
+
Implicit Cursors are also known as Default Cursors of SQL SERVER. These Cursors are allocated by SQL SERVER when the user performs DML operations.
1399
+
1400
+
**2. Explicit Cursor:**
1401
+
1402
+
* When the programmer wants to perform the row by row operations for the result set containing more than one row, then he explicitly declare a cursor with a name.
1403
+
1404
+
* They are managed by OPEN, FETCH and CLOSE.
1405
+
1406
+
* %FOUND, %NOFOUND, %ROWCOUNT and %ISOPEN attributes are used in both types of cursors.
1407
+
1408
+
**1. Declare Cursor Object:**
1409
+
1410
+
**Syntax:**
1411
+
1412
+
```sql
1413
+
--- DECLARE cursor_name CURSOR FOR SELECT * FROM table_name
1414
+
DECLARE s1 CURSOR FOR SELECT * FROM studDetails
1415
+
```
1416
+
1417
+
**2. Open Cursor Connection:**
1418
+
1419
+
```sql
1420
+
-- OPEN cursor_connection
1421
+
OPEN s1
1422
+
```
1423
+
1424
+
**3. Fetch Data from cursor:**
1425
+
1426
+
There are total 6 methods to access data from cursor.
1427
+
1428
+
* **FIRST** - is used to fetch only the first row from cursor table.
1429
+
* **LAST** - is used to fetch only last row from cursor table.
1430
+
* **NEXT** - is used to fetch data in forward direction from cursor table.
1431
+
* **PRIOR** - is used to fetch data in backward direction from cursor table.
1432
+
* **ABSOLUTE** - n is used to fetch the exact nth row from cursor table.
1433
+
* **RELATIVE** - n is used to fetch the data in incremental way as well as decremental way.
1434
+
1435
+
```sql
1436
+
FETCH FIRST FROM s1
1437
+
FETCH LAST FROM s1
1438
+
FETCH NEXT FROM s1
1439
+
FETCH PRIOR FROM s1
1440
+
FETCH ABSOLUTE 7 FROM s1
1441
+
FETCH RELATIVE -2 FROM s1
1442
+
```
1443
+
1444
+
**4. Close cursor connection:**
1445
+
1446
+
```sql
1447
+
--- CLOSE cursor_name
1448
+
CLOSE s1
1449
+
```
1450
+
1451
+
**5. Deallocate cursor memory:**
1452
+
1453
+
```sql
1454
+
--- DEALLOCATE cursor_name
1455
+
DEALLOCATE s1
1456
+
```
1457
+
1458
+
<div align="right">
1459
+
<b><a href="#table-of-contents">↥ back to top</a></b>
1460
+
</div>
1461
+
1462
+
## Q. When is the Explicit Cursor Used?
1463
+
1464
+
## # 22. SQL Stored Procedures
1465
+
1466
+
<br/>
1467
+
1468
+
## Q. Why stored procedures are called as executable code?
1469
+
1470
+
Stored procedure stored inside the database. This also includes the executable code that usually collects and customizes the operations like insert, encapsulation, etc. These stored procedures are used as APIs for simplicity and security purposes. The implementation of it allows the developers to have procedural extensions to the standard SQL syntax. Stored procedure doesn'\t come as a part of relational database model, but can be included in many implementations commercially.
1471
+
1472
+
<div align="right">
1473
+
<b><a href="#table-of-contents">↥ back to top</a></b>
1474
+
</div>
1475
+
1476
+
## Q. What are the advantages of using Stored Procedures?
1477
+
1478
+
- Procedure can reduce network traffic and latency, and can enhance application performance.
1479
+
- Procedure execution plans can be reused, staying cached in the management tool's memory, reducing its overhead.
1480
+
- Procedures provide the benefit of code reuse.
1481
+
- The logic can be encapsulated using procedures and can help to change procedure's code without interacting to application.
1482
+
- Procedures give more security to our data.
1483
+
1484
+
<div align="right">
1485
+
<b><a href="#table-of-contents">↥ back to top</a></b>
1486
+
</div>
1487
+
1488
+
## Q. What is stored procedure in SQL?
1489
+
1490
+
Stored Procedures are created to perform one or more DML operations on Database. It is nothing but the group of SQL statements that accepts some input in the form of parameters and performs some task and may or may not returns a value.
1491
+
1492
+
**Syntax:**
1493
+
1494
+
```sql
1495
+
CREATE or REPLACE PROCEDURE name(parameters)
1496
+
IS
1497
+
variables;
1498
+
BEGIN
1499
+
//statements;
1500
+
END;
1501
+
```
1502
+
1503
+
**Example:**
1504
+
1505
+
```sql
1506
+
CREATE PROCEDURE SelectAllCustomers
1507
+
AS
1508
+
SELECT * FROM Customers
1509
+
GO;
1510
+
```
1511
+
1512
+
Execute the stored procedure above as follows:
1513
+
1514
+
```sql
1515
+
EXEC SelectAllCustomers;
1516
+
```
1517
+
1518
+
<div align="right">
1519
+
<b><a href="#table-of-contents">↥ back to top</a></b>
1520
+
</div>
1521
+
1522
+
## Q. What is Stored Routine in SQL?
1523
+
1524
+
A stored routine is a set of SQL statements that are stored on the database server and can be used by any client
1525
+
with permission to use them. This provides a number of benefits.
1526
+
1527
+
1. Database operations are normalized so various applications will operate uniformly, even when written in different
1528
+
languages and operating on different platforms.
1529
+
2. Stored routines are easy to maintain, because they're all in one place rather than distributed among different applications.
1530
+
3. Traffic is reduced between the client and server, because data is processed on the server.
1531
+
4. security is enhanced by allowing clients to run with reduced permissions while still being able to perform necessary
1532
+
database operations.
1533
+
1534
+
There are two different kinds of stored routines.
1535
+
1536
+
a) Stored functions return a value, and are used in the context of an expression.
1537
+
b) Stored procedures are called separately, using the call statement, and may return result sets or set variables.
1538
+
1539
+
**Example 01:** Stored Functions
1540
+
1541
+
```sql
1542
+
DROP FUNCTION IF EXISTS track_len;
1543
+
1544
+
CREATE FUNCTION track_len(seconds INT)
1545
+
RETURNS VARCHAR(16) DETERMINISTIC
1546
+
RETURN CONCAT_WS(':', seconds DIV 60, LPAD(seconds MOD 60, 2, '0' ));
1547
+
1548
+
SELECT title, track_len(duration) FROM track;
1549
+
1550
+
SELECT a.artist AS artist,
1551
+
a.title AS album,
1552
+
t.title AS track,
1553
+
t.track_number AS trackno,
1554
+
track_len(t.duration) AS length
1555
+
FROM track AS t
1556
+
JOIN album AS a
1557
+
ON a.id = t.album_id
1558
+
ORDER BY artist, album, trackno
1559
+
;
1560
+
```
1561
+
1562
+
**Example 02:** Stored Procedures
1563
+
1564
+
```sql
1565
+
DROP PROCEDURE IF EXISTS list_albums;
1566
+
1567
+
DELIMITER //
1568
+
CREATE PROCEDURE list_albums ()
1569
+
BEGIN
1570
+
SELECT * FROM album;
1571
+
END
1572
+
//
1573
+
1574
+
DELIMITER ;
1575
+
CALL list_albums();
1576
+
```
1577
+
1578
+
**Example 03:** Stored Procedures with parameter
1579
+
1580
+
```sql
1581
+
DROP PROCEDURE IF EXISTS list_albums;
1582
+
1583
+
DELIMITER //
1584
+
CREATE PROCEDURE list_albums (a VARCHAR(255))
1585
+
BEGIN
1586
+
SELECT a.artist AS artist,
1587
+
a.title AS album,
1588
+
t.title AS track,
1589
+
t.track_number AS trackno,
1590
+
track_len(t.duration) AS length
1591
+
FROM track AS t
1592
+
JOIN album AS a
1593
+
ON a.id = t.album_id
1594
+
WHERE a.artist LIKE a
1595
+
ORDER BY artist, album, trackno
1596
+
;
1597
+
END //
1598
+
1599
+
DELIMITER ;
1600
+
CALL list_albums('%hendrix%');
1601
+
```
1602
+
1603
+
**Example 04:** Drop Stored Procedures & Stored Functions
1604
+
1605
+
```sql
1606
+
DROP FUNCTION IF EXISTS track_len;
1607
+
DROP PROCEDURE IF EXISTS total_duration;
1608
+
```
1609
+
1610
+
#### Q. What is the difference between Stored Procedure and User Defined Function?
1611
+
#### Q. How can you raise custom errors from stored procedure?
1612
+
1335
1613
<div align="right">
1336
1614
<b><a href="#table-of-contents">↥ back to top</a></b>
0 commit comments