] > Data and Schema Modifications in SQL

Data and Schema Modifications in SQL

7.1 Inserting Rows

insert into table (attributes)
<values (values) SQL-query >

insert into  Students (Name,Number,Sex) 
             values   (’Don’,4123,’F’) 
Students
NameNumberSex
Ben3412 M
Dan1234 M
Nel2341 F
NameNumberSex
Ben3412 M
Dan1234 M
Nel2341 F
Don4123 F

The previous example inserts a single record, the following incorporates information from an alternative table.

insert into  Students 
             (select Name,Number,Sex 
              from Applicants 
              where State = ’OH’) 
Students
NameNumberSex
Ben3412 M
Dan1234 M
Nel2341 F
Applicants
NameNumberSexState
Don4123 FOH
Pam3421 FMI
Students
NameNumberSex
Ben3412 M
Dan1234 M
Nel2341 F
Don4123 F

Incomplete insertions are similar to

insert into Students (Name,Number) 
            (select Name,Number 
              from Applicants 
              where State = ’OH’) 

7.2 Deleting Rows

delete from table
where condition

delete from  Students 
where Number < 2000 
Students
NameNumberSex
Ben3412 M
Dan1234 M
Nel2341 F
NameNumberSex
Ben3412 M
Nel2341 F

7.3 Updating Attributes

update table
set attribute = <expr SQL-query null default >,...
where condition

update  Students 
set Name = ’Tom’, Number = Number + 5 
where Name = ’Dan’ 
Students
NameNumberSex
Ben3412 M
Dan1234 M
Nel2341 F
NameNumberSex
Ben3412 M
Tom1239 M
Nel2341 F

7.4 Updating Table Definitions

alter table name
<
add column def
drop column name
alter column name < set default value drop default >
>

Names can be assigned to constraints by a prefix of the form constraint name.

create table Student( 
   Name   varchar (5) not null, 
   Number numeric(4) primary key 
) 
alter table Student 
     add column BirthDate date 

7.5 Modifications through Views

7.6 Removing Components

drop < table view > name restrict cascade

7.7 Updating Domain Definitions

alter domain name
<
set value value
drop default
add constraint def
drop constraint name
>

The revised specifications must agree with the data already stored in the table

ALTER DOMAIN Name SET NOT NULL; 
ALTER DOMAIN Name DROP NOT NULL; 

7.8 Assignment #3

Due: Mo, Feb 9, midnight

Provide a SQL file which performs the following operations.

  1. Declares the tables of the library schema of Problem 6.18 in the textbook (pages 186–187 4th ed; 215–216 5th ed).
  2. Populates the tables with entries which produce non-empty answers for the queries below
  3. Issues queries (a)–(g) of Problem 6.18

Notes

References:

Reference: Ch. 8.6 in textbook.