Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Practical-3

1. Create a view from the existing table as per your requirement.

SQL> create or replace view viewsalespeople as select snum view_id,


comm view_comm from salespeople;

SQL> select * from viewsalespeople;

VIEW_ID VIEW_COMM
---------- ----------
101 25.52
102 12.21
103 11.11
104 33.33
105 88.99
1 1.1
106
107 11.22
108
109

2. Update a view as per your requirement.

SQL> create or replace view viewsalespeople as SELECT * FROM


salespeople WHERE Ciry = 'Ahmedabad';
SQL> select * from viewsalespeople;

SNUM SNAME CIRY COMM


---------- -------------------- --------------- ----------
105 Maggi Vashi Ahmedabad 88.99
1 A Ahmedabad 1.1

3. Droping the view


SQL> drop view viewsalespeople;

4. ADD column in an existing table

SQL> ALTER TABLE salespeople ADD mobile varchar(10);


5. REMOVE column in an existing table

6. MODIFY the datatype of the newly added column.

SQL> ALTER TABLE salespeople DROP COLUMN mobile;

Table altered.

SQL> select * from salespeople;

SNUM SNAME CIRY COMM


---------- -------------------- --------------- ----------
101 Devendra Vashi Kosamba 25.52
102 Yogini Vashi Boridatr 12.21
103 Ada Lawrence Belgaum 11.11
104 Yesha Rana Deli 33.33
105 Maggi Vashi Ahmedabad 88.99
1A Ahmedabad 1.1
106 Karan Ahmedanad
107 Dip Gandhinagar 11.22
108 Sakib Goa
109 Vijay Mumbai

7. Make a new table from the old table with the same structure.

SQL> ALTER TABLE salespeople ADD mobile varchar(10);

Table altered.

SQL> ALTER TABLE salespeople MODIFY mobile number(10);


Table altered.

SQL> desc salespeople;

Name Null? Type


----------------------------------------- -------- -------
SNUM NOT NULL NUMBER(4)
SNAME VARCHAR2(20)
CIRY VARCHAR2(15)
COMM NUMBER(5,2)
MOBILE NUMBER(10)

8. Delete all the records from the table

SQL> create table sp2 as select * from salespeople;

Table created.

SQL> select * from salespeople;

SNUM SNAME CIRY COMM MOBILE


---------- -------------------- --------------- ----------
----------
101 Devendra Vashi Kosamba 25.52
102 Yogini Vashi Boridatr 12.21
103 Ada Lawrence Belgaum 11.11
104 Yesha Rana Deli 33.33
105 Maggi Vashi Ahmedabad 88.99
1A Ahmedabad 1.1
106 Karan Ahmedanad
107 Dip Gandhinagar 11.22
108 Sakib Goa
109 Vijay Mumbai

10 rows selected.
SQL> select * from sp2;

SNUM SNAME CIRY COMM MOBILE


---------- -------------------- --------------- ----------
----------
101 Devendra Vashi Kosamba 25.52
102 Yogini Vashi Boridatr 12.21
103 Ada Lawrence Belgaum 11.11
104 Yesha Rana Deli 33.33
105 Maggi Vashi Ahmedabad 88.99
1A Ahmedabad 1.1
106 Karan Ahmedanad
107 Dip Gandhinagar 11.22
108 Sakib Goa
109 Vijay Mumbai

10 rows selected.

9. Drop the table.

SQL> drop table sp2;

Table dropped.

SQL> select * from sp2;


select * from sp2
*
ERROR at line 1:
ORA-00942: table or view does not exist

10. Create a table with the CHECK constraint.

Create a table with CHECK constraint


=======================
SQL> CREATE TABLE Persons (
2 ID int NOT NULL,
3 LastName varchar(255) NOT NULL,
4 FirstName varchar(255),
5 Age int,
6 CHECK (Age>=18)
7 );

Table created.

SQL> insert into Persons


