dbms-file-1
dbms-file-1
Downloaded by Rekha
Studocu is not sponsored or endorsed by any college or university
Downloaded by Rekha
1. Used of CREATE, ALTER, RENAME, and DROP statements in the
database tables(relations).
Syntax: -
CREATE TABLE table name
( column1 datatype,
column2 datatype,
column3 datatype,
....
);
Example: -
OUTPUT:-
Downloaded by Rekha
• The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.
• The ALTER TABLE statement is also used to add and drop various constraints on an
existing table.
Syntax:-
ALTER TABLE table_name
ADD column_name datatype;
Example:-
OUTPUT:-
In MySQL, the RENAME TABLE statement emerges as a powerful tool, allowing database
administrators to seamlessly rename one or more tables.
Synatx:-
RENAME TABLE old_table_name TO new_table_name [, …];
Example:-
Downloaded by Rekha
SQL> ALTER TABLE Person1
2 Rename to Person2;
OUTPUT:-
Syntax:-
DROP TABLE table_name;
Example:-
Drop table Person1;
OUTPUT:-
Table Dropped
Downloaded by Rekha
I. INSERT INTO Command: -
The INSERT INTO statement is used to insert new records in a table.
Specify both the column names and the values to be inserted:
Syntax: -
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Exapmle:-
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
Downloaded by Rekha
SQL> insert into
Person1(PersonId,LastName,FirstName,Address,City) 2
Values('6','Kaur','Snehpreet','BRS Nagar','Ludhiana');
1 row created.
1 row created.
1 row created.
OUTPUT:-
Syntax:-
DELETE FROM table_name WHERE condition;
Example:-
Downloaded by Rekha
SQL> Delete from Person1 Where FirstName='Snehpreet';
Downloaded by Rekha
OUTPUT:-
1 row deleted.
Syntax:-
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:-
SQL> Update Person1
2 SET LastName='Sharma',City='Jalandhar'
3 Where PersonId=2;
OUTPUT:-
Downloaded by Rekha
Syntax:-
Example:-
OUTPUT:-
Downloaded by Rekha
6. Use of Aggregate Function.
Aggregate functions are functions that perform a calculation on a set of values and return a
single value.
Here are some Aggregate Function:-
I. COUNT:-
Example:-
OUTPUT:-
II. SUM:-
Syntax:-
SUM(expression)
Example:-
OUTPUT:-
Downloaded by Rekha
III. MAX:-
Syntax:-
MAX(expression)
Example:-
OUTPUT:-
Downloaded by Rekha
7. Use of substring comparison.
Syntax: -
SUBSTRING (string, start, length)
Parameter Values
Parameter Description
Example: -
Output:-
Downloaded by Rekha
8. Use of order by statement.
The ORDER BY statement in SQL is used to sort the fetched data in either ascending or
descending according to one or more columns.
Syntax:-
Example: -
Output: -
Downloaded by Rekha
9. Consider the following schema for a library data
base:- BOOK (Book_id, Title,
Publisher_name,Pub_year) BOOK_AUTHORS
(Book_id, Author_name) PUBLISHGER (Name,
address, Phone) BOOK_COPIES (Book_id,Branch
_id,NO-of_Copies) BOOK _LENDING (Book_id,
branch_id,Card_NO., Date_Out,Due_Date)
LIBRARY_BRANCH (Branch _id, Branch_name, Address)
Write SQL queries to
1. Retrieve details of all the books in the library_id, Title, Name
of Publisher, Authors, No. of copies in each branch. Etc.
2. Get the particulars of browsers who have borrowed more than
three books between Jan 2018 to June 2018.
3. Delete a book in BOOK TABLE. Update the contents of other
tables to reflect this data manipulation operations.
4. Partition the BOOK Tables based on year of
publication. Demonstrate its working with the simple
query.
5. Create a view of all books and its no. of copies data
currently available in the library.
Solution: -
Table creation: -
BOOK
Downloaded by Rekha
BOOK_AUTHORS
PUBLISHERS
BOOK COPIES
Downloaded by Rekha
BOOK_LEADING
LIBRARY_BRANCH
CARD
Downloaded by Rekha
Insertion Of Values to tables: -
BOOK
BOOK_AUTHORS
Downloaded by Rekha
PUBLISHER
Downloaded by Rekha
BOOK_COPIES
BOOK_LEADING
LIBRARY_BRANCH
Downloaded by Rekha
CARD
Downloaded by Rekha
TABLE VIEW: -
Downloaded by Rekha
BOOK_AUTHOOR
Downloaded by Rekha
PUBLISHER
BOOK_COPIES
Downloaded by Rekha
BOOK_LEADING
LIBRARY_BRANCH
CARD
Downloaded by Rekha
11. Write a PL/SQL code to add two numbers and display the result. Read
the numbers during run time declare
x number(5);
y number(5);
z:=x+y; dbms_output.put_line('Sum
is '||z); end;
OUTPUT:
Downloaded by Rekha
12. Write a PL/SQL code to find sum of first 10 natural numbers using
while and for loop.
DECLARE x
NUMBER; n
NUMBER; i
NUMBER;
FUNCTION Findmax(n IN NUMBER)
RETURN NUMBER
IS
sums NUMBER := 0;
BEGIN
FOR i IN 1..n
LOOP
sums := sums + i*(i+1)/2;
END LOOP;
RETURN sums;
END;
BEGIN
n := 4; x :=
findmax(n);
dbms_output.Put_line('Sum: '
|| x);
END;
OUTPUT:
Downloaded by Rekha
13. Write a program to create a trigger which will convert the name of a student
to upper case before inserting or updating the name column of student table.
CREATE TRIGGER insert_upper_case_name
BEFORE INSERT or UPDATE
ON salesman
FOR EACH ROW
begin
:new.name := upper( :new.name );
end;
OUTPUT:
Downloaded by Rekha
14. Write a PL/SQL block to count the number of rows affected by an
update statement using SQL%ROWCOUNT
CREATE:
)
INSERT:
BEGIN
INSERT INTO plch_flowers
VALUES (1, 'Orchid');
INSERT INTO plch_flowers
VALUES (2, 'Rose');
COMMIT;
Downloaded by Rekha
END;
EXECUTION:
DECLARE:
l_id INTEGER;
BEGIN
SELECT id
INTO l_id
FROM plch_flowers
WHERE nm = 'Orchid';
DBMS_OUTPUT.put_line ('RC=' || SQL%ROWCOUNT);
END;
OUTPUT:
Downloaded by Rekha
15. Write a PL/SQL block to increase the salary of all doctors by 1000.
CREATE:
create table Doctor ( Dr_ID
varchar (10),
Dr_Name varchar (20),
Age number (9),
Address varchar (25),
Salary number (12)
);
INSERT:
BEGIN
INSERT INTO Doctor
Downloaded by Rekha
VALUES ('1', 'Orchid',30,'London',25000);
COMMIT;
END;
EXECUTION:
DECLARE
Downloaded by Rekha
total_rows number(2);
BEGIN
UPDATE Doctor
SET salary = salary + 1000; IF
sql%notfound THEN
OUTPUT:
Downloaded by Rekha
Downloaded by Rekha