SQL - Data Manipulation Language (DML)

Outline

DELETE statement

  • Functionality:
    • DELETE statement deletes all rows that satisfy a given condition
    • The rows deleted by DELETE statement CAN be restored by ROLLBACK statement unless DELETE has been committed by COMMIT statement
    • DELETE statement DOES NOT delete a table
    • DELETE statement DOES NOT release disk storage occupied by the deleted rows
    • DELETE FROM TABLE

In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

DELETE statement

  • Sample database:
      CREATE TABLE DEPARTMENT(
       name               VARCHAR(50)        NOT NULL,
       code               CHAR(5)            NOT NULL,
       total_staff_number DECIMAL(2)         NOT NULL,
       chair              VARCHAR(50)            NULL,
       budget             DECIMAL(9,1)       NOT NULL,
        CONSTRAINT dept_pkey PRIMARY KEY(name),
        CONSTRAINT dept_ckey1 UNIQUE(code),
        CONSTRAINT dept_ckey2 UNIQUE(chair),
        CONSTRAINT dept_check1 CHECK (total_staff_number BETWEEN 1 AND 50) );
       CREATE TABLE COURSE(
       cnum               CHAR(7)           NOT NULL,
       title              VARCHAR(200)      NOT NULL,
       credits            DECIMAL(2)        NOT NULL,
       offered_by         VARCHAR(50)           NULL,
        CONSTRAINT course_pkey PRIMARY KEY(cnum),
        CONSTRAINT course_check1 CHECK (credits IN (6, 12)),
        CONSTRAINT course_fkey1 FOREIGN KEY(offered_by)
                              REFERENCES DEPARTMENT(name) ON DELETE CASCADE );
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

DELETE statement

  • Examples:
    • Delete a course with a code CSCI235
    • DELETE FROM COURSE
      WHERE cnum = 'CSCI235';
    • Delete all courses with 12 credits or such that their title includes a word "database"
    • DELETE FROM COURSE
      WHERE (credits = 12) OR (UPPER(title) LIKE '%DATABASE%');
    • Delete all departments where total number of staff members is less than 5
    • DELETE FROM DEPARTMENT 
      WHERE total_staff_number < 5;
    • Delete all departments
    • DELETE FROM DEPARTMENT;
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

DELETE statement

  • BEWARE !!!
      CREATE TABLE COURSE(
       cnum               CHAR(7)           NOT NULL,
       title              VARCHAR(200)      NOT NULL,
       credits            DECIMAL(2)        NOT NULL,
       offered_by         VARCHAR(50)           NULL,
        CONSTRAINT course_pkey PRIMARY KEY(cnum),
        CONSTRAINT course_check1 CHECK (credits IN (6, 12)),
        CONSTRAINT course_fkey1 FOREIGN KEY(offered_by)
                              REFERENCES DEPARTMENT(name) );
      
      DELETE FROM DEPARTMENT WHERE name='Physics';
      
      --------------
      DELETE FROM DEPARTMENT WHERE name='Physics'
      --------------
      
      ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key 
      constraint fails (`csit115`.`COURSE`, CONSTRAINT `course_fkey1` 
      FOREIGN KEY (`offered_by`) REFERENCES `DEPARTMENT` (`name`))
      
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

DELETE statement

  • BEWARE !!!
    • If ON DELETE CASCADE clause is not used in a specification of a foreign key then an order in which the rows are deleted is important !!!
    • If ON DELETE CASCADE clause is used in a specification of a foreign key then deletion of a row with a respective value of primary key triggers automatic deletion of the rows with the same value of a foreign key
    • Otherwise, the rows with the same value of a foreign key must be deleted first
    • 
      DELETE FROM COURSE WHERE offered_by ='Physics';
      DELETE FROM DEPARTMENT WHERE name='Physics';
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

SQL - Data Manipulation Language (DML)

Outline

INSERT statement

  • Functionality:
    • INSERT statement inserts a new row into a relational table and automatically verifies the consistency constraints
  • INSERTE

In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

INSERT statement

  • Sample database
      CREATE TABLE DEPARTMENT(
       name               VARCHAR(50)        NOT NULL,
       code               CHAR(5)            NOT NULL,
       total_staff_number DECIMAL(2)         NOT NULL,
       chair              VARCHAR(50)            NULL,
       budget             DECIMAL(9,1)       NOT NULL,
        CONSTRAINT dept_pkey PRIMARY KEY(name),
        CONSTRAINT dept_ckey1 UNIQUE(code),
        CONSTRAINT dept_ckey2 UNIQUE(chair),
        CONSTRAINT dept_check1 CHECK (total_staff_number BETWEEN 1 AND 50) );
      CREATE TABLE COURSE(
       cnum               CHAR(7)           NOT NULL,
       title              VARCHAR(200)      NOT NULL,
       credits            DECIMAL(2)        NOT NULL,
       offered_by         VARCHAR(50)           NULL,
        CONSTRAINT course_pkey PRIMARY KEY(cnum),
        CONSTRAINT course_check1 CHECK (credits IN (6, 12)),
        CONSTRAINT course_fkey1 FOREIGN KEY(offered_by)
                              REFERENCES DEPARTMENT(name) ON DELETE CASCADE );
      
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

