Sie sind auf Seite 1von 25

Pgm No : 1 Date: 30/07/09’

---------------------------------------------------------------------------------------------------
/* Create a table using the following fields
-> city
-> country
-> temp(temperature)
-> humidity
-> condition.
Normalize the table,then add all constraints and perform all operations
using sql queries.*/
---------------------------------------------------------------------------------------------------

TABLE CREATION USING CREATE COMMAND

TABLE world
-----------------

SQL> create table world(city varchar2(15) primary key not null,


country varchar2(15));
Table created.

TABLE climate
-------------------

SQL> create table climate(temp number(6,2),


humidity number(6,2),
condition varchar2(10),
city varchar2(15),
foreign key(city) references world);
Table created.
TABLE DESCRIPTION USING DESC

SQL> desc world;

Name Null? Type


------------------------------------- -------- -------------------------
CITY NOT NULL VARCHAR2(15)
COUNTRY VARCHAR2(15)

SQL> desc climate;


Name Null? Type
------------------------------------- -------- -------------------------
TEMP NUMBER(6,2)
HUMIDITY NUMBER(6,2)
CONDITION VARCHAR2(10)
CITY VARCHAR2(15)

INSERTING ROWS INTO TABLE world USING INSERT COMMAND


AND SUBSTITUITION VARIABLES

SQL> insert into world values('&city','&country');


Enter value for city: ernakulam
Enter value for country: india
old 1: insert into world values('&city','&country')
new 1: insert into world values('ernakulam','india')

1 row created.
SQL> insert into world values('&city','&country');
Enter value for city: kottayam
Enter value for country: india
old 1: insert into world values('&city','&country')
new 1: insert into world values('kottayam','india')

1 row created.

SQL> insert into world values('&city','&country');


Enter value for city: kollam
Enter value for country: india
old 1: insert into world values('&city','&country')
new 1: insert into world values('kollam','india')

1 row created.

SQL> insert into world values('&city','&country');


Enter value for city: kothamangalam
Enter value for country: india
old 1: insert into world values('&city','&country')
new 1: insert into world values('kothamangalam','india')

1 row created.

VIEWING DATA IN THE TABLE USING SELECT COMMAND

SQL> select * from world;

CITY COUNTRY
--------------- ---------------
ernakulam india
kottayam india
kollam india
kothamangalam India

INSERTING ROWS INTO TABLE climate USING INSERT


COMMAND AND SUBSTITUITION VARIABLES

SQL> insert into climate values(&temp,&humidity,'&condition','&city');


Enter value for temp: 120.60
Enter value for humidity: 30.0
Enter value for condition: rainy
Enter value for city: ernakulam
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(120.60,30.0,'rainy','ernakulam')

1 row created.

SQL> insert into climate values(&temp,&humidity,'&condition','&city')


Enter value for temp: 100.70
Enter value for humidity: 45.0
Enter value for condition: rainy
Enter value for city: kottayam
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(100.70,45.0,'rainy','kottayam')

1 row created.

SQL> insert into climate values(&temp,&humidity,'&condition','&city')


Enter value for temp: 230.89
Enter value for humidity: 45.9
Enter value for condition: hot
Enter value for city: kollam
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(230.89,45.9,'hot','kollam')

1 row created.

SQL> insert into climate values(&temp,&humidity,'&condition','&city');


Enter value for temp: 200
Enter value for humidity: 50
Enter value for condition: cold
Enter value for city: kothamangalam
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(200,50,'cold','kothamangalam')

1 row created.

VIEWING DATA IN THE TABLE USING SELECT COMMAND

SQL> select * from climate;

TEMP HUMIDITY CONDITION CITY


---------- -------------- ---------------- -------
120.6 30 rainy ernakulam
100.7 45 rainy kottayam
230.89 45.9 hot kollam
200 50 cold kothamangalam

INTEGRITY CONSTRAINT VOILATION DURING DELETE


