0% found this document useful (0 votes)
19 views

Practical 3

The document discusses various SQL commands like create view, update view, drop view, add column, remove column, modify column datatype, create new table from old table, delete records from table, drop table, create table with check constraint, use comments in SQL, and use LIKE operator for pattern matching.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Practical 3

The document discusses various SQL commands like create view, update view, drop view, add column, remove column, modify column datatype, create new table from old table, delete records from table, drop table, create table with check constraint, use comments in SQL, and use LIKE operator for pattern matching.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Database Management System -6CS104

2023-24
Name:Yash Lalchandani
Rollno :23MCA027
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 shiva bua Ahmedabad 88.99
1 A Ahmedabad 1.1
3. Dropping the view
SQL> drop view viewsalespeople;
View dropped.

4. ADD column in an existing table


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

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

5. REMOVE column in an existing table


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

6. MODIFY the datatype of the newly added column.


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)

7. Make a new table from the old table with the same structure.
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.
8. Delete all the records from the table
SQL> delete from sp2;

10 rows deleted.

SQL> select * from sp2;

no 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
==============================

TRUNCATE TABLE Categories;

The TRUNCATE TABLE command deletes the data inside a table, but not the
table itself.
===============================
10. Create a table with the 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
old 1: insert into Persons values(&ID,'&LastName','&FirstName',&Age)
new 1: insert into Persons values(2,'vashi','yana',10)
insert into Persons values(2,'vashi','yana',10)
*
ERROR at line 1:
ORA-02290: check constraint (SYSTEM.SYS_C0011461) violated

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.
=============================
SQL> select * from Customer;

CNUM CNAME CITY RATING SNUM


---------- -------------------- --------------- ---------- ----------
201 Aladin Goa 1 101
202 Malaram Surat 1 101
203 Ram Surat 2 102
204 Kishan Bharuch 3 104
205 Mehul Dhandhinagar 4 105
==============================================
12. selects all customers with a Customer Name starting with "G"

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


CNUM CNAME CITY RATING SNUM
---------- -------------------- --------------- ---------- ----------
201 Aladin Goa 1 101

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


?
13. selects all customers with a Customer Name ending with "G"
SQL> SELECT * FROM Customer WHERE City LIKE '%G';
no rows selected
14. selects all customers with a Customer Name 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

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

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 "uch"

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 ending with "a"
SQL> SELECT * FROM Customer WHERE City LIKE 'G%a';

CNUM CNAME CITY RATING SNUM


---------- -------------------- --------------- ---------- ----------
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
18. selects all customers that are 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