insert into table (attributes) values(values) SQL-query
insert into Students (Name,Number,Sex) values (’Don’,4123,’F’)
Students
Name
Number
Sex
Ben
3412
M
Dan
1234
M
Nel
2341
F
Name
Number
Sex
Ben
3412
M
Dan
1234
M
Nel
2341
F
Don
4123
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
Name
Number
Sex
Ben
3412
M
Dan
1234
M
Nel
2341
F
Applicants
Name
Number
Sex
State
Don
4123
F
OH
Pam
3421
F
MI
Students
Name
Number
Sex
Ben
3412
M
Dan
1234
M
Nel
2341
F
Don
4123
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 wherecondition
delete from Students where Number < 2000
Students
Name
Number
Sex
Ben
3412
M
Dan
1234
M
Nel
2341
F
Name
Number
Sex
Ben
3412
M
Nel
2341
F
7.3 Updating Attributes
update table set attribute = exprSQL-querynulldefault,... wherecondition
update Students set Name = ’Tom’, Number = Number + 5 where Name = ’Dan’
Students
Name
Number
Sex
Ben
3412
M
Dan
1234
M
Nel
2341
F
Name
Number
Sex
Ben
3412
M
Tom
1239
M
Nel
2341
F
7.4 Updating Table Definitions
alter table name
addcolumn def drop columnname alter columnname set defaultvalue 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
A view is declared to be updated-able by appending its definition with the clause ‘with check option’
create view Males (Nm,Num) select Name,Number from Students where Sex = ’M’ with check option
A view may be updated only if there is a unique way to achieve it on the base relations.
An ‘insert into Males(Name,Number) values (Ron,4123)’ adds the record ‘Ron,4123)’ to ‘Students’.
Students
Name
Number
Sex
Ben
3412
M
Dan
1234
M
Nel
2341
F
Name
Number
Sex
Ben
3412
M
Dan
1234
M
Nel
2341
F
Ron
4123
NULL
7.6 Removing Components
drop tableviewnamerestrictcascade
restrict asks the action to take place only if the component is empty
cascade removes the component and its dependents
7.7 Updating Domain Definitions
alter domain name
setvalue value dropdefault addconstraint def dropconstraint 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.
Declares the tables of the libraryschema of Problem 6.18 in the textbook (pages 186–187 4th ed; 215–216 5th ed).
Populates the tables with entries which produce non-empty answers for the queries below
Issues queries (a)–(g) of Problem 6.18
Notes
Submit your file electronically using the departmental submit utility: submit XXX lab3 lab3.sql. XXX
represents ‘c670aa’ for students of the 3:30 section, and ‘c670ab’ for students of the 5:30 section.
Files are restricted to 1,048,576 bytes
The submit program issues error messages to the student’s CSE email account
Your file should be well formatted and commented.
isql can be invoked in batch mode with commands of the form
isql -X -i file.sql
or of the form
isql -P password < file.sql
Complex files may be compilable only in batch mode, and not be receptive to import through the ‘:r’ command in
interactive mode.
The go directives are required also in files processed in batch mode. For instance,
/* comment */ create ... go insert to ... go select ... from ... where ... go delete from ... go ...
Sybase accounts are established in a a similar way as for Unix accounts. For SSH programs, use the host name
‘stdsun.cse.ohio-state.edu’.
If you don’t have a, or don’t remember your CSE Unix account, please go with an id to the SOC lab (8th floor of
DL) and get an account.
If you don’t have a, or don’t remember your, sybase account, please contact help@cse.ohio-state.edu. Put the word
PRIVATE in the subject header when sending a request that contains sensitive information, such as social security
numbers. (If you fail to do this, any information included in the request will be viewable to anyone, anywhere on the
web.)