| |
|
If this is your first visit, be sure to check out the FAQ by clicking the link above.
You may have to register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
|
 |

03-18-09, 20:12
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 104
|
|
|
key constraints inline or separate? (was "primitive n00b question")
|
|
I notice that it's uncommon to write a foreign key constraint in-line, like the following, but I'm unclear why... and also why some prefer the primary key constraint to be on a line below the column declaration. Why is this?
Code:
CREATE TABLE stands
(
standID INTEGER NOT NULL PRIMARY KEY
, slope INTEGER NOT NULL FOREIGN KEY
);
|
|

03-18-09, 20:33
|
|
Resident Curmudgeon
|
|
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,595
|
|
I prefer to see the PRIMARY KEY and FOREIGN KEY declarations on separate lines from the column name and data type. I can explain my reasoning, although I'm sure that others have their own reasons for choosing one style or the other.
The data type and NULL or NOT NULL are column definitions... They are fundamental to column definitions in SQL and SQL-like languages. The PRIMARY KEY and FOREIGN KEY declarations are constraints, they describe limitations on a table and not all SQL engines support them.
MySQL is a notorious offender in the area of constraints because it will usually accept the syntax, but may or may not enforce the constraint depending on the MySQL version, the database engine being used, etc.
-PatP
|
|

03-18-09, 20:46
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 104
|
|
|
So when reading code
|
|
That makes it easier to read code, too, I would imagine. Thanks.
|
|

03-18-09, 21:14
|
|
Registered User
|
|
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
|
|
Another reason is that unique/foreign key constraint definitions cannot be "inline" if you have multi-column keys. So instead of using different ways to write those constraints (sometimes inline, sometimes separately), it is much better to stick to the same style for all table definitions.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
|
|

03-18-09, 21:21
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
|
|
even in the ANSI SQL forum you take a slam at mysql, eh pat?
it grows tiresome
|
|

03-18-09, 23:37
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 104
|
|
|
thanks -- resolved on in-line constraints.
Quote:
|
Originally Posted by stolze
Another reason is that unique/foreign key constraint definitions cannot be "inline" if you have multi-column keys. So instead of using different ways to write those constraints (sometimes inline, sometimes separately), it is much better to stick to the same style for all table definitions.
|
Thanks. This makes clear sense. 
|
|

03-19-09, 04:09
|
|
Registered User
|
|
Join Date: Dec 2008
Location: At work...
Posts: 68
|
|
Quote:
|
Originally Posted by rbfree
I notice that it's uncommon to write a foreign key constraint in-line, like the following, but I'm unclear why... and also why some prefer the primary key constraint to be on a line below the column declaration. Why is this?
Code:
CREATE TABLE stands
(
standID INTEGER NOT NULL PRIMARY KEY
, slope INTEGER NOT NULL FOREIGN KEY
);
|
ANSI-invalid syntax. (primary key part ok, foreign key part invalid.) A foreign key is a table constraint, not a column constraint.
The statement above has no referenced table specified for the foreign key. A foreign key must always reference to a table (usually a second table, sometimes itself).
To verify a SQL statements standard compliance, check out the SQL Validator for an instant syntax verification:
Mimer SQL Developers - Mimer SQL Validator
|
|

03-19-09, 04:54
|
|
Registered User
|
|
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
|
|
Quote:
|
Originally Posted by JarlH
ANSI-invalid syntax. (primary key part ok, foreign key part invalid.) A foreign key is a table constraint, not a column constraint.
|
You are wrong here. The only reason why the example syntax is invalid is because the it would have to be "columnName INT REFERENCES tableName(colName)". Otherwise, you can inline a FOREIGN KEY constraint as shown. (Subclause 11.4, "<column definition>" is ISO/IEC 9075-2003 allows for a <references specification> there.
p.s: I would argue that a foreign key is a row constraint or a constraint on the table. It only restricts the values allowed in the columns of a single row and is not across rows.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
|
|

03-19-09, 05:11
|
|
Registered User
|
|
Join Date: Dec 2008
Location: At work...
Posts: 68
|
|
Yes, you're right. It's obvious that I need to check the standard one more time. Thanks!
|
|

03-19-09, 11:33
|
|
Registered User
|
|
Join Date: Feb 2009
Posts: 104
|
|
|
resolved -- thanks
Thanks for both these entries. Yes, the example was poor. My apologies for that. Lesson learned. I did get the logic, though. Just had to quit for awhile and come back and look... and the answer was obvious and the question seemed silly. Again, thanks for all help and patience with n00b issues.
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|