Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Creating simple database - constraints

Author  Topic 

kameleon
Starting Member

2 Posts

Posted - 2010-03-12 : 09:57:21
Hello,

I've had to do simple database with constraints (wherever they are possible). Database contains: Student, Course and association class named Pass. Here it is the sql code which I've written. Please check it out if constraints wchich I've made are good.


CREATE TABLE Students(
idNumber VARCHAR( 6 ) NOT NULL UNIQUE,
name VARCHAR( 30 ) NOT NULL,
surname VARCHAR( 35 ) NOT NULL,
birthDate DATE NOT NULL CHECK (
birthDate > '1950-01-01' AND birthDate < YEAR(GETDATE()) - 18
),
CONSTRAINT pk_students PRIMARY KEY( idNumber )
)

CREATE TABLE Courses (
idCourse VARCHAR( 7 ) NOT NULL UNIQUE DEFAULT 'Z00-00x',
name VARCHAR( 30 ) NOT NULL,
CONSTRAINT pk_courses PRIMARY KEY( courseCode )
);

CREATE TABLE Passes(
idCourse VARCHAR( 7 ) NOT NULL REFERENCES Courses,
idNumber VARCHAR( 6 ) NOT NULL REFERENCES Students,
passDate DATE CHECK (
SELECT birthDate FROM Studenci WHERE passDate > birthDate AND passDate <= GETDATE()
),
grade REAL DEFAULT 5.0 CHECK (
grade = 2.0 OR
grade = 3.0 OR
grade = 3.5 OR
grade = 4.0 OR
grade = 4.5 OR
grade = 5.0 OR
grade = 5.5
),
CONSTRAINT pk_Passes PRIMARY KEY(idCourse, idNumber)
)


p.s sorry for my English but I'm still learning.

DaleTurley
Yak Posting Veteran

76 Posts

Posted - 2010-03-12 : 11:01:59
"birthDate < YEAR(GETDATE()) - 18"
Without testing this id say that birthDate < DATEADD(YEAR, -18, GETDATE()) would be better.

"UNIQUE DEFAULT 'Z00-00x'" If there is a default value that is the same everytime then it can't be unique - unless the x means and incrementing number???
Go to Top of Page

kameleon
Starting Member

2 Posts

Posted - 2010-03-12 : 17:19:20
quote:
Originally posted by DaleTurley

"birthDate < YEAR(GETDATE()) - 18"
Without testing this id say that birthDate < DATEADD(YEAR, -18, GETDATE()) would be better.


Thank you. Unfortunately I cannot check it in SQL Server 2008 - I have problems with installation in Win 7 machine. But if i have a possibility I will try it.

quote:

"UNIQUE DEFAULT 'Z00-00x'" If there is a default value that is the same everytime then it can't be unique - unless the x means and incrementing number???



Yeah, good point.
BTW is it any possibility in SQL to automatic increment x number?
Go to Top of Page
   

- Advertisement -