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

dbms-file-1

The document provides an overview of SQL commands used for database management, including CREATE, ALTER, RENAME, DROP, INSERT, DELETE, and UPDATE statements. It also covers the use of SELECT statements, aggregate functions, substring comparison, and ordering data. Additionally, it includes examples of PL/SQL code for various operations such as triggers, counting affected rows, and updating records.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

dbms-file-1

The document provides an overview of SQL commands used for database management, including CREATE, ALTER, RENAME, DROP, INSERT, DELETE, and UPDATE statements. It also covers the use of SELECT statements, aggregate functions, substring comparison, and ordering data. Additionally, it includes examples of PL/SQL code for various operations such as triggers, counting affected rows, and updating records.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 34

DBMS FILE 1

organizational behaviour (Vignan’S Institute of Management and


Technology for Women)

Scan to open on Studocu

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).

I. CREATE TABLE Command:

• The CREATE TABLE statement is used to create a new table in a database.


• The column parameters specify the names of the columns of the table.
• The datatype parameter specifies the type of data the column can hold (e.g. varchar,
integer, date, etc.).

Syntax: -
CREATE TABLE table name
( column1 datatype,
column2 datatype,
column3 datatype,
....
);

Example: -

OUTPUT:-

II. ALTER TABLE Command:-

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:-

SQL> Alter table Person1

2 Add DOB date;

OUTPUT:-

III. RENAME TABLE Command:-

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:-

IV. DROP TABLE Command:-


The DROP TABLE statement is used to drop an existing table in a database.

Syntax:-
DROP TABLE table_name;

Example:-
Drop table Person1;

OUTPUT:-
Table Dropped

2. Used of INSERT INTO, DELETE, and UPDATE statement in the


database tables(relations).

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:-

SQL> insert into Person1(PersonId,LastName,Firstname,Address,City)


2 Values('1','kaur','Simran','Phullowal','Ludhiana');

1 row created.

SQL> insert into Person1(PersonID,LastName,FirstName,Address,City)


2 Values('2','Dhillod','Ekta','Model Town','Ludhiana');

1 row created.

SQL> insert into


Person1(PersonId,LastName,FirstName,Address,City) 2
Values('3','Soreng','Isha','BRS Nagar','LUdhiana');

1 row created.

SQL> insert into


Person1(PersonId,LastName,FirstName,Address,City) 2
Values('4','Rana','Muskan','Railway colony','Ludhiana');

1 row created.

SQL> insert into Person1(PersonId,LastName,FirstName,Address,City)


2 Values('5','Kujur','Aruna','Daler Nagar','Ludhiana');

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.

SQL> insert into Person1(PersonId,LastName,FirstName,Address,City)


2 Values('7','Gupta','Namita','Sarabha Nagar','Ludhiana');

1 row created.

SQL> insert into


Person1(PersonId,LastName,FirstName,Address,City) 2
Values('8','Gautam','Nitasha','Gurdev Nagar','Ludhiana');

1 row created.

OUTPUT:-

II. DELETE Command:-

The DELETE statement is used to delete existing records in a table.

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.

III. UPDATE Command:-

The UPDATE statement is used to modify the existing records in a table.

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:-

3. Use of Simple SELECT statement.

o The SELECT statement is used to select data from a database.


o The data returned is stored in a result table, called the result-set.

Downloaded by Rekha
Syntax:-

SELECT * FROM table_name;

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:-

COUNT () – returns the number of rows in a table or a group.


Syntax:-
COUNT(expression)

Example:-

OUTPUT:-

II. SUM:-

SUM () – returns the sum of the values of a numeric column.

Syntax:-
SUM(expression)

Example:-

OUTPUT:-

Downloaded by Rekha
III. MAX:-

MAX () – returns the maximum value of a selected column.

Syntax:-
MAX(expression)

Example:-

OUTPUT:-

Downloaded by Rekha
7. Use of substring comparison.

The SUBSTRING () function extracts some characters from a string.

Syntax: -
SUBSTRING (string, start, length)

Parameter Values

Parameter Description

string Required. The string to extract from

start Required. The start position. The first position in string is 1

Required. The number of characters to extract. Must be a positive


length
number

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:-

SELECT * FROM table_name ORDER BY column_name ASC | DESC

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 number(7); begin x:=10; y:=20;

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:

CREATE TABLE plch_flowers


(
id INTEGER PRIMARY KEY, nm

VARCHAR2 (100) UNIQUE

)
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);

INSERT INTO Doctor


VALUES ('2','Rose',25,'Paris',20000);

INSERT INTO Doctor


VALUES ('3','Jennie',21,'Busan',21000);

INSERT INTO Doctor


VALUES ('4','Lisa',24,'Thailand',22000);

INSERT INTO Doctor


VALUES ('5','Yoongi',29,'Seoul',30000);

INSERT INTO Doctor


VALUES ('6','Jin',30,'Sari',32000);

COMMIT;
END;

Select * from Doctor;

EXECUTION:
DECLARE

Downloaded by Rekha
total_rows number(2);

BEGIN

UPDATE Doctor
SET salary = salary + 1000; IF
sql%notfound THEN

dbms_output.put_line('no Doctor updated');


ELSIF sql%found THEN total_rows := sql
%rowcount;

dbms_output.put_line( total_rows || ' Doctor updated ');


END IF;
END;
/

OUTPUT:

Downloaded by Rekha
Downloaded by Rekha

You might also like