values(&ID,'&LastName','&FirstName',&Age)
SQL> insert into Persons
values(&ID,'&LastName','&FirstName',&Age);
Enter value for id: 1
Enter value for lastname: vashi
Enter value for firstname: devendra
Enter value for age: 42
old 1: insert into Persons
values(&ID,'&LastName','&FirstName',&Age)
new 1: insert into Persons values(1,'vashi','devendra',42)

1 row created.

SQL> insert into Persons


values(&ID,'&LastName','&FirstName',&Age);
Enter value for id: 2
Enter value for lastname: vashi
Enter value for firstname: yana
Enter value for age: 10

11. mention the comment in SQL

SQL> SELECT * FROM Salespeople -- display the whole


table contect';
SNUM SNAME CIRY COMM MOBILE
---------- -------------------- --------------- ----------
----------
101 Devendra Vashi Kosamba 25.52
102 Yogini Vashi Boridatr 12.21
103 Ada Lawrence Belgaum 11.11
104 Yesha Rana Deli 33.33
105 Maggi Vashi Ahmedabad 88.99
1A Ahmedabad 1.1
106 Karan Ahmedanad
107 Dip Gandhinagar 11.22
108 Sakib Goa
109 Vijay Mumbai

10 rows selected.

12. selects all customers with a CustomerName starting with "G".

SQL> SELECT * FROM Customer WHERE City LIKE '%G';

CNUM CNAME CITY RATING SNUM


---------- -------------------- --------------- ---------- ----------
201 Aladin Goa 1 101

13. selects all customers with a CustomerName ending with "G"

SQL> SELECT * FROM Customer WHERE City LIKE 'G%';

CNUM CNAME CITY RATING SNUM


---------- -------------------- --------------- ---------- ----------
201 Aladin Goa 1 101
14. selects all customers with a CustomerName that have "ra" in any
position

SQL> SELECT * FROM Customer WHERE City LIKE '%ra%';

CNUM CNAME CITY RATING SNUM


---------- -------------------- --------------- ---------- ----------
202 Malaram Surat 1 101
203 Ram Surat 2 102
204 Kishan Bharuch 3 104
205 Mehul Dhandhinagar 4 105

15. selects all customers with a City starting with any character,
followed by "_haruch"

SQL> SELECT * FROM Customer WHERE City LIKE '_haruch';

CNUM CNAME CITY RATING SNUM


---------- -------------------- --------------- ---------- ----------
204 Kishan Bharuch 3 104

16. selects all customers with a City starting with "B", followed by
any character, followed

by "a", followed by any character, followed by "uc"

SQL> SELECT * FROM Customer WHERE City LIKE 'B_a_uch';

CNUM CNAME CITY RATING SNUM


---------- -------------------- --------------- ---------- ----------
204 Kishan Bharuch 3 104

17. selects all customers with a City starting with "G" and ends with
"a"
SQL> SELECT * FROM Customer WHERE City LIKE 'G%a';

CNUM CNAME CITY RATING


---------- -------------------- --------------- ---------- ---------
201 Aladin Goa 1 101

18. selects all customers that are located in "Goa", "France" or


"Ahmedabad"

SQL> SELECT * FROM Customer WHERE City IN ('Goa', 'France',


'Ahmedabad');

CNUM CNAME CITY RATING SNUM


---------- -------------------- --------------- ---------- ---------
201 Aladin Goa 1 101

19. selects all customers that are not located in "Goa", "France" or
"Ahmedabad"

SQL> SELECT * FROM Customer WHERE City NOT IN ('Goa',


'France', 'Ahmedabad');

CNUM CNAME CITY RATING SNUM


---------- -------------------- --------------- ---------- ----------
202 Malaram Surat 1 101
203 Ram Surat 2 102
204 Kishan Bharuch 3 104
205 Mehul Dhandhinagar 4 105

20. selects all customers that are from the same city as the
salespeople:

SQL> SELECT * FROM Customer WHERE city IN (SELECT ciry


FROM salespeople);
CNUM CNAME CITY RATING SNUM
---------- -------------------- --------------- ---------- ----------
201 Aladin Goa 1 101

You might also like