OPERATION
SQL> delete from world where city='kothamangalam';
delete from world where city='kothamangalam'
*
ERROR at line 1:
ORA-02292: integrity constraint (SYSTEM.SYS_C002774) violated - child record
found

SOLUTION
--------------

SQL> delete from climate where city='kothamangalam';

1 row deleted.

SQL> delete from world where city='kothamangalam';

1 row deleted.
NEW TABLE

SQL> select * from world;

CITY COUNTRY
--------------- ---------------
ernakulam india
kottayam india
kollam india

INSERTING NEW ROWS INTO TABLE WORLD USING INSERT


COMMAND AND SUBSTITUITION VARIABLES

SQL> insert into world values('&city','&country');


Enter value for city: piravom
Enter value for country: india
old 1: insert into world values('&city','&country')
new 1: insert into world values('piravom','india')

1 row created.
NEW TABLE

SQL> select * from world;

CITY COUNTRY
--------------- ---------------
ernakulam india
kottayam india
kollam india
piravom india

INSERTING NEW ROWS INTO TABLE CLIMATE USING INSERT


COMMAND AND SUBSTITUITION VARIABLES

SQL> insert into climate values(&temp,&humidity,'&condition','&city');


Enter value for temp: 300
Enter value for humidity: 23
Enter value for condition: hot
Enter value for city: piravom
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(300,23,'hot','piravom')

1 row created.
NEW TABLE
SQL> select * from climate;

TEMP HUMIDITY CONDITION CITY


---------- ------------- --------------- -----------
120.6 30 rainy ernakulam
100.7 45 rainy kottayam
230.89 45.9 hot kollam
300 23 hot piravom

INTEGRITY CONSTRAINT VIOLATION DURING UPDATE


OPERATION

SQL> update world set city='trivandrum' where city='piravom';


update world set city='trivandrum' where city='piravom'
*
ERROR at line 1:
ORA-02292: integrity constraint (SYSTEM.SYS_C002774) violated - child record
Found
INTEGRITY CONSTRAINT VIOLATION DURING DROP OPERATION

SQL> drop table world;


drop table world
*
ERROR at line 1:
ORA-02449: unique/primary keys in table referenced by foreign keys
select * from world inner join climate on world.city=climate.city;

INTEGRITY CONSTRAINT VIOLATION DURING ALTER OPERATION

SQL> alter table world drop column city;


alter table world drop column city
*
ERROR at line 1:
ORA-12992: cannot drop parent key column
SQL> update climate set temp=30 where temp=120.6;

1 row updated.

INSERTING NEW ROWS INTO TABLE world USING INSERT


COMMAND AND SUBSTITUITION VARIABLES

SQL> insert into world values('&city','&country');


Enter value for city: islammabad
Enter value for country: pakistan
old 1: insert into world values('&city','&country')
new 1: insert into world values('islammabad','pakistan')

1 row created.

SQL> insert into world values('&city','&country');


Enter value for city: baghdad
Enter value for country: iraq
old 1: insert into world values('&city','&country')
new 1: insert into world values('baghdad','iraq')

1 row created.

SQL> insert into world values('&city','&country');


Enter value for city: washington
Enter value for country: usa
old 1: insert into world values('&city','&country')
new 1: insert into world values('washington','usa')
1 row created.

SQL> insert into world values('&city','&country');


Enter value for city: columbo
Enter value for country: usa
old 1: insert into world values('&city','&country')
new 1: insert into world values('columbo','usa')

1 row created.

SQL> insert into world values('&city','&country');


Enter value for city: karachi
Enter value for country: pakistan
old 1: insert into world values('&city','&country')
new 1: insert into world values('karachi','pakistan')

1 row created.

SQL> insert into world values('&city','&country');


Enter value for city: lahore
Enter value for country: pakistan
old 1: insert into world values('&city','&country')
new 1: insert into world values('lahore','pakistan')

