Вы находитесь на странице: 1из 65

Experiment 1

Aim: Introduction to DDL and DML


THEORY:
DBMS
A DBMS (Database Management System) is a software Experiment used to manage a database.
These Experiments enable users to access and modify database
A DBMS is a complex set of software Experiments that controls the organization, storage,
management, and retrieval of data in a database.
DDL:
A data definition language or data description language (DDL) is a syntax similar to a computer
Experimentming language for defining data structures, especially database schemas.
Many data description languages use a declarative syntax to define fields and data types. SQL,
however, uses a collection of imperative verbs whose effect is to modify the schema of the
database by adding, changing, or deleting definitions of tables or other objects. These statements
can be freely mixed with other SQL statements, so the DDL is not truly a separate language.
Following are the various DDL commands

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 employees (
id
INTEGER
PRIMARY KEY,
first_name
VARCHAR(50) NULL,
last_name
VARCHAR(75) NOT NULL,
dateofbirth
DATE
NULL
);

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 objecttype objectname parameters.
For example, the command to add (then remove) a column named bubbles for an existing table
named sink would be:
ALTER TABLE sink ADD bubbles INTEGER; ALTER TABLE sink DROP COLUMN bubbles;

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 Experimentming 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.

A popular data manipulation language is that of Structured Query Language (SQL), which is
used to retrieve and manipulate data in arelational database.
Data manipulation language is used to append, change or remove data in a table.
In the case of SQL, the verbs are:
SELECT ... FROM ... WHERE ...
INSERT INTO ... VALUES ...
UPDATE ... SET ... WHERE ...
DELETE FROM ... WHERE ...

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
Eg. select *from EMPLOYEE -all attributes
select fname, SSN from EMPLOYEE -only fname and SSN

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.
Eg. 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.
Eg. 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 pies
Where flavor=Lemon;

DDL
DML
CREATE - to create objects in database
SELECT - retrieve data from the a database
ALTER - alters the structure of database
INSERT - insert data into a table
DROP - delete objects from the database
UPDATE - updates existing data within table
TRUNCATE - remove all records from
DELETE - deletes all records from a table,
table, including all spaces allocated for
the space for the records remain
records are removed
o MERGE - UPSERT operation (insert/ update)
o COMMENT - add comments to data
o CALL - call a PL/SQL or Java subExperiment
dictionary
o EXPLAIN PLAN - explain access path to
o RENAME - rename an object
data
o
o
o
o
o
o
o
o

LOCK TABLE - control concurrency

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:
create table salary
(
id int primary key, salary float,
designation varchar(10) not null, check(salary>1000)
);

Output
Table created.

Query

create table emp


(
empid int primary key, name varchar(20)unique , city varchar(10),
eid int ,
foreign key (eid) references salary(id)
);

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:
insert into salary values(1,2000,'accountant');
insert into salary values(2,1500,'driver');

ID

SALARY
DESIGNATION

2000

accountant

1500

driver

2 rows returned in 0.00 seconds

insert into salary values(3,10000,'manager');

ID

SALARY
DESIGNATION

2000

accountant

1500

driver

10000

manager

3 rows returned in 0.01 seconds

insert into salary values(4,5000,'developer');

ID

SALARY
DESIGNATION

2000

accountant

1500

driver

10000

manager

5000

developer

4 rows returned in 0.00 seconds

insert into emp values(1,'Ajay',delhi,2);

EMPID

NAME
CITY
EID

aman
delhi
2

1 rows returned in 0.02 seconds

insert into emp values(2,'ankush','bihar',1)

EMPID

NAME
CITY
EID

aman
delhi
2

ankush
bihar
1

2 rows returned in 0.00 seconds

insert into emp values(3,'aamna','bu.p.',4)

EMPID
NAME
CITY
EID

1
aman
delhi
2

2
ankush
bihar
1

3
aamna
bu.p.
4

3 rows returned in 0.01 seconds

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!

Query:
1.Using where clause:
UPDATE emp
SET name='akash', city='Hamburg'
WHERE name='aamna';

EMPID
NAME
CITY
EID

1
aman
delhi
2

2
ankush
bihar
1

3
akash
Hamburg
4

3 rows returned in 0.01 seconds

update salary
set designation='worker' where id=2

ID

SALARY
DESIGNATION

2000

accountant

1500

worker

10000

manager

5000

developer

4 rows returned in 0.02 seconds

update salary
set salary='12000' where id=3

ID

SALARY
DESIGNATION

2000

accountant

1500

worker

12000

manager

