Experiment 1: Aim: Introduction To DDL and DML
Experiment 1: Aim: Introduction To DDL and DML
Experiment 1: Aim: Introduction To DDL and DML
1. Create:
- To make a new database, table, index, or stored procedure
CREATE TABLE statement
A commonly used CREATE command is the CREATE TABLE command. The typical
usage is:
CREATE TABLE [table name] ( [column definitions] ) [table parameters].
column definitions: A comma-separated list consisting of any of the following
Column definition: [column name] [data type] {NULL | NOT NULL} {column
options}
Primary key definition: PRIMARY KEY ( [comma separated column list] )
Constraints: {CONSTRAINT} [constraint definition]
RDBMS specific functionality
For example, the command to create a table named employees with a few sample
columns would be:
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
2. Alter
Alter - To modify an existing database object.
An ALTER statement in SQL changes the properties of an object inside of a
relational database management system (RDBMS). The types of objects that can
be altered depends on which RDBMS is being used. The typical usage is:
ALTER object type object name parameters.
For example, the command to add (then remove) a column named bubbles for an
existing table named sink would be:
P_Id
LastName
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
3. Drop
Drop - To destroy an existing database, table, index, or view.
For example, the command to drop a table named employees
would be: DROP TABLE employees;
The DROP statement is distinct from the DELETE and TRUNCATE
statements, in that DELETE and TRUNCATE do not remove the table
itself. For example, A DELETE statement might delete some (or all)
data from a table while leaving the table itself in database, whereas
DROP statement would remove entire table from database.
DML:
A data manipulation language (DML) is a family of syntax elements similar to a
computer Experimenting language used for inserting, deleting and updating data in
a database. Performing read-only queries of data is sometimes also considered a
component of DML.
1. Select:
Select clause is used to list the attributes desired in the result of a query. It
corresponds to the projection operation of the relational algebra.
Example: SELECT CustomerName,City FROM Customers;
2. Insert:
A newly created relation is empty initially. We can use the insert
command to load data into the relation.
insert into <table name> values(A1,A2,An)
The values are specified in the order in which the corresponding
attributes are listed in the relation schema.
3. Update:
In certain situations we may wish to change a value in a tuple without changing all
the values in the tuple. For this purpose, the update statement can be used.
Example: update
EMPLOYEE set
age=20
Where SSN=514065 ;
SQL provides a case construct which we can use to perform both the update with
a single update statement avoiding the problem with the order of updates.
Example : Update
account set
balance =case
when
balance<=1000
then balance*1.05
else balance*1.06
end ;
4. Delete:
To delete a tuple from relation r, we use the following command delete from r
where r is the name of the relation
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria
Anders';
Experiment 2
Aim: To create Table queries using the following constraints
Primary Key constraint
Foreign Key constraint
Check Constraint
Unique Constraint
Not null constraint
Theory:
1. Primary key
The PRIMARY KEY constraint uniquely identifies each record in a
database table. Primary keys must contain unique values.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE primary
key.
2. Foreign Key
A FOREIGN KEY in one table points to a PRIMARY KEY in another table.
3. Check:
The check clause in SQL can be applied to relation declarations as well to domain
declarations when applied to a relation declaration, the clause check (p) specified a
predicate p that must be specified by every tuple in a relation. A common use of
the check clause is to ensure that the attribute value satisfy specified condition.
4. Unique
The UNIQUE constraint uniquely identifies each record in a database table.
The UNIQUE and PRIMARY KEY constraints both provide a guarantee for
uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.
Note that you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
5) Not Null:
The not null specification prohibits the insertion of a null value.
For a attribute any database modification that would cause null to be inserted in an
attribute declared to be not null generates an error diagnostic. If an attribute is
declared as the primary key then it cannot take a null value.
Query:
Output
Table created.
Experiment 3
Aim: To perform the function of Insert statement in table.
Theory: The INSERT INTO statement is used to insert new
records in a table.
Query:
1) A new table is create to further insert records in the table.
CREATE TABLE CUSTOMERS(
ID INT
NOT NULL,
NOT NULL,
Experiment 4
Aim: To perform the function of update statement with and
without WHERE clause.
THEORY: The UPDATE statement is used to update existing records in
a table. Notice the WHERE clause in the SQL UPDATE statement!
The WHERE clause specifies which record or records that should be
updated. If you omit the WHERE clause, all records will be updated!
Update Statement:
In certain situations we may wish to change a value in a tuple without
changing all the values in the tuple. For this purpose, the update
statement can be used.
Example : UPDATE Customers
Query:
1) SELECT * FROM LOT ;
Experiment 5
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern ;
2) BETWEEN:
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2 ;
3) IN:
SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1,value2,...);
Query:
1)
BETWEEN:
2)
IN:
3)
LIKE:
Experiment 6
STATEMENTS:
1) GROUP BY:
2) ORDER BY:
SELECT column_name,column_name
FROM table_name
ORDER BY column_name,column_name ASC|DESC ;
Query:
1) GROUP BY:
SELECT * FROM VEHICLE
FROM VEHICLE
GROUP BY COLOR
2) ORDER BY:
SELECT * FROM VEHICLE
ORDER BY ID ASC
Experiment 7
Aim: To perform the function of DELETE statement with and
without WHERE clause.
Query:
1) SELECT * FROM CUSTOMERS
Note: Using table not from the Database.
Experiment 8
clause.
Query:
1) Adding a new Column:
ALTER TABLE P_SPACE
ADD VEHICLE_TYPE VARCHAR (10)
2) Dropping a Column:
3) Modify:
ALTER TABLE VEHICLE
ADD YEAR INT
4) Adding a Constraint:
ALTER TABLE VEHICLE
DROP PRIMARY KEY
Experiment 9
Aim: To perform the function of inbuilt SQL Functions like
SUM, MAX, MIN, AVG, COUNT, DISTINCT etc.
Query:
1) ALTER TABLE P_SPACE
ADD PARK_HOURS INT
SELECT * FROM P_SPACE
UPDATE P_SPACE
SET PARK_HOURS = 5 WHERE SPACE_NO = 34
UPDATE P_SPACE
SET PARK_HOURS = 6 WHERE SPACE_NO = 44
UPDATE P_SPACE
SET PARK_HOURS = 8 WHERE SPACE_NO = 54
UPDATE P_SPACE
SET PARK_HOURS = 4 WHERE SPACE_NO = 64
UPDATE P_SPACE SET PARK_HOURS = 5 WHERE SPACE_NO = 74
Experiment 10
P_SPACE
Query:
1) CREATE SEQUENCE DIV1
START WITH 25 INCREMENT BY 4
Experiment 11
Query:
1) SELECT * FROM P_SPACE
Dropping an Index :
DROP INDEX INDEX1
Experiment 12
Aim: To create and modify the View.
THEORY: 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.
Query:
1) CREATE VIEW DIVY AS
SELECT SPACE_NO, DEPT
FROM P_SPACE
WHERE DEPT= 'IT'
2) SELECT * FROM DIVY