Java Reference
In-Depth Information
F IGURE 32.9
You can run SQL commands in a script file.
Note
You can populate the javabook database using the script from Supplement IV.A.
populating database
32.3.3 Creating and Dropping Tables
Tables are the essential objects in a database. To create a table, use the create table state-
ment to specify a table name, attributes, and types, as in the following example:
create table
create table Course (
courseId char ( 5 ),
subjectId char ( 4 ) not null ,
courseNumber integer ,
title varchar ( 50 ) not null ,
numOfCredits integer ,
primary key (courseId)
);
This statement creates the Course table with attributes courseId , subjectId ,
courseNumber , title , and numOfCredits . Each attribute has a data type that specifies
the type of data stored in the attribute. char(5) specifies that courseId consists of five
characters. varchar(50) specifies that title is a variant-length string with a maximum
of 50 characters. integer specifies that courseNumber is an integer. The primary key is
courseId .
The tables Student and Enrollment can be created as follows:
create table Student (
ssn char ( 9 ),
firstName varchar ( 25 ),
mi char ( 1 ),
lastName varchar ( 25 ),
birthDate date ,
street varchar ( 25 ),
phone char ( 11 ),
zipCode char ( 5 ),
deptId char ( 4 ),
primary key (ssn)
create table Enrollment (
ssn char ( 9 ),
courseId char ( 5 ),
dateRegistered date ,
grade char ( 1 ),
primary key (ssn, courseId),
foreign key (ssn) references
Student(ssn),
foreign key (courseId) references
Course(courseId)
);
);
Note
SQL keywords are not case sensitive. This topic adopts the following naming conventions:
tables are named in the same way as Java classes, and attributes are named in the same
way as Java variables. SQL keywords are named in the same way as Java keywords.
naming convention
 
 
Search WWH ::




Custom Search