| |
|
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.
|
 |
|

04-18-08, 13:55
|
|
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?
|
|

04-18-08, 14:02
|
|
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"
|
|

04-18-08, 23:28
|
|
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.
|
|

04-19-08, 00:14
|
|
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"
|
|

04-19-08, 01:29
|
|
Registered User
|
|
Join Date: Mar 2008
Posts: 52
|
|
For DDL scripts (not application code) executed once on database creation.
|
|

04-19-08, 07:00
|
|
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?
|
|

04-19-08, 07:13
|
|
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.
|
|
|

04-19-08, 07:22
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
why not the non-unique indexes, poots?
|
|

04-19-08, 10:48
|
|
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.
|
|
|

04-19-08, 13:05
|
|
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 
|
|

04-19-08, 13:29
|
|
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.
|
|
|

04-19-08, 16:37
|
|
SQL Consultant
|
|
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
|
|
far as i know, yes, they all do
|
|

04-21-08, 12:46
|
|
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.
|
|

04-21-08, 15:50
|
|
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.
|
|
|

04-21-08, 17:51
|
|
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"
|
|
| 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
|
|
|
|
|