改进DDL中的SQL查询 [英] To improve SQL-queries in DDL

查看:139
本文介绍了改进DDL中的SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完成的改进


  1. nvarchar(5000) - > nvarchar(4000)在PostgreSQL中没有nvarchar = > TEXT

  2. 对某些变量的内存限制

  3. 语法略微更改为可读性


  4. Magnus的改进

    a>

我正在关注我的第一个数据库项目的计划



我想知道



在DDL中执行SQL查询

  CREATE TABLE answer 

question_id INTEGER FOREIGN KEY REFERENCES question(user_id)
PRIMARY KEY
CHECK(user_id> 0),
answer TEXT NOT NULL - answer必须有文本
);

CREATE TABLE问题

user_id INTEGER FOREIGN KEY
REFERENCES user_info(user_id)
PRIMARY KEY
CHECK(user_id> 0)
question_id INTEGER FOREIGN KEY REFERENCES标签(question_id)
NOT NULL
CHECK(user_id> 0)
SERIAL,
body TEXT NOT NULL,
title VARCHAR(60)NOT NULL, - 无空标题
moderator_removal BOOLEAN NOT NULL, - 默认为false
sent_time TIMESTAMP NOT NULL
);

CREATE TABLE标签

question_id INTEGER FOREIGN KEY REFERENCES问题(question_id)
CHECK(user_id> 0),
标签VARCHAR NULL,
CONSTRAINT no_duplicate_tag UNIQUE(question_id,tag)



CREATE TABLE user_infos

user_id INTEGER FOREIGN KEY REFERENCES问题user_id)
PRIMARY KEY
CHECK(user_id> 0)
SERIAL
UNIQUE,
username VARCHAR(25),
email VARCHAR - 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
) ;



- 具有默认值

ALTER TABLE问题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


- 具有默认值

ALTER TABLE问题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

关系表 p>

alt text http:/ /files.getdropbox.com/u/175564/db/db777.png



您会在DDL查询中改善什么?

解决方案

当你使用varchar(4000)等等时,4000实际上是一个概念上的最大值, ?还是你只是选择足够大的一切的东西?如果是第二个,只需使用文本数据类型。它会是一样快(实际上,一点点快,但你不可能能够测量)。



sent_time看起来应该是一个 timestamptz 。不要在varchar中存储日期/时间。



auto_increment不在postgres中,请使用串行
$ b

你有一个在标签和问题之间的循环引用,我相信你没有打算。而且对Questions.question_id的检查约束出现检查user_id - 太多复制/粘贴我打赌。



最后,不要使用混合大小写标识符。做一切小写,所以你不必引用他们。例如,对列名和表名使用小写。


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?

解决方案

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.

这篇关于改进DDL中的SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