dbms

Download dbms

If you can't read please download the document

Upload: divyesh-kumar

Post on 30-Sep-2015

3 views

Category:

Documents


0 download

DESCRIPTION

dbms

TRANSCRIPT

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 (

idINTEGERPRIMARY KEY,first_nameVARCHAR(50) NULL,last_nameVARCHAR(75) NOT NULL,dateofbirthDATENULL

);

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 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=20where 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 balance1000)

);

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

1

2000

accountant

2

1500

driver

2 rows returned in 0.00 seconds

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

ID

SALARY

DESIGNATION

1

2000

accountant

2

1500

driver

3

10000

manager

3 rows returned in 0.01 seconds

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

ID

SALARY

DESIGNATION

1

2000

accountant

2

1500

driver

3

10000

manager

4

5000

developer

4 rows returned in 0.00 seconds

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

EMPID

NAME

CITY

EID

1

aman

delhi

2

1 rows returned in 0.02 seconds

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

EMPID

NAME

CITY

EID

1

aman

delhi

2

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 empSET 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

1

2000

accountant

2

1500

worker

3

10000

manager

4

5000

developer

4 rows returned in 0.02 seconds

update salary

set salary='12000' where id=3

ID

SALARY

DESIGNATION

1

2000

accountant

2

1500

worker

3

12000

manager

4

5000

developer

4 rows returned in 0.00 seconds

update salary

set salary='6000'

where designation='developer' ;

IDSALARYDESIGNATION12000accountant21500worker312000manager46000developer

4 rows returned in 0.02 seconds

2.Without using where clause

update salary set salary='6000'

ID

SALARY

DESIGNATION

1

6000

accountant

2

6000

worker

3

6000

manager

4

6000

developer

4 rows returned in 0.03 seconds

update salary set salary=7000

ID

SALARY

DESIGNATION

1

7000

accountant

2

7000

worker

3

7000

manager

4

7000

developer

4 rows returned in 0.01 secondsExperiment 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

1

5000

accountant

2

1000

worker

3

7000

manager

4

3000

developer

4 rows returned in 0.01 seconds

delete from salarywhere designation='manager'

ID

SALARY

DESIGNATION

1

5000

accountant

2

1001

worker

4

3000

developer

3 rows returned in 0.01 seconds

2.Without using where clause

Select * from emp

EMPID

NAME

CITY

EID

1

aman

delhi

2

2

ankush

bihar

1

3

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

1

aamna

m p

4

2

aman

delhi

2

3

ankush

bihar

1

3 rows returned in 0.01

seconds

alter table emp

add age int

EMPID

NAME

CITY

EID

AGE

1

aamna

m p

4

-

2

aman

delhi

2

-

3

ankush

bihar

1

-

3 rows returned in 0.02 seconds

update emp set age=40

EMPID

NAME

CITY

EID

AGE

1

aamna

m p

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 empadd constraint pk primary key (empid,eid)

Table altered.

0.25 secondsExperiment 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

1

5000

accountant

2

1001

worker

4

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

1

aamna

m p

4

40

2001-12-12 , tuesday

4

sameer

bihar

4

50

2000-01-01

5

sadhna

delhi

4

50

2000-01-01

2

aman

delhi

2

40

2014-01-02, monday

3

ankush

bihar

1

40

2009-11-02, sunday

5 rows returned in 0.02 seconds

select distinct(city) from emp

CITY

delhi

m p

bihar

3 rows returned in 0.06 seconds