/* ====================================================================== /* SQL code for creating tables and constraints /* ====================================================================== create table Usr /* for users of the system; this is a peer-to-peer application ( loginName varchar(10) not null, password varchar(8) not null, lastName varchar(30) not null, firstName varchar(30) not null, phone char(10) not null, email varchar(30) not null, constraint PK_USR primary key (loginName) ) create table Student /* a student is a user ( loginName varchar(10) not null, major varchar(30) not null, status char(10) not null, constraint PK_STUDENT primary key (loginName), /* each user record corresponds to at most one student record constraint CK_STATUS_STUDENT check (status in ('freshman','sophomore','junior','senior','graduate')) ) create table FacultyStaff /* a faculty/staff is a user ( loginName varchar(10) not null, department varchar(30) not null, position varchar(30) not null, constraint PK_FACSTA primary key (loginName) ) create table Lives /* this relates each user with a single address ( loginName varchar(10) not null, addrId int not null, constraint PK_LIVES primary key (loginName) /* each user can live at at most one address ) create table Address /* each address has an id ( addrId int not null, constraint PK_ADDRESS primary key (addrId) ) create table CampusAddr /* a campus address is an address ( addrId int not null, street varchar(50) not null, roomNo varchar(10) not null, constraint PK_CAMPUSADDR primary key (addrId), constraint AK_ADDR_CAMPUSADDR unique (street, roomNo) /* stree and room nnumber together should be unique ) create table ResidenceAddr /* a residence address is an address ( addrId int not null, street varchar(50) not null, town varchar(30) not null, state char(2) default 'NY' not null, zipcode char(5) not null, nearestIntersection varchar(50) not null, constraint PK_RESIDADDR primary key (addrId), constraint AK_ADDR_RESIDADDR unique (street,town,state,zipcode,nearestIntersection) /* address items together should be unique ) /* all information above is required; the attribute values should be not null create table OptionalInfo /* each user can have a set of optional information ( loginName varchar(10) not null, birthYear int null , sex char(1) null , smokerOrNot char(1) null , preferDriveOrRide varchar(15) null , commuteCostCompensation int null , currentCommuteMethod varchar(20) null , constraint PK_OPTINFO primary key (loginName), /* each user can have at most one set of optional info constraint CK_SEX_OPTINFO check (sex in ('M','F')), constraint CK_SMOKERORNOT_OPTINFO check (smokerOrNot in ('Y','N')), constraint CK_BIRTHYEAR check (birthYear between 1000 and 9999), /* year is a 4-digit integer constraint CK_PREFDRIVEORRIDE_OPTINFO check (preferDriveOrRide in ('drive only','share driving','ride only')) ) create table Owns /* this relates each user with the cars he/she owns ( loginName varchar(10) not null, carId int not null, constraint PK_OWNS primary key (loginName,carId) ) create table Car /* cars are uniquely identified with the information modeled ( carId int not null, manufacture varchar(30) not null, model varchar(20) not null, year int not null, constraint PK_CAR primary key (carId), constraint AK_CARINFO_CAR unique (manufacture, model, year) /* manufacturer, model, year together should be unique constraint CK_CAR_YEAR check (year between 1000 and 9999), ) create table DailyCommute /* daily commute requests posted by users ( loginName varchar(10) not null, startTime datetime not null, endTime datetime not null, maxMinutesEarlyArrival int default 0 null , maxMinutesLateDeparture int default 0 null , year int not null, season char(10) not null, constraint PK_DAILYCOMMUTE primary key (loginName), /* each user can post at most one daily commute constraint CK_TIMES_DAILYCOMM check (startTime