改进 DDL 中的 SQL 查询
已完成改进
- nvarchar(5000) -> nvarchar(4000) 但 PostgreSQL 中没有 nvarchar => TEXT
- 内存限制某些变量,
- 语法稍微更改为更易读的
- 破折号到下划线
- Magnus 的改进
我正在遵循我的计划第一个数据库项目。
我想知道查询和关系表中的任何弱点。
DDL 中的 SQL 查询
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
关系表
替代文本 http://files.getdropbox.com/u/175564/db/db777.png
您会在 DDL 查询中改进哪些方面?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您使用 varchar(4000) 等时,4000 实际上是字符串可以存在的概念最大值吗? 或者你只是选择了“足够大的东西”? 如果是第二种,只需使用 text 数据类型。 它会同样快(实际上,稍微快一点,但你可能无法测量)。
sent_time 看起来应该是一个时间戳。 不要将日期/时间存储在 varchar 中。
postgres 中没有 auto_increment,请使用 serial 列。
您在标签和问题之间有循环引用,我确信您不是故意的。 并且您对 Questions.question_id 的检查约束似乎检查了 user_id - 我打赌复制/粘贴太多了。
最后,不要使用混合大小写的标识符。 一切都小写,这样就不必引用它们。 例如,使用小写字母表示列名和表名。
When you use varchar(4000) and so, is 4000 actually a conceptual maximum of how long the string can be there? Or did you just pick something "big enough for everything"? If the second, just use the text datatype. It will be just as fast (actually, a tiny bit faster, but you will not likely be able to measure that).
sent_time looks like it should be a timestamptz. Don't store date/time in a varchar.
auto_increment is not in postgres, use a serial column.
You have a circular reference between Tags and Questions, which I'm sure you didn't intend. And your check constraint on Questions.question_id appears checks user_id - too much copy/paste I bet.
Finally, don't use mixed case identifiers. Do everything lowercase, so you don't have to quote them. For instance, use lowercase for column and table names.