introduction to sql ii

Upload: cahnpek

Post on 30-May-2018

223 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Introduction to SQL II

    1/30

    LECTURE 1

    Introduction to SQL II

    Data Definition Language (DDL) Data Manipulation Language (DML)

  • 8/14/2019 Introduction to SQL II

    2/30

    DDL WITH TABLES.

    Creating Tables with Table constraints

    Modifying Table structures

  • 8/14/2019 Introduction to SQL II

    3/30

    Create Table using table and column

    constraints Constraints enforce rules on data whenever a row is

    inserted, updated, or deleted from a table. The constraints

    have to be satisfied for the operation to succeed.

    Data Integrity Constraints:

    Not null: specifies that the column cannot contain a

    null value.

    Unique: specifies that a column or combination ofcolumns whose values must be unique for all rows in

    the table.

  • 8/14/2019 Introduction to SQL II

    4/30

    Create Table using table and column

    constraints contd Check: specifies a condition that must be true

    Primary key: uniquely identifies each row of the table

    Foreign key: establishes and enforces a foreign key

    relationship between the column and a column of the

    referenced table.

    Constraints can be divided at one of the two levels

    i. Column constraint:

    References a single column and is defined within

    the column definition.

    Can define any type of integrity constraint.

  • 8/14/2019 Introduction to SQL II

    5/30

    o Table constraint:

    References one or more columns and is defined

    after the column list. Can define any constraint except not null.

    It is an issue of style to define constraints at either tablelevel or column level; however, the not null constraint

    must be strictly defined at the column level.

    Create Table using table and column

    constraints Contd

  • 8/14/2019 Introduction to SQL II

    6/30

    Example: [using column constraints]

    create table students (regNo varchar(15) primary key, name

    varchar(20), dob date, gender char(1) not null);

    OR

    [using table constraints]

    create table students (regNo varchar(15), name varchar(20),

    dob date, gender char(1) not null, constraintstudents_regNo_pk primary key(regNo));

    How would you confirm constraints defined on a table?

    Create Table using table and column

    constraints Contd

  • 8/14/2019 Introduction to SQL II

    7/30

    Modifying a Table Structure

    Modifying a table involves using the ALTER TABLE

    statement which could cater for three kinds of adjustments:

    MODIFY column (modify [data type/size][not null]

    [default]) ADD column(s) / constraint(s),

    DROP column(s) / constraint(s)

    Examples:

    alter table students add weight float (3,1);

    alter table students add height float (3,1);;

    alter table students modify weight int;

  • 8/14/2019 Introduction to SQL II

    8/30

    Modifying a Table Structure contd alter table students drop column height;

    alter table students modify gender char(1) default F;

    Alter table students add constraint students_gender_ck

    check (gender in (M,F)); Alter table students add constraint students_name_uk

    unique (name);

    alter table students drop key students_name_uk;

  • 8/14/2019 Introduction to SQL II

    9/30

    Notes:

    The not null constraint is added by modifying the column

    that is to be defined as not null

    Example:

    ALTER TABLE students

    MODIFY weight int not null;

    Adjustments to populated tables is more restrictive

    because the adjustments should not violet the nature of

    data stored in the tables.

    Modifying a Table Structure Contd

  • 8/14/2019 Introduction to SQL II

    10/30

    Removing a Table from a DB schema

    Example:

    DROP TABLE students;

  • 8/14/2019 Introduction to SQL II

    11/30

    Quiz Write all SQL statements that will create the relational DB schema:

    DEPTS (DeptNo, DName, Loc)

    PERSONNEL (EmpNo, EName, Position, *Mgr, HireDate, Sal,Comm, *DeptNo)

    Data Constraints: A department name value must never be repeated.

    No person should earn a salary that is more than 5000.

    Note:

    A foreign key is preceded by an asterisk (*); Mgr references EmpNo.

  • 8/14/2019 Introduction to SQL II

    12/30

    Quiz Contd

    1. Assuming that Sex has data type char, modify the personnel

    table such that it can accept values of varied lengths.

    2. Modify the staff table such that each staff member has a

    position.

  • 8/14/2019 Introduction to SQL II

    13/30

    Manipulating Data In a Table

    UPDATE statement

    DELETE Statement

  • 8/14/2019 Introduction to SQL II

    14/30

    DML Overview This is a language that provides a set of operations to

    support the basic data manipulation operations on thedata held in the database.

    There are many types of DML statements, these include;

    INSERT to add data/new rows to a table

    UPDATE to make changes to existing data/rowsin a table

  • 8/14/2019 Introduction to SQL II

    15/30

    DML Overview contd SELECT to query data in the database (could involve

    one table or many tables to be covered in another learning unit)

    DELETE to remove data from a table/remove existingrows from a table

    Therefore some of the operations performed are insertion,modification, retrieval and deletion.

  • 8/14/2019 Introduction to SQL II

    16/30

    Create a table called emp_table with following attributes:

    Example to be used

  • 8/14/2019 Introduction to SQL II

    17/30

    Statement for creating emp_tablecreate table emp_table (empId varchar(20), fName

    varchar(15), lName varchar(15) not null, dob date,

    constraint emp_empid_pk primary key(empId));

  • 8/14/2019 Introduction to SQL II

    18/30

    The Update Operation The Update operation is used to modify existing rows in a

    database.

    The general syntax of an update statement is;

    UPDATE table

    SET column = value [,column = value,]

    [WHERE condition];

  • 8/14/2019 Introduction to SQL II

    19/30

    Specific value(s) are modified if you specify the WHERE

    clause in the UPDATE statement. This is mainly by the

    use of the primary key because any other column name

    may cause several rows to be updated unexpectedly .

    Example:

    UPDATE emp_table

    SET lname = 'nakato'

    WHERE empid = '93/3/3';

    The Update Operation

  • 8/14/2019 Introduction to SQL II

    20/30

    The Update Operation contd

    All rows in the table are modified if you omit the where

    clause.

    Example:

    UPDATE emp_tableSET lname= 'nakato';

    Updating rows with values with that are tied to intergrity

    constraints usually returns an error when the constraint is

    violated. Example when updating a foreign key.

  • 8/14/2019 Introduction to SQL II

    21/30

    Updating with the use of a Subquery

    A subquery may be used to update a row or rows in a given

    relation

    Example:

    UPDATE emp_copySET job_id='SAL_PER',

    salary = (select salary

    from employees

    where employee_id = 100)

    WHERE job_id = 'FI_ACCOUNT';

  • 8/14/2019 Introduction to SQL II

    22/30

    The Update Operation contd

    Updating multiple columns using a subquery; The general syntax is

    UPDATE table

    SET column = ( SELECT columnFROM table

    WHERE condition)

    [,

    column = ( SELECT column

    FROM table

    WHERE condition)]

    [WHERE condition];

  • 8/14/2019 Introduction to SQL II

    23/30

  • 8/14/2019 Introduction to SQL II

    24/30

    The Delete Operation

    Specific row(s) are deleted if you specify the WHERE

    clause in the DELETE FROM statement.

    Example:

    DELETE FROM emp_copyWHERE job_id= SAL_PER;

    Take care, if a WHERE clause is not specified, then all

    rows from the table will be deleted.Example:

    DELETE FROM emp_copy;

  • 8/14/2019 Introduction to SQL II

    25/30

    Deleting Rows based on Another Table This is usually done using a Subquery

    Example:

    DELETE FROM emp_copy

    WHERE department_id = (SELECT department_id

    FROM departments

    WHERE location_id=2400);

  • 8/14/2019 Introduction to SQL II

    26/30

    Deleting Rows: Integrity constraint Error

    You can not delete a row that contains a primary key that is

    used as a foreign key in another table.

    Example:

    DELETE FROM departments

    WHERE department_id = 60;

    Error: child record found violation

  • 8/14/2019 Introduction to SQL II

    27/30

    Common Integrity Constraint Errors

    Leaving out values for columns with NOT NULLconstraints.

    Deleting a parent record that has dependencies on it.

    Example:DELETE FROM dept_copyWHERE department_id = 20;

    Inserting or updating a record with a foreign key valuethat does not exist in the referenced primary or unique

    key of the parent table. Example:UPDATE emp_copySET department_id = 55WHERE department_id = 110;

  • 8/14/2019 Introduction to SQL II

    28/30

  • 8/14/2019 Introduction to SQL II

    29/30

    Primary keys and Foreign key constraints to be added to the

    tables created:

    Add a primary key constraint on the emp_copy table.

    Add a primary key constraint on the dept_copy table

    Add a foreign key constraint on the dept_copy tablewhich references the primary key of the emp_copy

    table.

    ExerciseContd

  • 8/14/2019 Introduction to SQL II

    30/30

    ExerciseContd statements for adding

    the constraints alter table emp_copy add constraint

    emp_copy_employee_id_pk primary key(employee_id);

    alter table dept_copy add constraintdept_copy_department_id_pk primary key(department_id);

    alter table emp_copy add constraint

    emp_copy_department_id_uk foreign key(department_id)references dept_copy(department_id);