Dbs LS10EN SQL-DML
Dbs LS10EN SQL-DML
Summary
• HR sample database
• INSERT Statement Overview
• UPDATE Statement Overview
• DELETE Statement Overview
• SELECT Statement Overview
o Select Queries and Relation Algebra Operations
o SELECT Statement Basic Syntax
o SELECT Statement Full Syntax
o Select Query - Order of Execution
• SQL Functions
o Numeric Aggregate Functions
o Numeric Arithmetic Functions
o Character Functions
o Date Functions
o Advanced Functions
SQL DML.
SQL Data Manipulation Language modifies the database instance by inserting, updating, deleting and selecting, its data. DML is responsible
for all forms data modification in a database. SQL contains the following set of commands in its DML section:
• INSERT INTO/VALUES
• UPDATE/SET/WHERE
• DELETE FROM/WHERE
• SELECT/FROM/WHERE
These basic constructs allow database programmers and users to enter data and information into the database and retrieve efficiently using a
number of sorting, filtering and grouping options with using different function and mathematical calculation.
© Yuriy Shamshin 1/18
HR sample database.
You can use SQL Tutorial site https://www.sqltutorial.org/seeit/ for online testing examples and exercises on real DB.
1. The number of values must be the same as the number of columns. The columns and values must be the correspondent.
2. Before adding a new row, the database system checks for all integrity constraints e.g., FK constraint, PK constraint, CHECK constraint
and NOT NULL constraint. If one of these constraints is violated, the database system will issue an error and terminate the statement
without inserting any new row into the table.
3. If you don’t specify a column and its value in the INSERT statement when that column will take a default value specified in the table
structure (for example column2). The default value could be ‘string’, or 0, or a next integer value in a sequence, the current date/time, a
NULL value, etc.
4. If the row is inserted successfully, the database system returned the number of the affected rows: “Affected rows: 1”
Example for DB: dependent(depId, depFname, depLname, relationship, empId) >0---have---|- employees(empId, empFname, empLname, …)
INSERT INTO dependent (depFname, depLname, relationship, empId)
VALUES ('Ivan', 'Green', 'Son', 25); -- depId adding as default sequence value
You can check whether the row has been inserted successfully or not by using the following SELECT statement.
SELECT * FROM dependent
WHERE empId = 25;
© Yuriy Shamshin 3/18
Insert multiple rows into a table.
Syntax.
INSERT INTO table1
(column1, column2,...)
VALUES
(value1, value2,...),
(value1, value2,...),
...;
Example for DB: dependent(depId, depFname, depLname, relationship, empId) >0---have---|- employees(empId, empFname, empLname, …)
INSERT INTO dependent
(depFname, depLname, relationship, empId)
VALUES
('Max', 'Black', 'Son', 27),
('Alexa', 'Black', 'Doter', 27);
Syntax.
UPDATE table_name
SET
column1 = value1,
column2 = value3
[WHERE
Condition];
In this syntax:
1. Indicate the table that you want to update in the UPDATE clause.
2. Specify the columns to modify in the SET clause. The columns that are not listed in the SET clause will save original values.
3. Specify which rows to update in the WHERE clause, any row that causes the condition in the WHERE to evaluate to true will be modified.
4. Because the WHERE clause is optional, therefore, if you omit it, the all the rows in the table will be affected.
In the SET clause, instead of using the literal values, we used a subquery to get the corresponding last name value from the employees table.
Syntax.
DELETE
FROM
table_name
WHERE
condition;
In this syntax:
1. Provide the name of the table where you want to remove rows.
2. Specify the condition in the WHERE clause to identify the rows that need to be deleted.
3. If you omit the WHERE clause all rows in the table will be deleted. Therefore, you should always use the DELETE statement with caution.
4. The DELETE statement does return the number of rows deleted.
You can verify that the row with the dependent id 16 has been deleted by using the following statement:
SELECT COUNT(*)
FROM dependent
WHERE depId = 16;
Operations
SELECT A1, A2, ... – corresponds to a relational algebra Project operation (columns)
Π(…)
FROM r1, r2, ... – corresponds to a Cartesian product (tables) of relations r1, r2, …
r1×r2×…
WHERE P – corresponds to a Selection operation (rows). Can be omitted. When left off, P = true
σP(…)
SELECT A1, A2, ... FROM r1, r2, ... WHERE P; Assembling it all – equivalent to:
Π(σP(r1×r2×…))
SELECT
column1, column2, column3, ...
FROM
table_name;
In this syntax, you specify a list of comma-separated columns from which you want to query the data in the SELECT clause and specify the
table name in the FROM clause. When evaluating the SELECT statement, the database system evaluates the FROM clause first and then the
SELECT clause.
The semicolon (;) is not the part of a query. Typically, the database system uses the semicolon to separate two SQL queries.
In case you want to query data from all columns of a table, you can use the asterisk (*) operator instead of the column list as shown below.
SELECT
*
FROM
table_name;
Using the asterisk (*) operator is only convenient for querying data interactively through an SQL client application. However, if you use the
asterisk (*) operator in embedded statements in your application, you may have some potential problems.
Best practice: specify exactly which columns you want to receive data in, in what order and with what column name.
For example
SELECT author_name as COVID_19_risk_author
FROM book_author
WHERE age > 60;
This command will yield the names of authors from the relation book_author whose age is greater than 60 (Author with COVID-19 Risk).
Each query begins with finding the data that we need in a database, and then filtering that data down into something that can be processed
and understood as quickly as possible. Because each part of the query is executed sequentially, it's important to understand the order of
execution so that you know what results are accessible where.
The SELECT statement is one of the most complex commands in SQL include many clauses:
• SELECT − This is one of the fundamental query command of SQL. It is similar to the projection operation of relational algebra. It selects
the attributes based on the condition described by WHERE clause.
• FROM − This clause takes a relation name as an argument from which attributes are to be selected/projected. In case more than one
relation names are given, this clause corresponds to Cartesian product.
• JOIN – for querying data from multiple related tables
• WHERE – This clause defines predicate or conditions for filtering data based on a specified condition.
• GROUP BY – for grouping data based on one or more columns
• HAVING – for filtering groups
• ORDER BY – for sorting the result set
• LIMIT – for limiting rows returned
You will learn about these clauses in the subsequent tutorials on Practice Works PW-01 and PW-02.
2. WHERE
Once we have the total working set of data, the first-pass WHERE constraints are applied to the individual rows, and rows that do not satisfy the constraint
are discarded. Each of the constraints can only access columns directly from the tables requested in the FROM clause. Aliases in the SELECT part of the
query are not accessible in most databases since they may include expressions dependent on parts of the query that have not yet executed.
3. GROUP BY
The remaining rows after the WHERE constraints are applied are then grouped based on common values in the column specified in the GROUP BY clause.
As a result of the grouping, there will only be as many rows as there are unique values in that column. Implicitly, this means that you should only need to
use this when you have aggregate functions in your query.
4. HAVING
If the query has a GROUP BY clause, then the constraints in the HAVING clause are then applied to the grouped rows, discard the grouped rows that don't
satisfy the constraint. Like the WHERE clause, aliases are also not accessible from this step in most databases.
5. SELECT
Any expressions in the SELECT part of the query are finally computed.
6. DISTINCT
Of the remaining rows, rows with duplicate values in the column marked as DISTINCT will be discarded.
7. ORDER BY
If an order is specified by the ORDER BY clause, the rows are then sorted by the specified data in either ascending or descending order. Since all the
expressions in the SELECT part of the query have been computed, you can reference aliases in this clause.
8. LIMIT / OFFSET. Finally, the rows that fall outside the range specified by the LIMIT and OFFSET are discarded, leaving the final set of rows to be returned
from the query.
CONCLUSION. Not every query needs to have all the parts we listed above, but a part of why SQL is so flexible is that it allows developers and data
analysts to quickly manipulate data without having to write additional code, all just by using the above clauses.
© Yuriy Shamshin 12/18
SQL Functions
Read topic MySQL Functions https://www.w3schools.com/sql/sql_ref_mysql.asp
SQL functions help simplify different types of operations on the data. SQL supports fife types of functions:
Function Description
CURRENT_USER Returns the user and host name for the MySQL account that the server used to authenticate the current client
IFNULL Return a specified value if the expression is NULL, otherwise return the expression
NULLIF Compares two expressions and returns NULL if they are equal. Otherwise, the first expression is returned
SESSION_USER Returns the current MySQL user name and host name