You know that all information in your DB is stored in tables. Thus creating
tables is the first important thing you must learn. We have seen in the class
how a good DB must be designed. The following things must be carefully
considered:
1. What is the ER diagram for the mini-world ?
2. How can we use the ER diagram to get the DB Schema design ?
3. How do we assign the various constraints in the schema ?
The above steps should lead us to a DB design which has the following
information:
1. Names of all the tables we need.
2. For each table, what are all the atributes ?
3. For each attribute, what are the domain constraints ?
4. In each table, what is the primary key ?
5. What are all the referential constraints (recall: Foreign keys !)
And of course, we need to know how we can specify all these in SYBASE !
The following examples will demonstrate the mechanism to perform all the above tasks using the create table command.
The following all assumes you are logged in to the SYBASE server using the
unix command:
unix% isql -SSYB_TEACHING -U<your-login-name>
1 > use ieem230
2 > go
1 > create table student (
2 > name char(10),
3 > SID char(10))
4 > go
NOTES:
This is the simplest way to use the create table command. For
each attirbute, you must specify the datatype . Data types
can be fixed length character strings ( char(10) is a string of 10
characters), variable length chararcter strings ( varchar(80) is a
variable length string of up to 80 characters), integers (int),
dates/times (datetime), binary (bit), currency-amounts
( money) etc..
For more information,
Look
at the Sybase Online Manuals.
create table "compute"NOTES:
("max" int,"min" int, "total score" int)
create table salesNOTES:
(stor_id char(4) not null,
ord_num varchar(20) not null,
date datetime not null,
primary key clustered (stor_id, ord_num))
By now, you must have noticed that it is quite inconvenient to type such long commands directly into SYBASE. Especially if you are typing on many lines, and notice a typing error in the previous line. The best way to execute any command in SYBASE is by the use of script files . Here is an example of such a script file. Note that you may enter MANY sybase commands in the same file -- as long as you type the "go" on a new line following the completion of each command.
It is IMPORTANT to have the ':' character in the first line !
In unix, use your editor (e.g. pico) to create such a script file, make the file executable, and execute it to run the sybase command.
create table salesdetailNOTES:
(stor_id char(4) not null,
ord_num varchar(20) not null,
title_id int not null references titles(title_id),
qty smallint default 0 not null,
discount float not null,
constraint salesdet_constr
foreign key (stor_id, ord_num)
references sales(stor_id, ord_num))
create table publishersNOTES:
(pub_id char(4) not null check (pub_id in ("1389", "0736", "0877", "1622", "1756")),
pub_name varchar(40) null,
city varchar(20) null,
state char(2) null)
1 > sp_addtype hk_id, "char(10)", "not null"
2 > go
1 >
And then you can use it as follows:
create table people
(name char(12) not null,
id_num hk_id)