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

Queries - Class of DB

This document provides an overview of SQL (Structured Query Language) concepts including data types, creating and modifying database tables, inserting and updating data, and joining tables in queries. Key topics covered include the DESCRIBE statement to view table structures, the INSERT statement to add data to tables, the UPDATE and DELETE statements to modify data, the ALTER TABLE statement to modify table structures, and examples of creating tables and relationships between tables using primary and foreign keys. Constraints, transactions, and other SQL features are demonstrated through examples. Exercises at the end provide examples of creating a table with constraints and answering queries against multiple joined tables.

Uploaded by

syma masood
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Queries - Class of DB

This document provides an overview of SQL (Structured Query Language) concepts including data types, creating and modifying database tables, inserting and updating data, and joining tables in queries. Key topics covered include the DESCRIBE statement to view table structures, the INSERT statement to add data to tables, the UPDATE and DELETE statements to modify data, the ALTER TABLE statement to modify table structures, and examples of creating tables and relationships between tables using primary and foreign keys. Constraints, transactions, and other SQL features are demonstrated through examples. Exercises at the end provide examples of creating a table with constraints and answering queries against multiple joined tables.

Uploaded by

syma masood
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

QL

Create, Delete and Update


Lab 01

1
QUICK VIEW OF SQL
SERVER 2005

2
3
4
5
6
L Data types in
SQ SQL Plus
• char(n): Fixed-length character data (string), n characters long. The
maximum size for n 2000 in Oracle8. Example: char(40)
• varchar2(n): Variable-length character string. The maximum size for n is 4000
in Oracle8. Example: varchar2(80).
• number(o, d): Numeric data type for integers and reals. o = overall number of
digits, d = number of digits to the right of the decimal point. Examples: number(8),
number(5,2). Note that, e.g., number(5,2) cannot contain anything larger than
999.99 without resulting in an error. Data types derived from number are int[eger],
dec[imal], smallint and real.
• date: Date data type for storing date and time. The default format for a date is: DD-MMM-YY. Examples:
’13-OCT-94’, ’07-JAN-98’.

• long: Character data up to a length of 2GB.

7
Using SQL

DESCRIBE statement is used to display


the columns of a table and their data
types means the structure of table.

DESCRIBE <table name> ;

8
Using SQL
A sequence of database modifications, i.e. insert,
update, and delete statements is called a
TRNASACTION. COMMIT makes permanent any
database changes you made during the current
transaction. As long as the user has not issued the
commit statement, it is possible to undo all modifications
since the last commit. To undo modifications, one has to
issue the statement ROLLBACK.

COMMIT;
ROLLBACK;
9
Using SQL

TAB is a table in Oracle. TAB table


contains a list of all of the tables in a
database.

Select * from tab;

10
Using SQL
• To create a table in the current database,
use the CREATE TABLE keyword
create table <table> (
<column 1> <data type> [not null] [unique] [<column
constraint>],
.........
<column n> <data type> [not null] [unique] [<column
constraint>],
[<table constraint(s)>]
);
[constraint <name>] primary key | unique | not null
11
Example
CREATE TABLE authors
(auth_id int(9) not null,
auth_name char(40) not null)

auth_id auth_name
(9 digit int) (40 char string)

12
Using SQL
• To insert data in the current table, use
the keyword INSERT INTO

INSERT INTO <table> [(<column i, . . . , column j>)]


values (<value i, . . . , value j>);

13
Example
INSERT INTO authors
values(‘00001’, ‘John Smith’)

INSERT INTO authors


values(‘00002’, ‘Jane Doe’)

• Then issue the statement


SELECT * FROM authors

auth_id auth_name
00001 John Smith
00002 Jane Doe
14
Using SQL
• If you want to enter 100 records then you can avoid to
write insert statement 100 times by making use of
substitution variable.

INSERT INTO company VALUES (&cname, &city)


• You’ll set prompts for entering values of cname
and city.

Enter the value of cname:- ‘GSK’


Enter the value of city:- ‘Karachi’

15
Using SQL
• You enter the above values and press enter you
will get message
Record created.

• If you want to insert another record in same


table just type RUN at SQL prompt as:

Run;

16
Using SQL
To delete data from a table, use
the DELETE statement:

DELETE from <table> [where <condition>];

17
Example

