0% found this document useful (0 votes)
167 views14 pages

Ex - No-1 Data Definition, Table Creation, Constraints

The document discusses various data definition language (DDL) commands and constraints in SQL. It provides examples of using CREATE TABLE to create a table with columns and data types, ALTER TABLE to add, drop and modify columns, DROP TABLE to remove a table, and TRUNCATE TABLE to delete all rows from a table. It also describes different types of constraints like NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK that can be applied to columns to restrict the values. Examples are given of creating tables with these different constraints. The document also contains examples of SQL queries for DDL commands and constraints.

Uploaded by

Gowrisankar JG
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
167 views14 pages

Ex - No-1 Data Definition, Table Creation, Constraints

The document discusses various data definition language (DDL) commands and constraints in SQL. It provides examples of using CREATE TABLE to create a table with columns and data types, ALTER TABLE to add, drop and modify columns, DROP TABLE to remove a table, and TRUNCATE TABLE to delete all rows from a table. It also describes different types of constraints like NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, and CHECK that can be applied to columns to restrict the values. Examples are given of creating tables with these different constraints. The document also contains examples of SQL queries for DDL commands and constraints.

Uploaded by

Gowrisankar JG
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 14

Ex.

No-1

Data Definition, Table Creation, Constraints

Data Definition language:


Data Definition Language (DDL): statements which are used to define the database
structure or schema.
Examples: CREATE, ALTER, DROP statements
DDL COMMANDS
CREATE TABLE
This statement is used to create a table. The syntax for this command is
create table tablename (colname1 datatype [constraint], colname2 datatype [constraint]);
ALTER TABLE
This command is used to add, drop columns in a table. The syntax for this command is
alter table tablename add colname1 datatype [constraint];
alter table tablename drop column colname1;
DROP TABLE
The syntax for this command isdrop table tablename;
TRUNCATE TABLE
TRUNCATE removes all rows from a table. The operation cannot be rolled back and no
triggers will be fired. As such, TRUCATE is faster and doesn't use as much undo space as
a DELETE.
The syntax for TRUNCATE TABLE is
TRUNCATE TABLE "table_name"
RENAME TABLE
* All indexes, columns, default columns, constraints, referential authorization, rules,
and user authorities tables dependent on a renamed table will be renamed.
* RENAME statement is not allowed for IMAGE/SQL tables.
* When using RENAME command, data and grants made for tables are carried
forward for the new name. No unload, load data, or recreating index is necessary.
* All views dependent on a renamed table will be dropped.
* If a table has check constraints, then that table cannot be renamed.
RENAME TABLE dbname.user to users;

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));

Write SQL queries for DDL commands and Constraints

AIM:

To write the SQL queries for DDL commands and constraints.

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

------------------------------- -------- ---EMP_ID

NUMBER(5)

EMP_NAME
LOCATION
GENDER

VARCHAR2(20)
VARCHAR2(20)
VARCHAR2(10)

Modify the column data type:


alter table emp modify (emp_id varchar(5));
Table altered.

Modify the column size:


alter table emp modify(emp_id varchar(10));
Table altered.
Drop the column from table:
alter table emp drop column gender;
Table altered.

Rename the table:


rename emp to employee;
Table renamed.

Truncate the table:


truncate table employee;
Table truncated.

Drop the Table:


drop table employee;
Table dropped.
Not Null Constrint :
Create table with NOT NULL constraint
create table stu1(stu_id number(10),stu_name varchar(20) NOT NULL,phone_no
number(15));
Table created.
insert into stu1(stu_id,stu_name,phone_no)values(1,'Raja',9898989898);
1 row created.
insert into stu1(stu_id,stu_name,phone_no)values(2,'Venkat',NULL);
1 row created.
insert into stu1(stu_id,stu_name,phone_no)values(1,NULL,99945);
Error: ORA-01400: cannot insert NULL into ("SYSTEM"."STU1"."STU_NAME")

Primary key Constrint:


create table staff(staff_id number(5) primary key,staff_name varchar(10),gender
varchar(5));
Table created.
insert into staff(staff_id,staff_name,gender)values(1,'suresh','male');
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C00813) violated
insert into staff(staff_id,staff_name,gender)values(1,'suresh','male');
Error: ORA-00001: unique constraint (SYSTEM.SYS_C004026) violated
insert into staff(staff_id,staff_name,gender)values(2,'suresh','male');
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C00813) violated
insert into staff(staff_id,staff_name,gender)values(NULL,'suresh','male');
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."STAFF"."STAFF_ID")
Unique Constrint:
create table stu(stu_id number(10),stu_name varchar(20) NOT NULL,phone_no
number(15),email varchar(15) UNIQUE);
Table created.
insert into stu(stu_id,stu_name,phone_no,email)values(1,'raja',NULL,'sur_ra@gmail');
1 row created.
insert into stu(stu_id,stu_name,phone_no,email)values(2,'ravi',NULL,'sur_ra@gmail');
Error:ORA-00001: unique constraint (SYSTEM.SYS_C004025) violated
insert into stu(stu_id,stu_name,phone_no,email)values(2,'mano',NULL,NULL);
1 row created.
insert into stu(stu_id,stu_name,phone_no,email)values(3,'venkat',NULL,NULL);
1 row created.
Check Constrint:

