Unit IV SQL
Unit IV SQL
SQL
Introduction to Structured Query Language
(SQL)
• Data :- Raw facts and figures which are useful to an
organization. We cannot take decisions on the basis of data.
• INSERT
Used to enter data into table
Syntax:
INSERT INTO table_name
VALUES (value1, value2, … , valuen);
Adding Table Rows (continued)
DELETE
• Deletes a table row
Syntax:
DELETE FROM tablename
[WHERE conditionlist ];
WHERE condition is optional
Inserting Table Rows with a Select Subquery
INSERT
• Inserts multiple rows from another table (source)
• Uses SELECT subquery
• Query that is embedded (or nested) inside another query
• Executed first
Syntax:
• INSERT INTO tablename SELECT columnlist FROM
tablename;
Selecting Rows with Conditional Restrictions
• BETWEEN
• Used to check whether attribute value is within a range
• IS NULL
• Used to check whether attribute value is null
• LIKE
• Used to check whether attribute value matches given string
pattern
Special Operators (continued)
• IN
• Used to check whether attribute value matches any value within a
value list
• EXISTS
• Used to check if subquery returns any rows
Use of AGGREGATE functions in SQL.
Performing calculations on multiple rows of a single column
of a table and returning a value.
Create table with following attributes
from books
where
released_year !=‘2018’;
Select title, released_year
from books
where
released_year>2018;
Select title, released_year
from books
where
released_year>=2018;
mysql>select 99>1 mysql> select 99<1;
+------+
+------+ | 99<1 |
| 99>1 | +------+
+------+ | 0|
| 1| +------+
+------+
1 row in set (0.00 sec)
1 row in set (0.00 sec)
Exercise:
WHERE
condition1
AND
condition2
AND ...conditionN;
Exercise:
SELECT * FROM
table_name
WHERE
condition1
OR condition2
OR... conditionN;
Exercise:
SELECT * FROM
table_name WHERE
condition1 AND
(condition2 OR condition3);
SELECT attribute1,attribute2, attribute3
FROM tablename
WHERE attribute3
FROM customer
WHERE
AND
AND
NOT grade = 1;
SELECT
Attributes
FROM
Table_name
WHERE NOT (attr1 = '' OR attr2 = '') AND attr3 and attr < ;
SELECT * FROM books
WHERE EXISTS
(SELECT pages FROM books WHERE Pages > 50);
SELECT * FROM title
mysql> desc a;
+---------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| roll_no | int(11) | YES | | NULL | |
| name | char(20) | YES | | NULL | |
| branch | char(20) | YES | | NULL | |
+---------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> select * from a;
+---------+------+--------+
| roll_no | name | branch |
+---------+------+--------+
| 1 | abc | comp |
| 2 | abcd | comp |
| 3 | bcd | IT |
| 4 | cd | IT |
+---------+------+--------+
4 rows in set (0.00 sec)
mysql> create table c(roll_no int, name char(20), branch char(20));
Query OK, 0 rows affected (0.34 sec)
The SQL LEFT JOIN (specified with the keywords LEFT JOIN and ON)
joins two tables and fetches all matching rows of two tables for which
the SQL-expression is true,
plus rows from the first table that do not match any row in the second table.
mysql> select c.name,c.branch from c right join
a on a.roll_no=c.roll_no;
Cross join
The SQL CROSS JOIN produces a result set which is the number of
rows in the first table multiplied by the number of rows in the second
table
+---------+------+--------+---------+------+--------+
| roll_no | name | branch | roll_no | name | branch |
+---------+------+--------+---------+------+--------+
mysql> select * from a cross | 1 | abc | comp | 1 | abc | mech |
join c; | 2 | abcd | comp | 1 | abc | mech |
| 3 | bcd | IT | 1 | abc | mech |
| 4 | cd | IT | 1 | abc | mech |
| 1 | abc | comp | 2 | bc | civil |
| 2 | abcd | comp | 2 | bc | civil |
| 3 | bcd | IT | 2 | bc | civil |
| 4 | cd | IT | 2 | bc | civil |
| 1 | abc | comp | 3 | bcd | comp |
| 2 | abcd | comp | 3 | bcd | comp |
| 3 | bcd | IT | 3 | bcd | comp |
| 4 | cd | IT | 3 | bcd | comp |
| 1 | abc | comp | 1 | abc | comp |
| 2 | abcd | comp | 1 | abc | comp |
| 3 | bcd | IT | 1 | abc | comp |
| 4 | cd | IT | 1 | abc | comp |
| 1 | abc | comp | 3 | bcd | IT |
| 2 | abcd | comp | 3 | bcd | IT |
| 3 | bcd | IT | 3 | bcd | IT |
| 4 | cd | IT | 3 | bcd | IT |
+---------+------+--------+---------+------+--------+
20 rows in set (0.00 sec)
Null Values
The IS NULL command is used to test for empty values (NULL values).
mysql> select * from a; mysql> select roll_no *
+---------+------+ (isnull(roll_no)) from a;
| roll_no | name | +-----------------------------+
+---------+------+ | roll_no * (isnull(roll_no)) |
| 1 | abcd | +-----------------------------+
| NULL | bcd | | 0|
| 0 | ab | | NULL |
| NULL | dbms | | 0|
+---------+------+ | NULL |
4 rows in set (0.00 sec) +-----------------------------+
4 rows in set (0.00 sec)
Views
• A database view is a virtual table or logical table which is defined as
a SQL SELECT query with joins.
FROM table_name
WHERE condition;
To view the virtual table
Views
It means that even the value of the IN parameter is changed inside the stored
procedure, its original value is retained after the stored procedure ends. In other
words, the stored procedure only works on the copy of the IN parameter.
OUT – the value of an OUT parameter can be changed inside the
stored procedure and its new value is passed back to the calling
program.
Stored procedure cannot access the initial value of
the OUT parameter when it starts.
INOUT – an INOUT parameter is a combination
of IN and OUT parameters. It means that the calling program may
pass the argument, and the stored procedure can modify
the INOUT parameter, and pass the new value back to the calling
program.
DELIMITER //
Example
CREATE PROCEDURE GetOffice
ByCountry
(IN countryName VARCHAR(255))
IN parameter in BEGIN
the GetOfficeByCountry
SELECT *
stored procedure that
selects offices located in a FROM offices
particular country. WHERE country = countryName;
END //
DELIMITER ;
mysql> create procedure dt() select Curdate();
Query OK, 0 rows affected (0.32 sec)
END; /
SHOW ERRORS;
Using DROP procedure procedure_name you can drop the
procedure.
Debugging Complex SQL Queries – The General
Process
• Check on syntax and logic:
• The first basic step is investigating the SQL code.
• The different SQL statements like an update, insert, or delete
statements can be nested together.
• You need to work on the testing logic and separate the level of
nesting too.
• Now substitute representative values into correlated subqueries
for the automatic execution. These subsequent debugging steps
are able to handle only one query at a time.
• Check on SQL joins:
• Once you have resolved issues related to syntax or flow, the next
focus is joining logic.
• It is easy to run afoul of joins so we have to make sure that we are
not losing records accidentally.
• Check on Predicates:
• We have to restore the “where predicates” and run the query
again. In case, the query fails to run, there is clearly something
wrong with predicates.
• Read the code carefully that will help you to find errors certainly.
If you are still not able to spot errors then step back and restore a
few predicates at one time.
• Check on attributes:
• If joins and predicates both are correct, the next step is to focus
on attributes.
• We introduce multiple attributes together and you have to make
sure all attributes ensure the right results.
• Check on nesting queries: In the final step, put the nested queries
together from the initial SQL statement. We should test the SQL
statements thoroughly to make sure that it runs correctly.
Things to Consider when Writing Complex SQL Queries
• Before JDBC, ODBC API was the database API to connect and
execute the query with the database.
• ODBC API uses ODBC driver which is written in C language (i.e.
platform dependent and unsecured). That is why Java has defined its
own API (JDBC API) that uses JDBC drivers (written in Java language).
• We can use JDBC API to handle database using Java program and can
perform the following activities:
• Connect to the database
• Execute queries and update statements to the database
• Retrieve the result received from the database.
Basic steps to use a database in Java
1. Establish a connection
2. Create JDBC Statements
3. Execute SQL Statements
4. GET Result Set
5. Close connections
136
• (“oracle.jdbc.driver.OracleDriver”)
• Close Connection
• stmt.close();
• con.close();
Dynamic SQL
• Applications that query a database where new tables are created often.
Q&A
What are the technical specifications of MySQL?
author_lname
FROM
books
WHERE
ORDER BY author_fname;
What is a View?