改进 DDL 中的 SQL 查询

发布于 2024-07-30 07:42:44 字数 2950 浏览 3 评论 0原文

已完成改进

  1. nvarchar(5000) -> nvarchar(4000) 但 PostgreSQL 中没有 nvarchar => TEXT
  2. 内存限制某些变量,
  3. 语法稍微更改为更易读的
  4. 破折号到下划线
  5. 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

  1. nvarchar(5000) -> nvarchar(4000) BUT no nvarchar in PostgreSQL => TEXT
  2. memory limits to some variables
  3. the syntax slightly changed to more readable
  4. dashes to underscores
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以畏孤独 2024-08-06 07:42:44

当您使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文