Advance Lab2
Advance Lab2
Advance Lab2
Constraint
SQL Constraints are rules used to limit the type of data that can go into a table, to maintain the
accuracy and integrity of the data inside table.
Constraints can be divided into the following two types,
Constraints are used to make sure that the integrity of data is maintained in the database.
Following are the most used constraints that can be applied to a table.
NOT NULL
UNIQUE
PRIMARY KEY
FOREIGN KEY
CHECK
DEFAULT
UNIQUE Constraint
The above query will declare that the s_id field of Student table will only have unique
values and wont take NULL value.
The above query specifies that s_id field of Student table will only have unique value.
Order_Detail Table
10 Order1 101
11 Order2 103
12 Order3 102
In Customer_Detail table, c_id is the primary key which is set as foreign key
in Order_Detail table. The value that is entered in c_id which is set as foreign key
in Order_Detail table must be present in Customer_Detail table where it is set as
primary key. This prevents invalid data to be inserted into c_id column
of Order_Detail table.
If you try to insert any incorrect data, DBMS will return error and will not allow you to
insert the data.
);
In this query, c_id in table Order_Detail is made as foriegn key, which is a reference
of c_id column in Customer_Detail table.
1. On Delete Cascade : This will remove the record from child table, if that value of
foriegn key is deleted from the main table.
2. On Delete Null : This will set all the values in that record of child table as NULL,
for which the value of foriegn key is deleted from the main table.
3. If we don't use any of the above, then we cannot delete data from the main table
for which data in child table exists. We will get an error if we try to do so.
CHECK Constraint
Age int
);
The above query will restrict the s_id value to be greater than zero.
Syntax
The basic syntax of a UNION clause is as follows −
SELECT column1 [, column2 ]
FROM table1 [, table2 ]
[WHERE condition]
UNION
Example
Consider the following two tables.
Table 1 − CUSTOMERS Table is as follows.
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Table 2 − ORDERS Table is as follows.
+-----+---------------------+-------------+--------+
|OID | DATE | CUSTOMER_ID | AMOUNT |
+-----+---------------------+-------------+--------+
| 102 | 2009-10-08 00:00:00 | 3 | 3000 |
| 100 | 2009-10-08 00:00:00 | 3 | 1500 |
| 101 | 2009-11-20 00:00:00 | 2 | 1560 |
UNION ALL
Syntax
The basic syntax of NULL while creating a table.
SQL> CREATE TABLE CUSTOMERS(
ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR (25) ,
SALARY DECIMAL (18, 2),
PRIMARY KEY (ID)
);
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 the one that has been left blank during the 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 results. You must use the IS NULL or IS NOT NULL operators to check
for a NULL value.
Consider the following CUSTOMERS table having the records as shown below.
10 | P a g e Advance database lab pre by jilo D
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | bikila | 32 | naqamtee | 2000.00 |
| 2 | caala | 25 | gudar | 1500.00 |
| 3 | hacaalu | 23 | amboo | 2000.00 |
| 4 | Caltuu | 25 | gincii | 6500.00 |
| 5 | bontuu | 27 | holoota | 8500.00 |
| 6 | akmal | 22 | finfine | |
| 7 | guyo | 24 | moyale | |
+----+----------+-----+-----------+----------+
Now, following is the usage of the IS NOT NULLoperator.
SQL> SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM CUSTOMERS
WHERE SALARY IS NOT NULL;
This would produce the following result −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | bikila | 32 | naqamtee | 2000.00 |
| 2 | caala | 25 | gudar | 1500.00 |
| 3 | hacaalu | 23 | ambo | 2000.00 |
| 4 | Caltuu | 25 | gincii | 6500.00 |
| 5 | bontuu | 27 | holoota | 8500.00 |
+----+----------+-----+-----------+----------+
Now, following is the usage of the IS NULL operator.
SQL> SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM CUSTOMERS
WHERE SALARY IS NULL;
This would produce the following result −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 6 | akmal | 22 | finfine | |
| 7 | guyo | 24 | moyale | |
SQL - Alias Syntax
You can rename a table or a column temporarily by giving another name known as Alias.
The use of table aliases is to rename a table in a specific 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.
Example
Consider the CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | bikilq | 32 | naqamte | 2000.00 |
| 2 | caala | 25 | gudar | 1500.00 |
| 3 | hacaalu | 23 | ambo | 2000.00 |
| 4 | Caltuu | 25 | gincii | 6500.00 |
| 5 | bontu | 27 | holota | 8500.00 |
| 6 | akmal | 22 | finfine | 4500.00 |
| 7 | guyo | 24 | moyale | 10000.00 |
+----+----------+-----+-----------+----------+
Following is the example to ADD a New Column to an existing table −
ALTER TABLE CUSTOMERS ADD SEX char(1);
Now, the CUSTOMERS table is changed and following would be output from the
SELECT statement.
+----+---------+-----+-----------+----------+------+
| ID | NAME | AGE | ADDRESS | SALARY | SEX |
+----+---------+-----+-----------+----------+------+
| 1 | bikila | 32 | naqmte | 2000.00 | NULL |
| 2 | Ramesh | 25 | gudar | 1500.00 | NULL |
| 3 | kaushik | 23 | ambo | 2000.00 | NULL |
| 4 | kaushik | 25 | gincii | 6500.00 | NULL |
| 5 | Hardik | 27 | holota | 8500.00 | NULL |
| 6 | Komal | 22 | finfine | 4500.00 | NULL |
| 7 | Muffy | 24 | moyale | 10000.00 | NULL |
+----+---------+-----+-----------+----------+------+
Following is the example to DROP sex column from the existing table.
ALTER TABLE CUSTOMERS DROP SEX;
Now, the CUSTOMERS table is changed and following would be the output from the
SELECT statement.
Syntax
The basic syntax of a TRUNCATE TABLE command is as follows.
TRUNCATE TABLE table_name;
Example
Consider a CUSTOMERS table having the following records −
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | bikilq | 32 | naqamte | 2000.00 |
| 2 | caala | 25 | gudar | 1500.00 |
| 3 | hacaalu | 23 | ambo | 2000.00 |
| 4 | Caltuu | 25 | gincii | 6500.00 |
| 5 | bontu | 27 | holota | 8500.00 |
| 6 | akmal | 22 | finfine | 4500.00 |
| 7 | guyo | 24 | moyale | 10000.00 |
+----+----------+-----+-----------+----------+
Following is the example of a Truncate command.
SQL > TRUNCATE TABLE CUSTOMERS;
Now, the CUSTOMERS table is truncated and the output from SELECT statement will be as
shown in the code block below −
SQL> SELECT * FROM CUSTOMERS;
Empty set (0.00 sec)