Docsity

SQL Data Definition and Manipulation: Deleting, Changing, and Modifying Tables and Columns, Slides of Database Management Systems (DBMS)

Partial preview of the text

Download SQL Data Definition and Manipulation: Deleting, Changing, and Modifying Tables and Columns and more Slides Database Management Systems (DBMS) in PDF only on Docsity!

More SQL Data Definition

Deleting Tables

  • To delete a table use
  • DROP TABLE
  • [IF EXISTS]
  • Example:
  • DROP TABLE Module
    • BE CAREFUL with

any SQL statement

with DROP in it

  • You will delete any information in the table as well
  • You won’t normally be asked to confirm
  • There is no easy way to undo the changes

ALTERing Columns

To add or remove

columns use

ALTER TABLE

ADD COLUMN

ALTER TABLE

DROP COLUMN

Examples

ALTER TABLE Student

ADD COLUMN

Degree VARCHAR(50)

ALTER TABLE Student

DROP COLUMN Degree

ALTERing Constraints

To add or remove

columns use

ALTER TABLE

ADD CONSTRAINT

ALTER TABLE

DROP CONSTRAINT

Examples

ALTER TABLE Module

ADD CONSTRAINT

ck UNIQUE (title)

ALTER TABLE Module

DROP CONSTRAINT ck

INSERT

INSERT INTO

(col1, col2, …)

VALUES

(val1, val2, …)

  • The number of columns and values must be the same
  • If you are adding a value to every column, you don’t have to list them
  • SQL doesn’t require that all rows are different (unless a constraint says so)

INSERT

Student

ID

1

Name John

Year 1

Student ID 1 2

Name John Mary

Year 1

Student ID 1 2

Name John Mary

Year 1 3

Student ID 1 2

Name John Mary

Year 1 3

INSERT INTO Student (ID, Name, Year) VALUES (2, ‘Mary’, 3)

INSERT INTO Student (Name, ID) VALUES (‘Mary’, 2)

INSERT INTO Student VALUES (2, ‘Mary’, 3)

UPDATE

Student

ID

1 2 3 4

Name John Mark Anne Mary

Year 1 3 2 2

Student ID 1 2 3 4

Name John Mark Anne Mary

Year 2 4 3 3

UPDATE Student SET Year = Year + 1

UPDATE Student SET Year = 1, Name = ‘Jane’ WHERE ID = 4

Student ID 1 2 3 4

Name John Mark Anne Jane

Year 1 3 2 1

DELETE

  • Removes all rows

which satisfy the

condition

DELETE FROM

[WHERE

]

  • If no condition is given then ALL rows are deleted - BE CAREFUL
  • Some versions of SQL also have TRUNCATE TABLE which is like DELETE FROM but it is quicker as it doesn’t record its actions

SELECT

  • The SQL command

you will use most

often

  • Queries a set of tables and returns results as a table
  • Lots of options, we will look at many of them
  • Usually more than one way to do any given query - SQL’s SELECT is

different from the

relational algebra’s

selection σ

  • SELECT in SQL does all of the relational algebra
  • But it is a bit different because SQL differs from the relational model

SQL SELECT Overview

SELECT

[DISTINCT | ALL] <column-list>

FROM <table-names>

[WHERE ]

[ORDER BY <column-list>]

[GROUP BY <column-list>]

[HAVING ]

• ([] - optional, | - or)

Sample SELECTs

SELECT * FROM Student

stuID stuName stuAddress stuYear 1 Anderson 15 High St 1 2 Brooks 27 Queen’s Rd 3 3 Chen Lenton Hall 1 4 D’Angelo Derby Hall 1 5 Evans Lenton Hall 2 6 Franklin 13 Elm St 3 7 Gandhi Lenton Hall 1 8 Harrison Derby Hall 1

Sample SELECTs

SELECT stuName FROM Student

stuName Anderson Brooks Chen D’Angelo Evans Franklin Gandhi Harrison

Being Careful

  • When using DELETE

and UPDATE

  • You need to be careful to have the right WHERE clause
  • You can check it by running a SELECT statement with the same WHERE clause first

Before running

DELETE FROM Student

WHERE Year = 3

run

SELECT * FROM Student

WHERE Year = 3

Sequences

  • Often we want to

assign each row a

unique number

  • These are useful as primary keys
  • Using integers to reference rows is more efficient
  • We would like the DBMS to do this - In most versions of

SQL we can use

autoincrementing

fields to do this

  • Details differ between versions
  • Usually the first entry is assigned 1, the next 2, and so on, but Oracle lets you change this