1 row created.
NEW TABLE
SQL> select * from world;

CITY COUNTRY
--------------- ---------------
ernakulam india
kottayam india
kollam india
baghdad iraq
piravom india
islammabad pakistan
washington usa
columbo usa
karachi pakistan
lahore pakistan

10 rows selected.

INSERTING NEW ROWS INTO TABLE climate USING INSERT


COMMAND AND SUBSTITUITION VARIABLES

SQL> insert into climate values(&temp,&humidity,'&condition','&city');


Enter value for temp: 32
Enter value for humidity: 45
Enter value for condition: rainy
Enter value for city: lahore
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(32,45,'rainy','lahore')

1 row created.

SQL> insert into climate values(&temp,&humidity,'&condition','&city');


Enter value for temp: 23
Enter value for humidity: 56
Enter value for condition: hoy
Enter value for city:
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(23,56,'hoy','')

1 row created.

SQL> insert into climate values(&temp,&humidity,'&condition','&city');


Enter value for temp: 23
Enter value for humidity: 45
Enter value for condition: hot
Enter value for city: karachi
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(23,45,'hot','karachi')

1 row created.

SQL> insert into climate values(&temp,&humidity,'&condition','&city');


Enter value for temp: 30
Enter value for humidity: 40
Enter value for condition: cold
Enter value for city: islammabad
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(30,40,'cold','islammabad')

1 row created.

SQL> insert into climate values(&temp,&humidity,'&condition','&city');


Enter value for temp: 34
Enter value for humidity: 44
Enter value for condition: rainy
Enter value for city: columbo
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(34,44,'rainy','columbo')

1 row created.

SQL> insert into climate values(&temp,&humidity,'&condition','&city');


Enter value for temp: 40
Enter value for humidity: 50
Enter value for condition: hot
Enter value for city: washington
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(40,50,'hot','washington')

1 row created.

SQL> insert into climate values(&temp,&humidity,'&condition','&city');


Enter value for temp: 32
Enter value for humidity: 43
Enter value for condition: hot
Enter value for city: baghdad
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(32,43,'hot','baghdad')

1 row created.
NEW TABLE
SQL> select * from climate;

TEMP HUMIDITY CONDITION CITY


---------- ------------- ----------------- ------
120.6 30 rainy ernakulam
100.7 45 rainy kottayam
230.89 45.9 hot kollam
23 45 hot karachi
300 23 hot piravom
32 45 rainy lahore
30 40 cold islammabad
34 44 rainy columbo
40 50 hot washington
32 43 hot baghdad

10 rows selected.
QUERIES BASED ON TABLES world AND climate

i) To find the name of all cities with country 'india'


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SQL> select * from world where country='india';

CITY COUNTRY
--------------- ---------------
ernakulam india
kottayam india
kollam india
piravom india

ii) To sort the country in ascending order


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SQL> select * from world order by country asc;

CITY COUNTRY
--------------- ---------------
ernakulam india
kottayam india
kollam india
piravom india
baghdad iraq
islammabad pakistan
karachi pakistan
lahore pakistan
washington usa
columbo usa

10 rows selected.

iii) Joints & correlation using inner join


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SQL> select * from world inner join climate on world.city=climate.city;

CITY COUNTRY TEMP HUMIDITY CONDITION CITY


--------------- --------------- -------- -------------- ----------------
-------------
ernakulam india 120.6 30 rainy ernakulam
kottayam india 100.7 45 rainy kottayam
kollam india 230.89 45.9 hot kollam
karachi pakistan 23 45 hot karachi
piravom india 300 23 hot piravom
lahore pakistan 32 45 rainy lahore
islammabad pakistan 30 40 cold islammabad
columbo usa 34 44 rainy columbo
washington usa 40 50 hot washington
baghdad iraq 32 43 hot baghdad
10 rows selected.

iv) set functions & concatenation


~~~~~~~~~~~~~~~~~~~~~~~~~~~

