Data Base Class SQL
Data Base Class SQL
What is database?
What is RDBMS?
RDBMS stands for Relational Database Management System. RDBMS data is structured in
database tables, fields and records. Each RDBMS table consists of database table rows. Each
database table row consists of one or more database table fields.
RDBMS store the data into collection of tables, which might be related by common fields
(database table columns). RDBMS also provide relational operators to manipulate the data
stored into the database tables. Most RDBMS use SQL as database query language.
2. SQL
2.1 DDL
Data Definition Language (DDL) statements are used to define the database structure or
schema. Some examples:
2.2 DML
Data Manipulation Language (DML) statements are used for managing data within
schema objects. Some examples:
SELECT column_name,column_name
FROM table_name
SELECT *
FROM table_name
5. DELETE - deletes all records from a table, the space for the recordsremain
6. WHERE Clause - The WHERE clause is used to extract only those records
that fulfill a specified criterion.
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
7. AND Operator - The following SQL statement selects all customers from the
country "Germany" AND the city "Berlin", in the "Customers" table:
8. OR Operator - The following SQL statement selects all customers from the
city "Berlin" OR "München", in the "Customers" table:
You can also combine AND and OR (use parenthesis to form complex expressions).
The following SQL statement selects all customers from the country "Germany" AND the city
must be equal to "Berlin" OR "München", in the "Customers" table:
SELECT * FROM Customers
WHERE Country='Germany
AND (City='Berlin' OR City='München')
10.ORDER BY Keyword
The ORDER BY keyword is used to sort the result-set by one or more columns.
The ORDER BY keyword sorts the records in ascending order by default. To sort the records
in a descending order, you can use the DESC keyword.
11.LIKE Operator
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
12.Wildcard Characters
In SQL, wildcard characters are used with the SQL LIKE operator.
SQL wildcards are used to search for data within a table.
With SQL, the wildcards are:
Wildcard Description
% A substitute for zero or more characters
Example 1
The following SQL statement selects all customers with a City starting with "ber":
SELECT * FROM Customers
WHERE City LIKE 'ber%';
Example 2
The following SQL statement selects all customers with a City containing the pattern "es":
Example 3
The following SQL statement selects all customers with a City starting with any character,
followed by "erlin":
Example 4
The following SQL statement selects all customers with a City starting with "L", followed by
any character, followed by "n", followed by any character, followed by "on":
Example 5
The following SQL statement selects all customers with a City starting with "b", "s", or "p":
Example 6
The following SQL statement selects all customers with a City starting with "a", "b", or "c":
Example 7
The following SQL statement selects all customers with a City NOT starting with "b", "s", or
"p":
SELECT * FROM Customers
WHERE City LIKE '[!bsp]%';
13.SQL Aliases
SQL aliases are used to give a database table, or a column in a table, a temporary name.
SELECT column_name(s)
FROM table_name AS alias_name;
14.BETWEEN Operator
The BETWEEN operator selects values within a range. The values can be numbers, text, or
dates.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;
2.3 DCL
2.4 TCL
Transaction Control (TCL) statements are used to manage the changes made by DML
statements. It allows statements to be grouped together into logical transactions.
1. COMMIT : Save work done
2. SAVEPOINT : Identify a point in a transaction to which you can later roll back
3. ROLLBACK : Restore database to original since the last COMMIT
4. SET TRANSACTION: Change transaction options like isolation level and what rollback
segment to use
The PRIMARY KEY constraint uniquely identifies each record in a database table.
Each table should have a primary key, and each table can have only ONE primary key.
Let's illustrate the foreign key with an example. Look at the following two tables:
1 77895 3
2 44678 3
3 22456 2
4 24562 1
SQL JOINS
SQL joins are used to combine rows from two or more tables.
1. INNER JOIN
The INNER JOIN keyword selects all rows from both tables as long as there is a match
between the columns in both tables.
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching rows
in the right table (table2). The result is NULL in the right side when there is no match.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
LEFT OUTER JOIN table2
ON table1.column_name=table2.column_name;
PS! In some databases LEFT JOIN is called LEFT OUTER JOIN.
3. RIGHT JOIN Keyword
The RIGHT JOIN keyword returns all rows from the right table (table2), with the matching
rows in the left table (table1). The result is NULL in the left side when there is no match.
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
or:
SELECT column_name(s)
FROM table1
RIGHT OUTER JOIN table2
ON table1.column_name=table2.column_name;
PS! In some databases RIGHT JOIN is called RIGHT OUTER JOIN.
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right
table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;