5000

developer

4 rows returned in 0.00 seconds


update salary
set salary='6000'
where designation='developer' ;
ID
SALARY
DESIGNATION
1
2000
accountant
2
1500
worker
3
12000
manager
4
6000
developer

4 rows returned in 0.02 seconds

2.Without using where clause

update salary set salary='6000'

ID

SALARY
DESIGNATION

6000

accountant

6000

worker

6000

manager

6000

developer

4 rows returned in 0.03 seconds

update salary set salary=7000

ID

SALARY
DESIGNATION

7000

accountant

7000

worker

7000

manager

7000

developer

4 rows returned in 0.01 seconds

Experiment 5
Aim: To perform the function of Delete statement with and without
WHERE clause
Theory:
The DELETE statement is used to delete rows in a table. Notice the WHERE clause in the SQL
DELETE statement!

The WHERE clause specifies which record or records that should be deleted. If you omit the
WHERE clause, all records will be deleted!

Query:
1.Using where clause
Select * from salary

ID

SALARY
DESIGNATION

5000

accountant

1000

worker

7000

manager

3000

developer

4 rows returned in 0.01 seconds

delete from salary


where designation='manager'

ID

SALARY
DESIGNATION

5000
accountant

1001
worker

3000
developer

3 rows returned in 0.01 seconds

2.Without using where clause


Select * from emp

EMPID

NAME
CITY
EID

aman
delhi
2

ankush
bihar
1

akash
Hamburg
4

3 rows returned in 0.02 seconds


delete from emp

3 row(s) deleted.
0.03 seconds

no data found

Experiment 6
Aim: To perform the function of Alter statement for adding new
columns and constraints, modifying column type,etc
Theory:
The ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify
columns in an existing table.
Query:
1.To add a column in a table
Select * from emp

EMPID

NAME

CITY

EID

aamna

mp

aman
delhi

ankush
bihar

3 rows returned in 0.01

seconds

alter table emp

add age int

EMPID

NAME

CITY
EID
AGE

aamna
mp
4
-

aman
delhi
2
-

ankush
bihar
1
-

3 rows returned in 0.02 seconds

update emp set age=40

EMPID
NAME
CITY
EID
AGE

1
aamna
mp
4
40

2
aman
delhi
2
40

3
ankush
bihar
1
40

3 rows returned in 0.00 seconds

2. To delete a column in a table


alter table emp drop column age

3. To change the data type of a column in a table


alter table emp modify age float
Table altered.
0.28 seconds

4.To drop a constraint


ALTER TABLE emp drop primary key
Table altered.
1.54 seconds

5.To add constraint


ALTER TABLE emp
add constraint pk primary key (empid,eid)
Table altered.
0.25 seconds

Experiment 7
Aim: To perform the function of SQL inbuilt functions like
SUM,MAX,MIN,AVG,COUNT,DISTINCT etc
Theory:
1.Sum()
The SUM() function returns the total sum of a numeric column.
select * from salary

ID

SALARY
DESIGNATION

5000

accountant

1001

worker

3000

developer

3 rows returned in 0.04 seconds


select sum(salary)from salary
SUM(SALARY)
9001

1 rows returned in 0.03 seconds

2.Max()
The MAX() function returns the largest value of the selected column.
select max(salary)as maximum_salary from salary
MAXIMUM _SALARY
5000

1 rows returned in 0.00 seconds

3.Min()
The MIN() function returns the smallest value of the selected column. select
min(salary)as minimum_salary from salary
MINIMUM_ SALARY
1001

1 rows returned in 0.03 seconds

4.Avg()
The AVG() function returns the average value of a numeric column. select
avg(salary)as average_salary from salary
AVERAGE_ SALARY
3000.33333333333333333333333333333333333

1 rows returned in 0.06 seconds

5.Count()
The COUNT() function returns the number of rows that matches a specified criteria.
SELECT count(*) from emp
COUNT(*)
3

1 rows returned in 0.01 seconds

6.Distinct()
select * from emp

EMPID

NAME
CITY
EID
AGE
DATE_ OF_ JOIN

aamna
mp
4
40
2001-12-12 , tuesday

sameer
bihar
4

50
2000-01-01

sadhna
delhi
4
50
2000-01-01

aman
delhi
2
40
2014-01-02
, monday

ankush
bihar
1
40
2009-11-02
, sunday

5 rows returned in 0.02 seconds

select distinct(city) from emp


CITY
delhi
mp
bihar

3 rows returned in 0.06 seconds

Вам также может понравиться