SQL Data Manipulation Language (DML) : Eng. Ibraheem Lubbad
SQL Data Manipulation Language (DML) : Eng. Ibraheem Lubbad
SQL Data Manipulation Language (DML) : Eng. Ibraheem Lubbad
Faculty of Engineering
Computer Engineering Department
Database Lab (ECOM 4113)
Lab 2
SQL
Data Manipulation Language
(DML)
Data Definition Language (DDL) - These SQL commands are used for
creating, modifying, and dropping the structure of database objects.
The commands are ALTER, ANALYZE, AUDIT, COMMENT, CREATE, DROP,
FLASHBACK, GRANT, PURGE, RENAME, REVOKE and TRUNCATE.
Data Control Language (DCL) - These SQL commands are used for
providing security to database objects. These commands are GRANT and
REVOKE.
In our lab we will use university schema (you can open it by click on file)
SELECT Statement:
The SELECT statement retrieves data from a database. The data is returned in a
table-like structure called a result-set. SELECT is the most frequently used action
on a database. To create a simple SQL SELECT Statement, you must specify the
column(s) name and the table name.
-table-name : is the name of the table from which the information is retrieved.
-column_list : includes one or more columns from which data is retrieved.
-The code within the brackets is optional.
Arithmetic Expressions:
Arithmetic operators can perform arithmetical operations on numeric
operands involved. Arithmetic operators are addition (+), subtraction (-),
multiplication (*) and division (/). The + and - operators can also be used in
date arithmetic
You cannot use equal operator (=) to compare two null values! Instead, use (IS)
with special keyword null to check if a value is null or not.
Example: retrieve all student who his grade has not been awarded
Use * To Select All Column
SELECT ID, COURSE_ID,GRADE
FROM TAKES
WHERE GRADE IS NULL;
A Concatenation Operator
Links columns or character strings to other columns
Is represented by two vertical bars (|| )
Creates a resultant column that is a character expression
Example: Find the names of all instructors in the Computer Science department
Syntax of SQL INSERT Statement
SELECT NAME
FROM INSTRUCTOR
WHERE DEPT_NAME = 'COMP. SCI.';
Notes:
When you deal with character strings or date values, you must enclosed
them by single quotation marks ( ‘ ’ )
Character values are case-sensitive, and date values are format-sensitive.
Example: Find the names of all student whose name with second and third
character “an”
Comparison operators:
Operator Operator Description
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
BETWEEN ... AND... Between two values (inclusive)
IN(set) Match any of a list of values
ANY(set) Compare to any value in a set
ALL(set) Compare to all values in a set
[NOT] LIKE Match a character pattern
IS[NOT] NULL Is a null value
Rules of Precedence:
Operator Priority
Parentheses () 1
Arithmetic operators /,* 2
+,- 3
Concatenation operator || 4
Comparison conditions <,> ,>=, <=,<> 5
IS [NOT] NULL, LIKE, [NOT] IN 6
[NOT] BETWEEN 7
Not equal to 8
NOT logical condition 9
AND logical condition 10
OR logical condition 11
Note: <> all is identical to not in, whereas = all is not the same as in
Example: find the names of instructors with salary amounts between $90,000 and
$100,000.
Instead of:
SELECT NAME
FROM INSTRUCTOR
WHERE SALARY <= 100000 AND SALARY >= 90000;
Example: Find all the names of instructors whose names are neither “Mozart” nor
“Einstein”.
By default, “ORDER BY” Clause sorts the retrieved rows in ascending order. To
reverse the ordering, use “DESC” keyword after column-name.
You can also insert new rows without specifying column names, by typing:
“INSERT INTO” table-name VALUES (value-list)”. In this case, you MUST order
values in the same order of its corresponding columns.
Q) Create a section of ‘Database lab CS-348’ course in fall 2016, with sec id of 1
and class room al Quds 218.
DELETE Statement
DELETE statement is used to delete rows from a table according to a specified
condition.
Example:
Using DELETE Statement
DELETE FROM CLASSROOM
WHERE BUILDING='AL QUDS'
Note:
It also can be used to delete all rows from a table by not specifying any
conditions. If you want to empty a table, you just have to issue this command:
“DELETE FROM table-name”. You should be aware when using this form.
Q) Delete enrollments in the above section where the student’s name is contain
“han” substring
Example:
END