Zia - Lecture - 24-April-2014.pdf

Embed Size (px)

Citation preview

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    1/48

    DBMS

    Lectures

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    2/48

    Contents

    Create Database

    Create Table

    Alter Table Create Constraints

    Create index

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    3/48

    Create Database

    The CREATE DATABASE statement is used to

    create a database.

    SQL CREATE DATABASE Syntax

    Example

    CREATE DATABASE dbname;

    CREATE DATABASE student_db;

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    4/48

    Create Table

    The CREATE TABLE statement is used to create

    a table in a database.

    Tables are organized into rows and columns;

    and each table must have a name.

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    5/48

    Create Table

    SQL CREATE TABLE Syntax

    CREATE TABLE table_name

    (column_name1 data_type(size),

    column_name2 data_type(size),

    column_name3 data_type(size),....

    );

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    6/48

    Create Table

    Example

    CREATE TABLE Persons

    (PersonID int,

    LastName varchar(255),

    FirstName varchar(255),

    Address varchar(255),

    City varchar(255)

    );

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    7/48

    Alter Table

    The ALTER TABLE statement is used to:

    Add

    Delete

    Modify

    the columns in an existing table.

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    8/48

    Alter Table

    To add a column in a table, use the following

    syntax:

    Example:

    ALTER TABLE table_nameADD column_name datatype

    ALTER TABLE Persons

    ADD DateOfBirth date

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    9/48

    Alter Table

    To delete a column in a table, use the

    following syntax:

    Example:

    ALTER TABLE table_nameDROP COLUMN column_name

    ALTER TABLE Persons

    DROP COLUMN DateOfBirth

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    10/48

    Alter Table

    To modify the data type of a column in a table,

    use the following syntax:

    Example:

    ALTER TABLE table_nameMODIFY column_name datatype

    ALTER TABLE Persons

    MODIFY DateOfBirth varchar(255)

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    11/48

    Alter Table

    Change/modify SQL Server / MS Access:

    My SQL / Oracle:

    Oracle 10G and later:

    ALTER TABLE table_name

    ALTER COLUMN column_name datatype

    ALTER TABLE table_name

    MODIFY COLUMN column_name datatype

    ALTER TABLE table_name

    MODIFY column_name datatype

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    12/48

    SQL Constraints

    SQL constraints are used to specify rules forthe data in a table.

    If there is any violation between the

    constraint and the data action, the action isaborted by the constraint.

    Constraints can be specified when the table is

    created (inside the CREATE TABLE statement)or after the table is created (inside the ALTERTABLE statement).

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    13/48

    SQL Constraints

    NOT NULL

    UNIQUE

    PRIMARY KEY FOREIGN KEY

    CHECK

    DEFAULT

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    14/48

    SQL Constraints

    SQL CREATE TABLE + CONSTRAINT Syntax

    CREATE TABLE table_name(

    column_name1 data_type(size) constraint_name,

    column_name2 data_type(size) constraint_name,

    column_name3 data_type(size) constraint_name,....

    );

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    15/48

    NOT NULL - Constraint

    NOT NULL - Indicates that a column cannot

    store NULL value

    The NOT NULL constraint enforces a field to

    always contain a value.

    This means that you cannot insert a new

    record, or update a record without adding a

    value to this field.

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    16/48

    NOT NULL - Constraint

    Example

    CREATE TABLE PersonsNotNull

    (P_Id int NOT NULL,

    LastName varchar(255) NOT NULL,

    FirstName varchar(255),

    Address varchar(255),

    City varchar(255)

    )

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    17/48

    UNIQUE - Constraints

    UNIQUE - Ensures that each row for a column

    must have a unique value

    OR

    The UNIQUE constraint uniquely identifies

    each record in a database table.

    Note: You can have many UNIQUE constraints

    per table

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    18/48

    UNIQUE - Constraints

    Example

    CREATE TABLE Persons

    (

    P_Id int NOT NULL UNIQUE,

    LastName varchar(255) NOT NULL,

    FirstName varchar(255),

    Address varchar(255),City varchar(255)

    )

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    19/48

    UNIQUE - Constraints

    Example

    CREATE TABLE Persons

    (P_Id int NOT NULL,

    LastName varchar(255) NOT NULL,

    FirstName varchar(255),

    Address varchar(255),City varchar(255),

    CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)

    )

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    20/48

    UNIQUE Constraints Alter Table

    To create a UNIQUE constraint on the "P_Id"

    column when the table is already created, use

    the following SQL:

    ALTER TABLE Persons

    ADD UNIQUE (P_Id)

    ALTER TABLE Persons

    ADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    21/48

    UNIQUE Constraints Alter Table

    To drop a UNIQUE constraint, use the

    following SQL:

    ALTER TABLE Persons

    DROP CONSTRAINT uc_PersonID

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    22/48

    PRIMARY KEY - Constraints

    PRIMARY KEY - A combination of a NOT NULL

    and UNIQUE.

    Ensures that a column (or combination of two

    or more columns) have an unique identity

    which helps to find a particular record in a

    table more easily and quickly

    Each table should have a primary key, and

    each table can have only ONE primary key.

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    23/48

    PRIMARY KEY - Constraints

    Example

    CREATE TABLE Persons

    (

    P_Id int NOT NULL PRIMARY KEY,

    LastName varchar(255) NOT NULL,

    FirstName varchar(255),

    Address varchar(255),City varchar(255)

    )

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    24/48

    PRIMARY KEY - Constraints

    Example

    CREATE TABLE Persons

    (

    P_Id int NOT NULL,LastName varchar(255) NOT NULL,

    FirstName varchar(255),

    Address varchar(255),

    City varchar(255),CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)

    )

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    25/48

    PK Constraints Alter Table

    To create a PRIMARY KEY constraint on the

    "P_Id" column when the table is already

    created, use the following SQL:

    ALTER TABLE Persons

    ADD PRIMARY KEY (P_Id)

    ALTER TABLE Persons

    ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    26/48

    PK Constraints Alter Table

    To drop a PRIMARY KEY constraint, use the

    following SQL:

    ALTER TABLE Persons

    DROP CONSTRAINT pk_PersonID

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    27/48

    FOREIGN KEY - Constraints

    FOREIGN KEY - Ensure the referential integrity

    of the data in one table to match values in

    another table

    A FOREIGN KEY in one table points to a

    PRIMARY KEY in another table.

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    28/48

    FOREIGN KEY - Constraints

    Example

    CREATE TABLE Orders

    (

    O_Id int NOT NULL PRIMARY KEY,

    OrderNo int NOT NULL,

    P_Id int FOREIGN KEY REFERENCES Persons(P_Id)

    )

    P_Id LastName FirstName

    1 Ali Muhammad

    2 Khan Ahmad

    3 Aslam Muhammad

    O_Id OrderNum P_Id

    1 456 2

    2 457 3

    3 458 1

    Persons Orders

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    29/48

    FOREIGN KEY - Constraints

    ExampleCREATE TABLE Orders

    (

    O_Id int NOT NULL,

    OrderNo int NOT NULL,

    P_Id int,

    PRIMARY KEY (O_Id),

    CONSTRAINT fk_PerOrders FOREIGN KEY (P_Id)

    REFERENCES Persons(P_Id)

    )

    P_Id LastName FirstName

    1 Ali Muhammad

    2 Khan Ahmad

    3 Aslam Muhammad

    O_Id OrderNum P_Id

    1 456 2

    2 457 3

    3 458 1

    Persons Orders

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    30/48

    FK Constraints Alter Table

    To create a FOREIGN KEY constraint on the

    "P_Id" column when the "Orders" table is

    already created, use the following SQL:

    ALTER TABLE Orders

    ADD FOREIGN KEY (P_Id)

    REFERENCES Persons(P_Id)

    ALTER TABLE OrdersADD CONSTRAINT fk_PerOrders

    FOREIGN KEY (P_Id) //multiple columns can be added

    REFERENCES Persons(P_Id)

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    31/48

    FK Constraints Alter Table

    To drop a FOREIGN KEY constraint, use the

    following SQL:

    ALTER TABLE Orders

    DROP CONSTRAINT fk_PerOrders

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    32/48

    CHECK - Constraints

    CHECK - Ensures that the value in a column

    meets a specific condition

    If you define a CHECK constraint on a single

    column it allows only certain values for this

    column.

    If you define a CHECK constraint on a table it

    can limit the values in certain columns based

    on values in other columns in the row.

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    33/48

    CHECK - Constraints

    Example:

    CREATE TABLE Persons

    (P_Id int NOT NULL CHECK (P_Id>0),

    LastName varchar(255) NOT NULL,

    FirstName varchar(255),

    Address varchar(255),City varchar(255)

    )

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    34/48

    CHECK Constraints Alter Table

    To create a CHECK constraint on the "P_Id"

    column when the table is already created, use

    the following SQL:

    ALTER TABLE Persons

    ADD CHECK (P_Id>0)

    ALTER TABLE Persons

    ADD CONSTRAINT chk_Person CHECK (P_Id>0 AND

    City=Islamabad')

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    35/48

    CHECK Constraints Alter Table

    To drop a CHECK constraint, use the following

    SQL:

    ALTER TABLE Persons

    DROP CONSTRAINT chk_Person

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    36/48

    DEFAULT - Constraints

    DEFAULT - Specifies a default value when

    specified none for this column

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    37/48

    DEFAULT - Constraints

    Example:

    CREATE TABLE Persons

    (P_Id int NOT NULL,

    LastName varchar(255) NOT NULL,

    FirstName varchar(255),

    Address varchar(255),City varchar(255) DEFAULT ISLAMABAD '

    )

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    38/48

    DEFAULT - Constraints

    Example:

    CREATE TABLE Orders

    (O_Id int NOT NULL,

    OrderNo int NOT NULL,

    P_Id int,

    OrderDate date DEFAULT GETDATE()

    )

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    39/48

    DEFAULT Constraints Alter Table

    To create a DEFAULT constraint on the "City"

    column when the table is already created, use

    the following SQL:

    ALTER TABLE Persons

    MODIFY City DEFAULT ISLAMABAD'

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    40/48

    DEFAULT Constraints Alter Table

    To drop a DEFAULT constraint, use the

    following SQL:

    ALTER TABLE Persons

    ALTER COLUMN City DROP DEFAULT

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    41/48

    Create index

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    42/48

    Create index

    An index can be created in a table to find data

    more quickly and efficiently.

    The CREATE INDEX statement is used to create

    indexes in tables.

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    43/48

    Create index

    SQL CREATE INDEX Syntax

    Creates an index on a table. Duplicate valuesare allowed:

    CREATE INDEX index_name

    ON table_name (column_name)

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    44/48

    Create index

    Example

    CREATE INDEX PIndex

    ON Persons (LastName)

    CREATE INDEX PIndex

    ON Persons (LastName, FirstName)

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    45/48

    Class Activity

    Make group of 3 people

    Normalize the date up to 3NF

    Time 30min

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    46/48

    Solution

    EMPLOYEE

    DEPARTMENT

    DEPT_LOCATION

    WORKS_ON

    PROJECT

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    47/48

    Class Activity

    ENAME,

    NIC,

    DOB,

    ADDRESS,

    DEPTLOC,

    DEPTNAME,

    DEPTNUM,

    DEPTTYPE,

    PROJNAME,

    PROJNUM,

    PROJLOC,

    WORKHOUR

  • 7/25/2019 Zia - Lecture - 24-April-2014.pdf

    48/48

    DEPTTYPE

    NIC

    NIC

    DOB