Queries_Class of DB

Embed Size (px)

Citation preview

  • 8/7/2019 Queries_Class of DB

    1/32

    1

    Create, Delete and UpdateCreate, Delete and UpdateLab 01Lab 01

  • 8/7/2019 Queries_Class of DB

    2/32

    2

    QUICK VIEW OF SQLSERVER 2005

  • 8/7/2019 Queries_Class of DB

    3/32

    3

  • 8/7/2019 Queries_Class of DB

    4/32

    4

  • 8/7/2019 Queries_Class of DB

    5/32

    5

  • 8/7/2019 Queries_Class of DB

    6/32

    6

  • 8/7/2019 Queries_Class of DB

    7/32

    7

    Data types inData types in

    SQL PlusSQL Plus

    char(n): Fixed-length character data (string), n characters long. Themaximum size for n 2000 in Oracle8. Example: char(40)

    varchar 2(n): Variable-length character string. The maximum size for n is 4000in Oracle8. Example: varchar2(80).

    number(o, d): Numeric data type for integers and reals. o = overall number ofdigits, 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 than999.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 of2GB.

  • 8/7/2019 Queries_Class of DB

    8/32

    8

    Using SQLUsing SQL

    DESCRIBE ;DESCRIBE ;

    DESCRIBE statement is used to display

    the columns of a table and their data

    types means the structure of table.

  • 8/7/2019 Queries_Class of DB

    9/32

    9

    Using SQLUsing SQL

    COMMIT;COMMIT;

    ROLLBACK;ROLLBACK;

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

  • 8/7/2019 Queries_Class of DB

    10/32

    10

    Using SQLUsing SQL

    Select * from tab;Select * from tab;

    TAB is a table in Oracle. TAB table

    contains a list of all of the tables in a

    database.

  • 8/7/2019 Queries_Class of DB

    11/32

    11

    Using SQLUsing SQL

    To create a table in the current database,

    use the CREATE TABLE keyword

    create table ( [not null] [unique] [],

    . . . . . . . . .

    [not null] [unique] [],

    []

    );

    [constraint ] primary key | unique | not null

  • 8/7/2019 Queries_Class of DB

    12/32

    12

    ExampleExample

    CREATE TABLE authorsCREATE TABLE authors

    (auth_id int(9) not null,(auth_id int(9) not null,

    auth_name char(40) not null)auth_name char(40) not null)

    auth_id auth_name

    (9 digit int) (40 char string)

  • 8/7/2019 Queries_Class of DB

    13/32

    13

    Using SQLUsing SQL

    To insert data in the current table, use

    the keyword INSERT INTO

    INSERT INTO [()]

    values ();

  • 8/7/2019 Queries_Class of DB

    14/32

    14

    ExampleExample

    INSERT INTO authors

    values(00001, John Smith)

    INSERT INTO authors

    values(00002, Jane Doe)

    auth_id auth_name

    00001 John Smith

    00002 Jane Doe

    Then issue the statement

    SELECT * FROM authors

  • 8/7/2019 Queries_Class of DB

    15/32

    15

    Using SQLUsing 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) Youll set prompts for entering values of cname

    and city.

    Enter the value of cname:- GSK

    Enter the value of city:- Karachi

  • 8/7/2019 Queries_Class of DB

    16/32

    16

    Using SQLUsing SQL

    You enter the above values and press enter you

    will get message

    Record created.

    If you want to insert another record in sametable just type RUN at SQL prompt as:

    Run;

  • 8/7/2019 Queries_Class of DB

    17/32

    17

    Using SQLUsing SQL

    DELETE from [whereDELETE from [where

    ];];

    To delete data from a table, use

    the DELETE statement:

  • 8/7/2019 Queries_Class of DB

    18/32

    18

    ExampleExample

    auth_id auth_name

    00002 Jane Doe

    000000001 John Smith

    DELETE from authorsDELETE from authors

    WHERE auth_name=John SmithWHERE auth_name=John Smith

  • 8/7/2019 Queries_Class of DB

    19/32

    19

    Using SQLUsing SQL

    UPDATE setUPDATE set

    = , . . = , . .

    . , = . , =

    [where ];[where ];

    To Update information in a database use

    the UPDATE keyword

  • 8/7/2019 Queries_Class of DB

    20/32

    20

    ExampleExample

    auth_id auth_name

    00001 Hello

    00002 Hello

    UPDATE authorsUPDATE authors

    SET auth_name=helloSET auth_name=hello

    Sets all auth_name fields to helloSets all auth_name fields to hello

  • 8/7/2019 Queries_Class of DB

    21/32

    21

    Using SQLUsing SQL

    ALTER TABLE ALTER TABLE

    ADD( ADD(

    [default ] []);

    To change a table in a database use ALTER

    TABLE. ADD adds a characteristic.

  • 8/7/2019 Queries_Class of DB

    22/32

    22

    ExampleExample

    auth_id auth_name

    00002 Jane Doe

    00001 John Smith

    ALTER TABLE authorsALTER TABLE authors

    ADD birth_date datetime nullADD birth_date datetime null

    ADD puts a new column in the table

    called birth_date

    birth_date

    .

    .

    Type Initializer

  • 8/7/2019 Queries_Class of DB

    23/32

    23

    Using SQLUsing SQL

    DROP TABLE [cascadeDROP TABLE [cascade

    constraints];constraints];

    The DROP command removes a table

    from the database. All the tables' rows,

    indexes and privileges will also beremoved

  • 8/7/2019 Queries_Class of DB

    24/32

    24

    ExampleExample

    auth_id auth_name auth_city auth_state

    123456789 Jane Doe Dearborn MI

    000000001 John Smith Taylor MI

    DROP TABLE authors;DROP TABLE authors;

  • 8/7/2019 Queries_Class of DB

    25/32

    25

    Using SQLUsing SQL

    TRUNCATE TABLE ;TRUNCATE TABLE ;

    The TRUNCATE command clears the

    records from the table but the table

    structure still remains. It can't roll back.

  • 8/7/2019 Queries_Class of DB

    26/32

    26

    Exercise

    We want to create a table called PROJECT to store informationabout projects. For each project, we want to store the numberand the name of the project, the employee number of theprojects manager, the budget and the number of personsworking 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 tablePROJECT, the project start date should be set to January 1st,2000.

  • 8/7/2019 Queries_Class of DB

    27/32

    27

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

  • 8/7/2019 Queries_Class of DB

    28/32

    28

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

  • 8/7/2019 Queries_Class of DB

    29/32

    29

    Foreign key Constraintcreate 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

    );

  • 8/7/2019 Queries_Class of DB

    30/32

    30

    Example 2CREATE 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;

  • 8/7/2019 Queries_Class of DB

    31/32

    31

    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' wheresupplier_name='Ali'

    delete from supplier where supplier_name='ABC'

    select * from products

    select * from supplier

  • 8/7/2019 Queries_Class of DB

    32/32

    32

    Answer of Queries given in paper

    1. select Distinct s.sname from student s, classc, 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