SQL> select country,avg(temp) from world inner join climate on world.city =


climate.city group by country;

COUNTRY AVG(TEMP)
--------------- ----------------
india 188.0475
iraq 32
pakistan 28.3333333
usa 37

SQL> select temp,humidity,condition,city from climate where city in(select city


from world where country='india');

TEMP HUMIDITY CONDITION CITY


---------- ------------- ---------------- --------
120.6 30 rainy ernakulam
100.7 45 rainy kottayam
230.89 45.9 hot kollam
300 23 hot piravom

SQL> select condition,count(condition) from climate group by condition;


CONDITION COUNT(CONDITION)
----------------- ------------------------------
cold 1
hot 5
rainy 4

SQL> select count(country) from world group by country;

COUNT(COUNTRY)
-----------------------------
4
1
3
2
UPDATION OF TABLE climate

SQL> update climate set temp=27.33 where temp=100.7;

1 row updated.

SQL> update climate set temp=25.02 where temp=230.89;

1 row updated.

SQL> update climate set temp=30.02 where temp=300;

1 row updated.
NEW TABLE

SQL> select * from climate;


TEMP HUMIDITY CONDITION CITY
--------- -------------- ----------------- --------------
30 30 rainy ernakulam
27.33 45 rainy kottayam
25.02 45.9 hot kollam
23 45 hot karachi
30.02 23 hot piravom
32 45 rainy lahore
30 40 cold islammabad
34 44 rainy columbo
40 50 hot washington
32 43 hot baghdad

10 rows selected.

OTHER QUERIES
i)

SQL> select city || ' has a temperature of '|| temp from climate;

CITY||'HASATEMPERATUREOF'||TEMP
----------------------------------------------------------
ernakulam has a temperature of 30
kottayam has a temperature of 27.33
kollam has a temperature of 25.02
karachi has a temperature of 23
piravom has a temperature of 30.02
lahore has a temperature of 32
islammabad has a temperature of 30
columbo has a temperature of 34
washington has a temperature of 40
baghdad has a temperature of 32

10 rows selected.

TO CREATE SYNONYM WEATHER FOR TABLE climate

SQL> create synonym weather for climate;

Synonym created.

TABLE DESCRIPTION - weather

SQL> desc weather;


Name Null? Type
---------------------------------- -------- ---------------------
TEMP NUMBER(6,2)
HUMIDITY NUMBER(6,2)
CONDITION VARCHAR2(10)
CITY VARCHAR2(15)

VIEWING DATA IN THE TABLE USING SELECT COMMAND

SQL> select * from weather;

TEMP HUMIDITY CONDITION CITY


------- --------------- ----------------- -----------
30 30 rainy ernakulam
27.33 45 rainy kottayam
25.02 45.9 hot kollam
23 45 hot karachi
30.02 23 hot piravom
32 45 rainy lahore
30 40 cold islammabad
34 44 rainy columbo
40 50 hot washington
32 43 hot baghdad

10 rows selected.

CREATING INDEX ON COLUMN CITY OF TABLE weather

SQL> create index city_id on weather(city);

Index created.
TO CREATE SYNONYM location FOR TABLE world

SQL> create synonym location for world;

Synonym created.
TABLE DESCRIPTION - location

SQL> desc location;


Name Null? Type
----------------------------------------- -------- ----------------------------
CITY NOT NULL VARCHAR2(15)
COUNTRY VARCHAR2(15)

VIEWING DATA IN THE TABLE USING SELECT COMMAND

SQL> select * from location;


CITY COUNTRY
------------ -------------
ernakulam india
kottayam india
kollam india
baghdad iraq
piravom india
islammabad pakistan
washington usa
columbo usa
karachi pakistan
lahore pakistan

10 rows selected.
SELECT OPERATIONS

SQL> select w.country,w.city,c.temp,c.humidity,c.condition from world w,climate c


where w.city=c.city;