create table empsal(emp_id varchar(10),emp_name varchar(20),salary


number(15)check(salary>0));
Table created.

Result:
The SQL queries for DDL commands and constraints are executed in oracle server
and the results are verified.

Ex.No-2

Insert, Select Commands, Update and Delete


Commands.

DML - stands for Data Manipulation Language, its the part of SQL that deals with
querying, updating, deleting and inseting records in tables, views.

The following types of actions may be performed using DML commands:

SELECT - extracts data from a database

UPDATE - updates data in a database

DELETE - deletes data from a database

INSERT INTO - inserts new data into a database

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

SQL SELECT Syntax


SELECT column_name(s)
FROM table_name
And
SELECT * FROM table_name
2) Insert - This command is used to add record(s) to a table. While inserting a record
using the insert statement, the number of records being entered should match the columns
of the table.
The first form doesn't specify the column names where the data will be inserted, only
their values:
INSERT INTO table_name
VALUES (value1, value2, value3,...)
The second form specifies both the column names and the values to be inserted:
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
3) Update - This command is used to edit the record(s) of a table. It may be used to
update a single row based on a condition, all rows, or a set of rows based on a condition.
It is used along with the set clause. Optionally, a where clause may be used to match
conditions.

SQL UPDATE Syntax


UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies
which record or records that should be updated. If you omit the WHERE clause, all
records will be updated!

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.

SQL DELETE Syntax

DELETE FROM table_name


WHERE some_column=some_value
Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies
which record or records that should be deleted. If you omit the WHERE clause, all
records will be deleted

Result:
The SQL queries for insert, select, update and delete commands are executed in
oracle server and the results are verified.

Ex-no-3

Nested Queries and Join Queries

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.

Different SQL JOINs


Before we continue with examples, we will list the types of JOIN you can use, and the
differences between them.

JOIN:

Return rows when there is at least one match in both tables

LEFT JOIN:

RIGHT JOIN: Return

FULL JOIN:

right table

Return all rows from the left table, even if there are no matches in the

the left table

all rows from the right table, even if there are no matches in

Return rows when there is a match in one of the tables

SQL INNER JOIN

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

SQL LEFT JOIN


The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there
are no matches in the right table (table_name2).

SQL LEFT JOIN Syntax


SELECT column_name(s)
FROM table_name1
LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

SQL RIGHT JOIN


The RIGHT JOIN keyword Return all rows from the right table (table_name2), even if
there are no matches in the left table (table_name1).

SQL RIGHT JOIN Syntax


SELECT column_name(s)
FROM table_name1
RIGHT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

SQL FULL JOIN


The FULL JOIN keyword return rows when there is a match in one of the tables.

SQL FULL JOIN Syntax


SELECT column_name(s)
FROM table_name1
FULL JOIN table_name2
ON table_name1.column_name=table_name2.column_name
Write Nested queries and joined queries for oracle server

AIM

To study about Nested queries and join queries

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

To find Min salary in the table:


Select min(salary) from employee_salary;
MIN(SALARY)
----------5000
To find the employee details who get lowest salary in the company?
select * from employee_salary where salary=(select min(salary) from employee_salary);
EMP_NAME DEPT
---------- ---------- --------Jay

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

---------- ---------- --------Hari

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

SQL CREATE VIEW Statement


In SQL, a view is a virtual table based on the result-set of an SQL statement.
A view contains rows and columns, just like a real table. The fields in a view are fields
from one or more real tables in the database.
You can add SQL functions, WHERE, and JOIN statements to a view and present the
data as if the data were coming from one single table.

SQL CREATE VIEW Syntax


CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

SQL Updating a View


You can update a view by using the following syntax:

SQL CREATE OR REPLACE VIEW Syntax


CREATE OR REPLACE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

SQL Dropping a View


You can delete a view with the DROP VIEW command.

SQL DROP VIEW Syntax


DROP VIEW view_name

Write views for DDL commands in SQL queries

You might also like