Ex - No-1 Data Definition, Table Creation, Constraints
Ex - No-1 Data Definition, Table Creation, Constraints
No-1
SQL Constraints
Types of SQL Constraints
- NOT NULL
- UNIQUE
- CHECK
- Primary Key
- Foreign Key
Each type of SQL Constraint is described in below with example.
SQL NOT NULL Constraint.
By default, a column can hold NULL value. If we not want to allow NULL or empty
value in a column of our table, then we need to place an SQL Constraint on this column
specifying that NULL empty column is now not an allowable value.
For example, in the following statement,
Example of NOT NULL Constraint
CREATE TABLE Employees
(empID integer NOT NULL,
FirstName varchar (30) NOT NULL,
LastName varchar(30));
Columns "empID" and "FirstName" cannot include NULL, while "LastName" can
include NULL, while inserting the record in this table we must provide empID and
FirstName after this SQL NOT NULL Constraints apply.
SQL UNIQUE Constraint.
The UNIQUE SQL constraint ensures that all values in a column are distinct and unique
values repetition will not allow after applying SQL UNIQUE Constraint.
For example, the following statement explains real example of SQL UNIQUE
Constraints.
Example of SQL UNIQUE Constraint
CREATE TABLE Employees
(empID integer UNIQUE,
FirstName varchar (30),
LastName varchar(30));
Column "empID" cannot include duplicate values, while such constraint does not hold for
columns "FirstName" and "LastName" so these last two can hold duplication.
Please note that a column that is specified as a primary key must also be unique. At the
same time, a column that's unique may or may not be a primary key.
SQL CHECK Constraint.
The SQL CHECK constraint ensures that all values in a column satisfy certain
conditions, suppose we are wishing to enter some conditional record like age of employ
not less then 20 so we need to verify it with SQL CHECK Constraint.
For example, in the following statement explains how to use the SQL CHECK Constraint
in our queries.
Example of SQL CHECK Constraint
CREATE TABLE Employees
(empID integer CHECK (empID > 10),
FirstName varchar (30),
LastName varchar(30));
AIM:
SQL QUERIES:
Create table:
create table emp(emp_id number(5),emp_name varchar(20),location varchar(20));
Table created.
Alter table:
alter table emp add (gender varchar(10));
Table altered.
Descripe the structure of table
desc emp;
Name
Null? Type
NUMBER(5)
EMP_NAME
LOCATION
GENDER
VARCHAR2(20)
VARCHAR2(20)
VARCHAR2(10)
Result:
The SQL queries for DDL commands and constraints are executed in oracle server
and the results are verified.
Ex.No-2
DML - stands for Data Manipulation Language, its the part of SQL that deals with
querying, updating, deleting and inseting records in tables, views.
1) Select - This command is used to fetch a result set of records from a table, view or a
group of tables, views by making use of SQL joins.
Retrieval of data using SQL statements can be done by using different predicates along
with it like
Where
Group By
Having
Order By
4) Delete - This command is used to remove record(s) from a table. All records may be
removed in one go, or a set of records may be deleted based on a condition.
Result:
The SQL queries for insert, select, update and delete commands are executed in
oracle server and the results are verified.
Ex-no-3
SQL Subquery
Subquery or Inner query or Nested query is a query in a query. A subquery is usually
added in the WHERE Clause of the sql statement. Most of the time, a subquery is used
when you know how to search for a value using a SELECT statement, but do not know
the exact value.
Subqueries are an alternate way of returning data from multiple tables.
Subqueries can be used with the following sql statements along with the comparision
operators like =, <, >, >=, <= etc.
SELECT
INSERT
UPDATE
DELETE
SQL JOIN
The JOIN keyword is used in an SQL statement to query data from two or more tables,
based on a relationship between certain columns in these tables.
Tables in a database are often related to each other with keys.
A primary key is a column (or a combination of columns) with a unique value for each
row. Each primary key value must be unique within the table. The purpose is to bind data
together, across tables, without repeating all of the data in every table.
JOIN:
LEFT JOIN:
FULL JOIN:
right table
Return all rows from the left table, even if there are no matches in the
all rows from the right table, even if there are no matches in
The INNER JOIN keyword return rows when there is at least one match in both
tables.
SQL INNER JOIN Syntax
SELECT column_name(s)
FROM table_name1
INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name
AIM
SQL QUERIES:
Main Query
Which employees have salaries greater than Oms salary?
Subquery:
What is Oms salary?
Syntax
SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM
table);
Tables used:
1.employee
2.employee_salary
Create table 1:
create table employee(emp_name varchar(20),city varchar(20));
Table created.
Insert values:
insert into employee(emp_name,city)values('Hari','Pune');
1 row created.
Create table 2:
create table employee_salary(emp_name varchar(10),dept varchar(10),salary
number(10));
Table created.
Insert values:
insert into employee_salary(emp_name,dept,salary)values('Hari','Computer',10000);
1 row created.
To find the employee names who have get the salary higher than Oms Salary?
select emp_name,salary from employee_salary where salary>(select salary from
employee_salary where emp_name='Om');
EMP_NAME
SALARY
---------- --------Hari
10000
Billi
8000
IT
5000
SALARY
To find the employee details who get Highest salary in the company?
select * from employee_salary where salary=(select max(salary) from employee_salary);
EMP_NAME DEPT
SALARY
Computer
10000
Join queries:
Inner Join:
select employee.emp_name,employee_salary.salary from employee innerjoin
employee_salary on employee.emp_name=employee_salary.emp_name;
select * from employee innerjoin employee_salary on
employee.emp_name=employee_salary.emp_name;
Full Outer join:
select * from employee full outer join employee_salary on
employee.emp_name=employee_salary.emp_name;
Left outer join
select * from employee leftouterjoin employee_salary on
employee.emp_name=employee_salary.emp_name;
Right outer join:
select * from employee right outer join employee_salary on
employee.emp_name=employee_salary.emp_name;
Result:
The SQL nested queries and joined queries are executed in oracle server and the
results are verified.
Ex-no-4
View
View
A view is a virtual table that consists of columns from one or more tables. Though it is
similar to a table, it is stored in the database. It is a query stored as an object. Hence, a
view is an object that derives its data from one or more tables. These tables are referred to
as base or