Improvements done
- nvarchar(5000) -> nvarchar(4000) BUT no nvarchar in PostgreSQL => TEXT
- memory limits to some variables
- the syntax slightly changed to more readable
- dashes to underscores
- Magnus' improvements
I am following my plan for my first database project.
I would like to know any weaknesses in the queries and in the relational table.
SQL-queries in DDL
CREATE TABLE answers
(
question_id INTEGER FOREIGN KEY REFERENCES questions(user_id)
PRIMARY KEY
CHECK (user_id>0),
answer TEXT NOT NULL -- answer must have text
);
CREATE TABLE questions
(
user_id INTEGER FOREIGN KEY
REFERENCES user_info(user_id)
PRIMARY KEY
CHECK (user_id>0),
question_id INTEGER FOREIGN KEY REFERENCES tags(question_id)
NOT NULL
CHECK (user_id>0)
SERIAL,
body TEXT NOT NULL, -- question must have body
title VARCHAR(60) NOT NULL, -- no empty title
moderator_removal BOOLEAN NOT NULL, -- by default false
sent_time TIMESTAMP NOT NULL
);
CREATE TABLE tags
(
question_id INTEGER FOREIGN KEY REFERENCES questions(question_id)
CHECK (user_id>0),
tag VARCHAR(20) NOT NULL,
CONSTRAINT no_duplicate_tag UNIQUE (question_id,tag)
)
CREATE TABLE user_infos
(
user_id INTEGER FOREIGN KEY REFERENCES questions(user_id)
PRIMARY KEY
CHECK (user_id>0)
SERIAL
UNIQUE,
username VARCHAR(25),
email VARCHAR(320) NOT NULL -- maximun possible
UNIQUE,
password_sha512 INTEGER NOT NULL,
is_moderator BOOLEAN NOT NULL,
is_Login BOOLEAN NOT NULL,
has_been_sent_a_moderator_message BOOLEAN NOT NULL
);
-- to have default values
ALTER TABLE questions ALTER COLUMN moderator_removal SET DEFAULT FALSE
ALTER TABLE user_info ALTER COLUMN is_moderator SET DEFAULT FALSE
ALTER TABLE user_info ALTER COLUMN is_login SET DEFAULT FALSE
ALTER TABLE user_info ALTER COLUMN has_been_sent_a_moderator_message SET DEFAULT FALSE
-- to have default values
ALTER TABLE questions ALTER COLUMN moderator_removal SET DEFAULT FALSE
ALTER TABLE user_info ALTER COLUMN is_moderator SET DEFAULT FALSE
ALTER TABLE user_info ALTER COLUMN is_login SET DEFAULT FALSE
ALTER TABLE user_info ALTER COLUMN has_been_sent_a_moderator_message SET DEFAULT FALSE
Relational Table
alt text http://files.getdropbox.com/u/175564/db/db777.png
What would you improve in the DDL queries?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…