Queries - Class of DB
Queries - Class of DB
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’.
7
Using SQL
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
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
13
Example
INSERT INTO authors
values(‘00001’, ‘John Smith’)
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.
15
Using SQL
• You enter the above values and press enter you
will get message
Record created.
Run;
16
Using SQL
To delete data from a table, use
the DELETE statement:
17
Example
auth_id auth_name
00002 Jane Doe
000000001 John Smith
18
Using SQL
To Update information in a database use
the UPDATE keyword
19
Example
UPDATE authors
SET auth_name=‘hello’
auth_id auth_name
00001 Hello
00002 Hello
20
Using SQL
To change a table in a database use
ALTER TABLE. ADD adds a characteristic.
21
Example
Type Initializer
ALTER TABLE authors
ADD birth_date datetime null
23
Example
24
Using SQL
The TRUNCATE command clears the
records from the table but the table
structure still remains. It can't roll back.
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
27
INSERTION
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) );
30
INSERT INTO supplier VALUES (1,'Ali','Ali')
INSERT INTO supplier VALUES (2,'Sara','Sara')
32