Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Create Tables

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2009-08-03 : 20:47:05
Hello,

I am creating the tables of SQL 2008 database.
Most of the code is mine but 4 of the tables code came from another project.

The way I and the other project creates tables and relationships is quite different:
1) They use the following a lot:
set quoted_identifier off
go
set ansi_nulls on
go

2) They define the constrains after the tables have been created using an Alter command.

3) They always have:
with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]

I would like to know if I am doing something wrong ...
Is there is a correct way to do everything the same way?

EXAMPLES

How I create tables >

use MyDB;
go

create table dbo.Tags
(
Id uniqueidentifier not null rowguidcol
constraint PK_Tag primary key clustered,
GroupId uniqueidentifier not null,
constraint FK_Tags_Groups
foreign key(GroupId)
references Groups(Id)
on delete cascade,
[Name] nvarchar(40) not null,
) -- Tags

create table dbo.Posts
(
Id uniqueidentifier not null rowguidcol
constraint PK_Post primary key clustered,
Body nvarchar(max) not null,
Created datetime not null,
Excerpt nvarchar(max) not null,
[File] varbinary(max) filestream default(0x),
IPaper bit not null default 0,
Published bit not null default 0,
Title nvarchar(200) not null,
Updated datetime not null
) -- Posts

create table dbo.PostsTags
(
PostId uniqueidentifier not null,
TagId uniqueidentifier not null,
constraint PK_PostsTags
primary key clustered (PostId, TagId),
constraint FK_PostsTags_Posts
foreign key(PostId)
references Posts(Id)
on delete cascade,
constraint FK_PostsTags_Tags
foreign key(TagId)
references Tags(Id)
on delete cascade
) -- PostsTags




How the other project creates tables >

-- Applications
set ansi_nulls on
go
set quoted_identifier on
go
create table dbo.Applications
(
Id uniqueidentifier ROWGUIDCOL not null
constraint DF_Application_Id default (newid()),
[Name] nvarchar(256) not null,
Description nvarchar(256) null,
constraint PK_Application primary key clustered
(
Id ASC
) with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]
go

-- Roles
set ansi_nulls on
go
set quoted_identifier on
go
create table dbo.Roles
(
Id uniqueidentifier rowguidcol not null
constraint DF_Role_Id default (newid()),
[Name] nvarchar(50) not null,
ApplicationId uniqueidentifier not null,
constraint PK_Role primary key clustered
(
Id asc
) with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]
go

-- Users
set ansi_nulls on
go
set quoted_identifier on
go
create table dbo.Users
(
Id uniqueidentifier rowguidcol not null
constraint DF_User_Id default (newid()),
ApplicationId uniqueidentifier not null,
[Name] nvarchar(50) null,
CreationDate datetime null,
Username nvarchar(50) not null,
constraint PK_User primary key clustered
(
Id asc
) with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]
go

-- UsersRoles
set ansi_nulls on
go
set quoted_identifier on
go
create table dbo.UsersRoles
(
UserId uniqueidentifier not null,
RoleId uniqueidentifier not null,
constraint PK_UsersInRoles primary key clustered
(
UserId asc,
RoleId asc
) with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]
go

-- Profiles
set ansi_nulls on
go
set quoted_identifier on
go
create table dbo.Profiles
(
UserId uniqueidentifier rowguidcol not null constraint DF_Profile_UserId default (newid()),
PropertyNames ntext null,
PropertyValuesString ntext null,
PropertyValuesBinary image null,
LastUpdatedDate datetime not null,
constraint PK_Profile primary key clustered
(
UserId asc
) with (pad_index = off, statistics_norecompute = off, ignore_dup_key = off, allow_row_locks = on, allow_page_locks = on) on [primary]
) on [primary]
go

-- Roles
alter table dbo.Roles with check add constraint FK_Roles_Applications foreign key(ApplicationId) references dbo.Applications(Id)
on update cascade
on delete cascade
go

-- Roles
alter table dbo.Roles check constraint FK_Roles_Applications
go
alter table dbo.Users with check add constraint FK_Users_Applications foreign key(ApplicationId) references dbo.Applications(Id)
go

-- Users
alter table dbo.Users check constraint FK_Users_Applications
go
alter table dbo.UsersRoles with check add constraint FK_UsersRoles_Roles foreign key(RoleId) references dbo.Roles(Id)
on update cascade
on delete cascade
go

-- UsersRoles
alter table dbo.UsersRoles check constraint FK_UsersRoles_Roles
go
alter table dbo.UsersRoles with check add constraint FK_UsersRoles_Users foreign key(UserId) references dbo.Users(Id)
on update cascade
on delete cascade
go

-- UsersRoles
alter table dbo.UsersRoles check constraint FK_UsersRoles_Users
go
alter table dbo.Profiles with check add constraint FK_Profiles_Users foreign key(UserId) references dbo.Users(Id)
go
alter table dbo.Profiles check constraint FK_Profiles_Users



Thank You,
Miguel
   

- Advertisement -