当我应该使用执照;不是NULL”在MySQL表和有什么好处吗?When should I be using “NOT NULL” in a MySQL table and are there any benefits?
I have the following rails migration:
create_table :articles do |t| t.integer :user_id, :allow_null => false t.integer :genre_id, :allow_null => false t.string :url, :limit => 255, :allow_null => false t.string :title, :limit => 60, :allow_null => false t.text :summary, :limit => 350, :allow_null => false t.integer :votes_count, :default => 0 t.datetime :published_at, :default => nil t.timestamps end
All the fields that are "NOT NULL" are validated in the model first, so I'm wondering if I need to bother having allow_null in the migration? I'm not sure what benefits "NOT NULL" gives to the database, if any.
Not much if you mean in terms of performance or storage efficiency. However, it's just good practice to push as many of your low-level constraints into the database layer. For one thing, it guarantees that a subtle bug in Rails isn't going to lead to some randomly
NULL data in a not-null field. Likewise, if you ever run another app against the same database, it will be extremely helpful to have the constraints in a central place for maintenance and to avoid duplication.
NOT NULL is one more thing where the computer can keep an eye on you and keep you from making mistakes.
这并不seemt o影响mySQL,但你应该意识到最近2固定迁移问题(一个当你不指定默认) http://blog.codefront.net/2008/05/04/living-on-the-edge-of-rails-19-change_table-for-migrations-and-more/ http://antoniocangiano.com/2008/07/14/a-close-look-at-three-rails-21-bugs/
This doesn't seemt o affect mySQL, but you should be aware of 2 recently fixed migration issues (one when you don't specify default)
- MySQL加密列MySQL encrypted columns
- 全文搜索相关性测量在吗? - Full-text search relevance is measured in?
- 如何编写一个mysql查询,将读取数据从一个表和写信给另一个?How do I write a mysql query that will read data from one table and write to another?
- 如何使用外键为新表how to use foreign key into new table
- 如果不存在MYSQL:创建表MYSQL: Create Table If Not Exists
- 当我应该使用执照;不是NULL”在MySQL表和有什么好处吗?When should I be using “NOT NULL” in a MySQL table and are there any benefits?
- 你怎么两个语句吗?How do you OR two LIKE statements?
- 个人项目——下一个实际的语言/技术学习Personal Project - Next practical language/tech to learn
- MySQL—;马克1匹配的行MySQL — mark all but 1 matching row
- 什么你读过的最好的书或文章优化mysql服务器(linux)?(关闭)Whats the best book or article you have read on optimizing mysql servers (linux)? [closed]
- 发现差异在MySQL的两个表的行数Finding difference in row count of two tables in MySQL