COUNTRY CITY TEMP HUMIDITY CONDITION


--------------- ------------- --------- ---------------- ------------------
india ernakulam 30 30 rainy
india kottayam 27.33 45 rainy
india kollam 25.02 45.9 hot
pakistan karachi 23 45 hot
india piravom 30.02 23 hot
pakistan lahore 32 45 rainy
pakistan islammabad 30 40 cold
usa columbo 34 44 rainy
usa washington 40 50 hot
iraq baghdad 32 43 hot
10 rows selected.

ADDING CONSTRAINTS
UNIQUE
~~~~~~--

SQL> alter table climate add constraint un unique(city);

Table altered.

CHECK
~~~~~~~

SQL> alter table climate add constraint ch check(temp > 20 and temp < 50);

Table altered.

ON DELETE CASCADE , DEFERRABLE INITIALLY DEFERRED


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

SQL> alter table climate drop column city;

Table altered.

SQL> desc climate;


Name Null? Type
------------------------------------- -------- -------------------
TEMP NUMBER(6,2)
HUMIDITY NUMBER(6,2)
CONDITION VARCHAR2(10)
SQL> alter table climate add city varchar2(15);

Table altered.

SQL> alter table climate add constraint forei foreign key(city) references
world(city) on delete cascade deferrable initially deferred;

Table altered.

update climate set city='ernakulam' where temp=30 and humidity=30;


update climate set city='kottayam' where temp=27.33 and humidity=45;
update climate set city='kollam' where temp=25.02 and humidity=45.9;
update climate set city='karachi' where temp=23 and humidity=45;
update climate set city='piravom' where temp=30.02 and humidity=23;
update climate set city='lahore' where temp=32 and humidity=45;
update climate set city='islammabad' where temp=30 and humidity=40;
update climate set city='columbo' where temp=34 and humidity=44;
update climate set city='washington' where temp=40 and humidity=50;
update climate set city='baghdad' where temp=32 and humidity=43;

SQL> desc climate;


Name Null? Type
-------------------------------------- -------- ----------------------------
TEMP NUMBER(6,2)
HUMIDITY NUMBER(6,2)
CONDITION VARCHAR2(10)
CITY VARCHAR2(15)

VIOLATING THE UNIQUE CONSTRAINT


SQL> insert into climate values(&temp,&humidity,'&condition','&city');
Enter value for temp: 32
Enter value for humidity: 32
Enter value for condition: hot
Enter value for city: piravom
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(32,32,'hot','piravom')
insert into climate values(32,32,'hot','piravom')
*
ERROR at line 1:
ORA-00001: unique constraint (SYSTEM.UN) violated

VIOLATING THE CHECK CONSTRAINT

SQL> insert into climate values(&temp,&humidity,'&condition','&city');


Enter value for temp: 19
Enter value for humidity: 30
Enter value for condition: cold
Enter value for city: elenji
old 1: insert into climate values(&temp,&humidity,'&condition','&city')
new 1: insert into climate values(19,30,'cold','elenji')
insert into climate values(19,30,'cold','elenji')
*
ERROR at line 1:
ORA-02290: check constraint (SYSTEM.CH) violated

USE OF ON DELETE CASCADE

SQL> delete from world where city='piravom';

1 row deleted.
CITY COUNTRY
--------------- ---------------
ernakulam india
kottayam india
kollam india
baghdad iraq
islammabad pakistan
karachi pakistan
lahore pakistan
washington usa
columbo usa

9 rows selected.

SQL> select * from climate;

TEMP HUMIDITY CONDITION CITY


----------- ------------- ----------------- ------------
30 30 rainy ernakulam
27.33 45 rainy kottayam
25.02 45.9 hot kollam
23 45 hot karachi
32 45 rainy lahore
30 40 cold islammabad
34 44 rainy columbo
40 50 hot washington
32 43 hot baghdad
9 rows selected.

Das könnte Ihnen auch gefallen