INSERT statement

  • Examples:
      
      INSERT INTO DEPARTMENT 
                  VALUES ('Computer Science', 'CSCI', 30, 'Peter', 123456.9 );
      
      INSERT INTO COURSE VALUES('CSCI235', 'Databases', 6, 'Computer Science');
      
      INSERT INTO DEPARTMENT(name, code, chair, budget, total_staff_number) 
                     VALUES ('Mathematics', 'MATH', NULL, 12345.6, 10);
      	      
      INSERT INTO COURSE(cnum, title, offered_by, credits)        
                  VALUES('MATH345', 'Topology', 'Mathematics', 6);
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

INSERT statement

  • BEWARE !!!
      
      INSERT INTO COURSE 
                  VALUES ('PHYS999','Special Theory of Relativity',6,'Physics');
      
      --------------
      INSERT INTO COURSE VALUES ('PHYS999', 'Special Theory of Relativity', 6, 
                                                                            'Physics')
      --------------
      
      ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint 
      fails (`csit115`.`COURSE`, CONSTRAINT `course_fkey1` FOREIGN KEY (`offered_by`) 
      REFERENCES `DEPARTMENT` (`name`) ON DELETE CASCADE)
  • An order in which the rows are inserted into the relational tables is important !!!
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

SQL - Data Manipulation Language (DML)

Outline

UPDATE statement

  • Functionality:
    • UPDATE statement modifies all rows that satisfy a given condition !
    • The values of attributes modified by UPDATE statement CAN be restored by ROLLBACK statement unless UPDATE has been committed by COMMIT statement
  • UPDATE

In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

UPDATE statement

  • Sample database
      CREATE TABLE DEPARTMENT(
       name               VARCHAR(50)        NOT NULL,
       code               CHAR(5)            NOT NULL,
       total_staff_number DECIMAL(2)         NOT NULL,
       chair              VARCHAR(50)            NULL,
       budget             DECIMAL(9,1)       NOT NULL,
        CONSTRAINT dept_pkey PRIMARY KEY(name),
        CONSTRAINT dept_ckey1 UNIQUE(code),
        CONSTRAINT dept_ckey2 UNIQUE(chair),
        CONSTRAINT dept_check1 CHECK (total_staff_number BETWEEN 1 AND 50) );
      CREATE TABLE COURSE(
       cnum               CHAR(7)           NOT NULL,
       title              VARCHAR(200)      NOT NULL,
       credits            DECIMAL(2)        NOT NULL,
       offered_by         VARCHAR(50)           NULL,
        CONSTRAINT course_pkey PRIMARY KEY(cnum),
        CONSTRAINT course_check1 CHECK (credits IN (6, 12)),
        CONSTRAINT course_fkey1 FOREIGN KEY(offered_by)
                              REFERENCES DEPARTMENT(name) ON DELETE CASCADE );
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

UPDATE statement

  • Examples
    • Change total number of credits to 12 for the courses CSCI235, CSCI205, and CSCI203
    • UPDATE COURSE
      SET credits = 12 
      WHERE cnum IN ('CSCI235', 'CSCI205', 'CSCI203');
    • Change a name of chaiperson to Margaret and increase the total number of staff members by one in Department of Physics
    • UPDATE DEPARTMENT 
      SET chair = 'Margaret', 
          total_staff_number = total_staff_number + 1 
      WHERE name = 'Physics';
    • Increase the total number of staff members by two in all departments
    • UPDATE DEPARTMENT 
      SET total_staff_number = total_staff_number + 2;
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

UPDATE statement

  • BEWARE !!!
      UPDATE DEPARTMENT 
      SET name='IT' 
      WHERE name='Physics';
      --------------
      update DEPARTMENT set name ='IT' where name = 'Physics'
      --------------
      
      ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key 
      constraint fails (`csit115`.`COURSE`, CONSTRAINT `course_fkey1` 
      FOREIGN KEY (`offered_by`) REFERENCES `DEPARTMENT` (`name`) 
      ON DELETE CASCADE)
      
    • If ON UPDATE CASCADE clause is not used in a specification of foreign key then an order in which the rows are updated is important !!!
    • CREATE TABLE COURSE(
       cnum               CHAR(7)           NOT NULL,
       title              VARCHAR(200)      NOT NULL,
       credits            DECIMAL(2)        NOT NULL,
       offered_by         VARCHAR(50)           NULL,
        CONSTRAINT course_pkey PRIMARY KEY(cnum),
        CONSTRAINT course_check1 CHECK (credits IN (6, 12)),
        CONSTRAINT course_fkey1 FOREIGN KEY(offered_by)
                              REFERENCES DEPARTMENT(name) ON UPDATE CASCADE );
In HTML view press 'p' to see the lecture notes
TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023

References

TOP         Created by Janusz R. Getta, CSIT115 Data Management and Security, Autumn 2023