SQL 2
SQL 2
Constraints are the rules enforced on data columns on table. These are used to limit the
type of data that can go into a table. This ensures the accuracy and reliability of the data
in the database.
Constraints could be column level or table level. Column level constraints are applied
only to one column, whereas table level constraints are applied to the whole table.
Following are commonly used constraints available in SQL. These constraints have
already been discussed in SQL - RDBMS Conceptschapter but its worth to revise them
at this point.
NOT NULL Constraint: Ensures that a column cannot have NULL value.
CHECK Constraint: The CHECK constraint ensures that all values in a column
satisfy certain conditions.
INDEX: Use to create and retrieve data from the database very quickly.
Constraints can be specified when a table is created with the CREATE TABLE
statement or you can use ALTER TABLE statement to create constraints even after the
table is created.
Dropping Constraints:
Any constraint that you have defined can be dropped using the ALTER TABLE
command with the DROP CONSTRAINT option.
For example, to drop the primary key constraint in the EMPLOYEES table, you can use
the following command:
Integrity Constraints:
Integrity constraints are used to ensure accuracy and consistency of data in a relational
database. Data integrity is handled in a relational database through the concept of
referential integrity.
There are many types of integrity constraints that play a role in referential integrity (RI).
These constraints include Primary Key, Foreign Key, Unique Constraints and other
constraints mentioned above.
The SQL Joins clause is used to combine records from two or more tables in a
database. A JOIN is a means for combining fields from two tables by using values
common to each.
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
+-----+---------------------+-------------+--------+
Now, let us join these two tables in our SELECT statement as follows:
+----+----------+-----+--------+
+----+----------+-----+--------+
| 3 | kaushik | 23 | 3000 |
| 3 | kaushik | 23 | 1500 |
| 2 | Khilan | 25 | 1560 |
| 4 | Chaitali | 25 | 2060 |
+----+----------+-----+--------+
Here, it is noticeable that the join is performed in the WHERE clause. Several operators
can be used to join tables, such as =, <, >, <>, <=, >=, !=, BETWEEN, LIKE, and NOT;
they can all be used to join tables. However, the most common operator is the equal
symbol.
RIGHT JOIN: returns all rows from the right table, even if there are no matches in
the left table.
FULL JOIN: returns rows when there is a match in one of the tables.
SELF JOIN: is used to join a table to itself as if the table were two tables,
temporarily renaming at least one table in the SQL statement.
CARTESIAN JOIN: returns the Cartesian product of the sets of records from the
two or more joined tables.
The SQL UNION clause/operator is used to combine the results of two or more SELECT
statements without returning any duplicate rows.
To use UNION, each SELECT must have the same number of columns selected, the
same number of column expressions, the same data type, and have them in the same
order, but they do not have to be the same length.
Syntax:
The basic syntax of UNION is as follows:
[WHERE condition]
UNION
[WHERE condition]
Here given condition could be any given expression based on your requirement.
Example:
Consider the following two tables, (a) CUSTOMERS table is as follows:
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
+-----+---------------------+-------------+--------+
+-----+---------------------+-------------+--------+
+-----+---------------------+-------------+--------+
Now, let us join these two tables in our SELECT statement as follows:
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
UNION
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
+------+----------+--------+---------------------+
| ID | NAME | AMOUNT | DATE |
+------+----------+--------+---------------------+
+------+----------+--------+---------------------+
The same rules that apply to UNION apply to the UNION ALL operator.
Syntax:
The basic syntax of UNION ALL is as follows:
[WHERE condition]
UNION ALL
[WHERE condition]
Here given condition could be any given expression based on your requirement.
Example:
Consider the following two tables, (a) CUSTOMERS table is as follows:
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
+-----+---------------------+-------------+--------+
+-----+---------------------+-------------+--------+
+-----+---------------------+-------------+--------+
Now, let us join these two tables in our SELECT statement as follows:
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID
UNION ALL
FROM CUSTOMERS
ON CUSTOMERS.ID = ORDERS.CUSTOMER_ID;
+------+----------+--------+---------------------+
+------+----------+--------+---------------------+
| 1 | Ramesh | NULL | NULL |
+------+----------+--------+---------------------+
There are two other clauses (i.e., operators), which are very similar to UNION clause:
SQL EXCEPT Clause : combines two SELECT statements and returns rows
from the first SELECT statement that are not returned by the second SELECT
statement.
The SQL NULL is the term used to represent a missing value. A NULL value in a table is
a value in a field that appears to be blank.
A field with a NULL value is a field with no value. It is very important to understand that a
NULL value is different than a zero value or a field that contains spaces.
Syntax:
The basic syntax of NULL while creating a table:
);
Here, NOT NULL signifies that column should always accept an explicit value of the
given data type. There are two columns where we did not use NOT NULL, which means
these columns could be NULL.
A field with a NULL value is one that has been left blank during record creation.
Example:
The NULL value can cause problems when selecting data, however, because when
comparing an unknown value to any other value, the result is always unknown and not
included in the final results.
You must use the IS NULL or IS NOT NULL operators in order to check for a NULL
value.
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | |
| 7 | Muffy | 24 | Indore | |
+----+----------+-----+-----------+----------+
FROM CUSTOMERS
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
FROM CUSTOMERS
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | |
| 7 | Muffy | 24 | Indore | |
+----+----------+-----+-----------+----------+
You can rename a table or a column temporarily by giving another name known as alias.
The use of table aliases means to rename a table in a particular SQL statement. The
renaming is a temporary change and the actual table name does not change in the
database.
The column aliases are used to rename a table's columns for the purpose of a particular
SQL query.
Syntax:
The basic syntax of table alias is as follows:
WHERE [condition];
FROM table_name
WHERE [condition];
Example:
Consider the following two tables, (a) CUSTOMERS table is as follows:
+----+----------+-----+-----------+----------+
+----+----------+-----+-----------+----------+
| 6 | Komal | 22 | MP | 4500.00 |
+----+----------+-----+-----------+----------+
+-----+---------------------+-------------+--------+
+-----+---------------------+-------------+--------+
+-----+---------------------+-------------+--------+
Now, following is the usage of table alias:
+----+----------+-----+--------+
+----+----------+-----+--------+
| 3 | kaushik | 23 | 3000 |
| 3 | kaushik | 23 | 1500 |
| 2 | Khilan | 25 | 1560 |
| 4 | Chaitali | 25 | 2060 |
+----+----------+-----+--------+
FROM CUSTOMERS
+-------------+---------------+
| CUSTOMER_ID | CUSTOMER_NAME |
+-------------+---------------+
| 1 | Ramesh |
| 2 | Khilan |
| 3 | kaushik |
| 4 | Chaitali |
| 5 | Hardik |
| 6 | Komal |
| 7 | Muffy |
+-------------+---------------+
SQL - ALTER TABLE Command