Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old May 15th, 2005, 10:37 AM
dean1401 dean1401 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 10 dean1401 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 25 m 24 sec
Reputation Power: 0
Sql Uniqueidentifier GUIDS

Hi all,
I posted a few threads earlier, but have since discovered my problem relates to "uniqueidentifiers".

I have a Users table which has a uniqueidentifier called "UserID" as the PK. I have created a new table and I want to use the "UserID" from my Users table as the my FK. Through many hours of searching the net, it seems these are called GUIDs................... does anyone know how to reference this "UserID" for my new table??????? It does not insert automatically!!!!!!

Thanks

Reply With Quote
  #2  
Old May 15th, 2005, 11:55 AM
trubolotta trubolotta is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: Wheeling, IL
Posts: 171 trubolotta User rank is Private First Class (20 - 50 Reputation Level)trubolotta User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 5 Days 11 h 45 m 27 sec
Reputation Power: 4
The SQL Server function you are looking for to generate a uniqueidentifier is NEWID(). Use this as the default for that column or in an insert statement, that is VALUE (NEWID()). The data type to specify when using this as a foreign key is uniqueidentifier but just don't specify a default.

Hope this helps.

Reply With Quote
  #3  
Old May 15th, 2005, 03:40 PM
dean1401 dean1401 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 10 dean1401 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 25 m 24 sec
Reputation Power: 0
Hi Trubollota

That is exactly what I have done, but for some reason I keep getting the error that a NULL value cannot be inserted into this column. My thinking was that SQL handles the inserting of this column based on the PK and FK setup! Am I correct in thinking this is the right way or am I still missing a step ie do I have to ALTER the procedure manually or something????? It's driving me nuts!!! Smile!

Thanks for the reply!



Quote:
Originally Posted by trubolotta
The SQL Server function you are looking for to generate a uniqueidentifier is NEWID(). Use this as the default for that column or in an insert statement, that is VALUE (NEWID()). The data type to specify when using this as a foreign key is uniqueidentifier but just don't specify a default.

Hope this helps.

Reply With Quote
  #4  
Old May 15th, 2005, 06:39 PM
trubolotta trubolotta is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2005
Location: Wheeling, IL
Posts: 171 trubolotta User rank is Private First Class (20 - 50 Reputation Level)trubolotta User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 5 Days 11 h 45 m 27 sec
Reputation Power: 4
You may want to examine the following scripts. I created both tables manually setting the default value of UserID in MyUserTBL to NEWID().

Code:
/****** Object:  Table [dbo].[MyUserTBL]	Script Date: 5/15/05 5:34:27 PM ******/
CREATE TABLE [dbo].[MyUserTBL] (
 [UserID] [uniqueidentifier] NOT NULL ,
 [UserName] [varchar] (32) NOT NULL 
) ON [PRIMARY]
GO
/****** Object:  Table [dbo].[MyOtherTBL]	Script Date: 5/15/05 5:34:28 PM ******/
CREATE TABLE [dbo].[MyOtherTBL] (
 [MOTPKID] [int] IDENTITY (1, 1) NOT NULL ,
 [UserFKID] [uniqueidentifier] NOT NULL ,
 [SomeVarCharData] [varchar] (32) NOT NULL 
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[MyUserTBL] WITH NOCHECK ADD 
 CONSTRAINT [DF_MyUserTBL_UserID] DEFAULT (newid()) FOR [UserID],
 CONSTRAINT [PK_MyUserTBL] PRIMARY KEY  NONCLUSTERED 
 (
  [UserID]
 )  ON [PRIMARY] 
GO
ALTER TABLE [dbo].[MyOtherTBL] WITH NOCHECK ADD 
 CONSTRAINT [PK_MyOtherTBL] PRIMARY KEY  NONCLUSTERED 
 (
  [MOTPKID]
 )  ON [PRIMARY] 
GO
ALTER TABLE [dbo].[MyOtherTBL] ADD 
 CONSTRAINT [FK_MyOtherTBL_MyUserTBL] FOREIGN KEY 
 (
  [UserFKID]
 ) REFERENCES [dbo].[MyUserTBL] (
  [UserID]
 )
GO


The tables are very simple and you may want to compare them to yours. I added sample data to MyUserTBL just adding some names and then ran the following insert to move the data to MyOtherTBL:

Code:
INSERT INTO dbo.MyOtherTBL
 (
 UserFKID,
 SomeVarCharData
 )
SELECT dbo.MyUserTBL.UserID,
 dbo.MyUserTBL.UserName
FROM dbo.MyUserTBL

and it worked, correctly inserting the UserFKID foreign key from UserID and creating the new primary key. This at least establishes it can be done.

Reply With Quote
  #5  
Old May 17th, 2005, 03:38 PM
dean1401 dean1401 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 10 dean1401 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 25 m 24 sec
Reputation Power: 0
Hi Trubollota

Thanks a lot for that extremely detailed solution!
I am currently trying to implement a variation of it!
Will post a victory or failure cry! Thanks again



Quote:
Originally Posted by trubolotta
You may want to examine the following scripts. I created both tables manually setting the default value of UserID in MyUserTBL to NEWID().

Code:
/****** Object: Table [dbo].[MyUserTBL]	Script Date: 5/15/05 5:34:27 PM ******/
CREATE TABLE [dbo].[MyUserTBL] (
[UserID] [uniqueidentifier] NOT NULL ,
[UserName] [varchar] (32) NOT NULL 
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[MyOtherTBL]	Script Date: 5/15/05 5:34:28 PM ******/
CREATE TABLE [dbo].[MyOtherTBL] (
[MOTPKID] [int] IDENTITY (1, 1) NOT NULL ,
[UserFKID] [uniqueidentifier] NOT NULL ,
[SomeVarCharData] [varchar] (32) NOT NULL 
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[MyUserTBL] WITH NOCHECK ADD 
CONSTRAINT [DF_MyUserTBL_UserID] DEFAULT (newid()) FOR [UserID],
CONSTRAINT [PK_MyUserTBL] PRIMARY KEY NONCLUSTERED 
(
[UserID]
) ON [PRIMARY] 
GO
ALTER TABLE [dbo].[MyOtherTBL] WITH NOCHECK ADD 
CONSTRAINT [PK_MyOtherTBL] PRIMARY KEY NONCLUSTERED 
(
[MOTPKID]
) ON [PRIMARY] 
GO
ALTER TABLE [dbo].[MyOtherTBL] ADD 
CONSTRAINT [FK_MyOtherTBL_MyUserTBL] FOREIGN KEY 
(
[UserFKID]
) REFERENCES [dbo].[MyUserTBL] (
[UserID]
)
GO


The tables are very simple and you may want to compare them to yours. I added sample data to MyUserTBL just adding some names and then ran the following insert to move the data to MyOtherTBL:

Code:
INSERT INTO dbo.MyOtherTBL
(
UserFKID,
SomeVarCharData
)
SELECT dbo.MyUserTBL.UserID,
dbo.MyUserTBL.UserName
FROM dbo.MyUserTBL

and it worked, correctly inserting the UserFKID foreign key from UserID and creating the new primary key. This at least establishes it can be done.

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > Sql Uniqueidentifier GUIDS


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT