I was wondering if there is any way to make a single Not Null constraint that could apply to all the attributes in my table. I know I could add one to each line, but I am just trying to learn to be more efficient. I tried searching online and I didn't find anything on this topic.
Thanks!
MORE INFO:
I guess my question could be generalized to ask "Is there any way to define table-wide or database-wide constraints?". For this entire database, I need to the default value of every varchar to '' (the empty string) but right now I am doing that manually for every varchar.
Again, I already did most of it, but it seems l开发者_运维问答ike there should be a way to avoid this repetition.
From http://dev.mysql.com/doc/refman/5.1/en/create-table.html - the only place you can put a null/not null is on a column.
column_definition:
data_type [NOT NULL | NULL] [DEFAULT default_value]
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]
[COMMENT 'string']
[COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
[STORAGE {DISK|MEMORY|DEFAULT}]
[reference_definition]
There's really no reason to have a table constraint, IMO. The DDL specifies constrains on the data in each column, not the column definition itself. While one might want to put this in as syntactic sugar, it seems like there could be disadvantages - 1) to add a new column you might have to alter the table constraint 2) MySQL defaults to NULL and this might lead to confusion
No, there isn't any way to do this. You have to speciy this at the column level but this is something you only do once. Perhaps is a couple of keystrokes more or one click per column. No inefficiency there.
Have you think in triggers
or stored procedures
? It totally depends on your DBMS. But it also adds a over work in the DB.
Forget about the CHECK
clause because mySql
ignores it.
精彩评论