DELETE from authors


WHERE auth_name=‘John Smith’

auth_id auth_name
00002 Jane Doe
000000001 John Smith

18
Using SQL
To Update information in a database use
the UPDATE keyword

UPDATE <table> set


<column i> = <expression i>, . . . , <column j> =
<expression j>
[where <condition>];

19
Example

UPDATE authors
SET auth_name=‘hello’

auth_id auth_name
00001 Hello
00002 Hello

Sets all auth_name fields to hello

20
Using SQL
To change a table in a database use
ALTER TABLE. ADD adds a characteristic.

ALTER TABLE <table>


ADD(<column> <data type> [default <value>]
[<column constraint>]);

21
Example
Type Initializer
ALTER TABLE authors
ADD birth_date datetime null

auth_id auth_name birth_date


00002 Jane Doe .
00001 John Smith .

ADD puts a new column in the table


called birth_date
22
Using SQL
The DROP command removes a table
from the database. All the tables' rows,
indexes and privileges will also be
removed
DROP TABLE <table> [cascade
constraints];

23
Example

DROP TABLE authors;

auth_id auth_name auth_city auth_state


123456789 Jane Doe Dearborn MI
000000001 John Smith Taylor MI

24
Using SQL
The TRUNCATE command clears the
records from the table but the table
structure still remains. It can't roll back.

TRUNCATE TABLE <table>;

25
Exercise
We want to create a table called PROJECT to store information
about projects. For each project, we want to store the number
and the name of the project, the employee number of the
project’s manager, the budget and the number of persons
working on the project, and the start date and end date of the
project. Furthermore, we have the following conditions:
• a project is identified by its project number,
• the name of a project must be unique,
• the manager and the budget must be defined.
• no two projects have the same start and end date.
• If no start date is given when inserting a tuple into the table
PROJECT, the project start date should be set to January 1st,
2000.

26
Table Definition

create table PROJECT (


PNO number(3) constraint prj_pk primary key,
PNAME varchar2(60) unique,
PMGR number(4) not null,
PERSONS number(5),
BUDGET number(8,2) not null,
PSTART date default(’01-JAN-00’),
PEND date,
constraint no_same_dates unique (PEND, PSTART) );

27
INSERTION

insert into PROJECT(PNO, PNAME, PERSONS,


BUDGET, PSTART)
values(313, ’DBS’, 4, 150000.42, ’10-OCT-94’);
OR
insert into PROJECT
values(313, ’DBS’, 7411, null, 150000.42, ’10-
OCT-94’, null);

28
Foreign key Constraint
create table DEPARTMENT(
departt_id number(5) primary key,
departt_name varchar2(20) not null,
constraint departt unique(departt_name)
);
create table DESIGNATION(
desig_id number(5) primary key,
desig_name varchar2(20) not null,
constraint desig unique(desig_name)
);
create table VACANCY(
vac_id number(5) primary key,
vac_exp varchar2(20),
desig_id number(5) constraint fk_con references DESIGNATION(desig_id),
departt_id number(5) references DEPARTMENT
);
29
Example 2
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar(50) not null,
contact_name varchar(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id, supplier_name) );

CREATE TABLE products


( product_id numeric(10) not null,
supplier_id numeric(10) not null,
supplier_name varchar(50) not null );

ALTER TABLE products


add CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id, supplier_name)
REFERENCES supplier(supplier_id, supplier_name)
ON DELETE CASCADE
ON UPDATE cascade;

30
INSERT INTO supplier VALUES (1,'Ali','Ali')
INSERT INTO supplier VALUES (2,'Sara','Sara')

INSERT INTO products VALUES (1,1,'Ali')


INSERT INTO products VALUES (2,2,'Sara')

update supplier set supplier_name='ABC' where


supplier_name='Ali'

delete from supplier where supplier_name='ABC'


select * from products
select * from supplier
31
Answer of Queries given in paper
1. select Distinct s.sname from student s, class
c, Enrolled e where s.snum=E.snum and
e.cname=c.name and c.name=‘database' and
s.level = 'JR‘
OR

1. select distinct s.sname from student s,


enrolled e where s.level=‘JR' and
s.snum=e.snum and e.cname=‘database'
2. select s.level, avg(s.age) from student s
group by s.level

32

You might also like