Lab 1
Lab 1
Lab 1
---------------------------------------------------------------------------------------------------
/* 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 world
-----------------
TABLE climate
-------------------
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.
1 row created.
1 row created.
CITY COUNTRY
--------------- ---------------
ernakulam india
kottayam india
kollam india
kothamangalam India
1 row created.
1 row created.
1 row created.
1 row created.
SOLUTION
--------------
1 row deleted.
1 row deleted.
NEW TABLE
CITY COUNTRY
--------------- ---------------
ernakulam india
kottayam india
kollam india
1 row created.
NEW TABLE
CITY COUNTRY
--------------- ---------------
ernakulam india
kottayam india
kollam india
piravom india
1 row created.
NEW TABLE
SQL> select * from climate;
1 row updated.
1 row created.
1 row created.
1 row created.
1 row created.
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.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
NEW TABLE
SQL> select * from climate;
10 rows selected.
QUERIES BASED ON TABLES world AND climate
CITY COUNTRY
--------------- ---------------
ernakulam india
kottayam india
kollam india
piravom india
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.
COUNTRY AVG(TEMP)
--------------- ----------------
india 188.0475
iraq 32
pakistan 28.3333333
usa 37
COUNT(COUNTRY)
-----------------------------
4
1
3
2
UPDATION OF TABLE climate
1 row updated.
1 row updated.
1 row updated.
NEW TABLE
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.
Synonym created.
10 rows selected.
Index created.
TO CREATE SYNONYM location FOR TABLE world
Synonym created.
TABLE DESCRIPTION - location
10 rows selected.
SELECT OPERATIONS
ADDING CONSTRAINTS
UNIQUE
~~~~~~--
Table altered.
CHECK
~~~~~~~
SQL> alter table climate add constraint ch check(temp > 20 and temp < 50);
Table altered.
Table altered.
Table altered.
SQL> alter table climate add constraint forei foreign key(city) references
world(city) on delete cascade deferrable initially deferred;
Table altered.
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.