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.

 
Go Back  dBforums > General > Database Concepts & Design > Style: Specifying constraints, In-line vs Out-of-line (CREATE TABLE vs ALTER TABLE)?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-18-08, 13:55
tmpuzer tmpuzer is offline
Registered User
 
Join Date: Mar 2008
Posts: 52
Style: Specifying constraints, In-line vs Out-of-line (CREATE TABLE vs ALTER TABLE)?

Two styles for defining constraints:

In-line:
Code:
CREATE TABLE fruit
(
  fruit_id INT IDENTITY(1,1)
      CONSTRAINT PK_fruit PRIMARY KEY CLUSTERED,
  fruit_name NVARCHAR(50),
  fruit_type INT
      CONSTRAINT FK_fruit_fruit_types FOREIGN KEY
      REFERENCES fruit_types (fruit_type_id) ON UPDATE CASCADE,
  date_created DATETIME DEFAULT CURRENT_TIMESTAMP
)
Out-of-line:
Code:
CREATE TABLE fruit
(
  fruit_id INT,
  fruit_name NVARCHAR(50),
  fruit_type_id INT,
  date_created DATETIME
)

ALTER TABLE fruit ALTER COLUMN fruit_id INT NOT NULL

ALTER TABLE fruit ADD
  CONSTRAINT PK_fruit PRIMARY KEY CLUSTERED (fruit_id),
  CONSTRAINT FK_fruit_fruit_types FOREIGN KEY (fruit_type_id)
      REFERENCES fruit_types (fruit_type_id),
  CONSTRAINT DF_fruit_date_created DEFAULT
      CURRENT_TIMESTAMP FOR date_created
Which style do you prefer and why?
Reply With Quote
  #2 (permalink)  
Old 04-18-08, 14:02
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
I'd say on the list of importing decisions in my life, that links somewhere above "Paper or plastic?" but below "Venti or Grande'?".
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #3 (permalink)  
Old 04-18-08, 23:28
tmpuzer tmpuzer is offline
Registered User
 
Join Date: Mar 2008
Posts: 52
What about canvas bags?? To me it's an issue of layout and readability, which IMO are important issues.
Reply With Quote
  #4 (permalink)  
Old 04-19-08, 00:14
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
Quote:
Originally Posted by tmpuzer
To me it's an issue of layout and readability, which IMO are important issues.
For DDL scripts that are only executed once, when the Database is created or for updated?
I hope you're not creating these tables and indexes on the fly in your application code.
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
  #5 (permalink)  
Old 04-19-08, 01:29
tmpuzer tmpuzer is offline
Registered User
 
Join Date: Mar 2008
Posts: 52
For DDL scripts (not application code) executed once on database creation.
Reply With Quote
  #6 (permalink)  
Old 04-19-08, 07:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Quote:
Originally Posted by tmpuzer
To me it's an issue of layout and readability, which IMO are important issues.
okay, fair point

so, which one do you prefer?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 04-19-08, 07:13
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
I put everything that I can in create table statement (basically everything except non-unique indexes).

Keeps my code compact and together. But yeah - I would not call it a religious dogma.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #8 (permalink)  
Old 04-19-08, 07:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
why not the non-unique indexes, poots?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 04-19-08, 10:48
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Because unique indexes can be (implicitly) added as constraints. Non unique indexes are not constraints so cannot be added as part of the create table statement. T-SQL I'm talking about here - I would imagine (but don't know since I am one of Bill's b*tches) that most RDBMSs are the same.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #10 (permalink)  
Old 04-19-08, 13:05
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
they aren't

which is why adding all indexes via ALTER is more portable

for most people, e.g. yourself, this matters little, as you operate within a single conceptual DBA silo of thought, and never port to heterogeneous platforms

eh
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 04-19-08, 13:29
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Which RDMBSs enforce unique constraints by a mechanism other than indexes? TMK all the major ones do.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #12 (permalink)  
Old 04-19-08, 16:37
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
far as i know, yes, they all do
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #13 (permalink)  
Old 04-21-08, 12:46
tmpuzer tmpuzer is offline
Registered User
 
Join Date: Mar 2008
Posts: 52
Quote:
Originally Posted by r937
so, which one do you prefer?
I prefer specifying the constraints in-line (inside the create table statement), as it keeps the definition all together as one unit. But I was wondering what motivation people had for the out-of-line style. One thing I don't like about the in-line option is I feel it clutters the create table statement so it's harder to see at a glance what columns the table has.
Reply With Quote
  #14 (permalink)  
Old 04-21-08, 15:50
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Nope - that isn't necessary. This is my create table template. I keep ALL constraints at the bottom, in strict order (PK, Alt Key, check, foreign key), except defaults - but only because I can't move these. It is also more consistent - if your key is composite you can't declare it as part of the column definition anyway.

Code:
IF NOT EXISTS (SELECT NULL FROM sys.schemas WHERE name = 's_name') BEGIN
    EXEC ('CREATE SCHEMA s_name')
END

IF EXISTS (SELECT NULL FROM sys.tables WHERE object_id = OBJECT_ID('s_name.t_name')) BEGIN
    DROP TABLE s_name.t_name
END

CREATE TABLE s_name.t_name
    (
        c_name        INT IDENTITY    NOT NULL
        , c_2_name    VARCHAR(10)   NULL            CONSTRAINT df_c_2_name DEFAULT ('def val')
        , CONSTRAINT pk_t_name_u_c                 PRIMARY KEY CLUSTERED (c_name) WITH (FILLFACTOR = 100)
        , CONSTRAINT ix_t_name_c_2_name_u_nc  UNIQUE NONCLUSTERED (c_2_name) WITH (FILLFACTOR = 100)
        , CONSTRAINT ck_t_name_c_2_name         CHECK (c_2_name IN('def val')) 
        , CONSTRAINT fk_t_name_t_2_name          FOREIGN KEY (c_2_name) REFERENCES s_name.t_2_name (c_2_name) --ON DELETE CASCADE
    )
GO
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #15 (permalink)  
Old 04-21-08, 17:51
blindman blindman is offline
World Class Flame Warrior
 
Join Date: Jun 2003
Location: Ohio
Posts: 11,726
This whole in-depth discussion of how to format DDL statements reminds me of one of Shakespeare's plays.

Much ado about nothing.

<Insert rim shot here>
__________________
If it's not practically useful, then it's practically useless.

blindman
www.chess.com: "sqlblindman"
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On