Click here to Skip to main content
15,921,989 members
Home / Discussions / Database
   

Database

 
AnswerRe: check data integrity using triggers Pin
Pete O'Hanlon20-Nov-06 4:33
mvePete O'Hanlon20-Nov-06 4:33 
GeneralRe: check data integrity using triggers Pin
drequinox20-Nov-06 7:27
drequinox20-Nov-06 7:27 
QuestionRe: check data integrity using triggers Pin
drequinox20-Nov-06 9:50
drequinox20-Nov-06 9:50 
QuestionAccess 2000-Jet/VB6 - SQL Query (HELP- SQL Gurus) Pin
tintinchasm19-Nov-06 22:34
tintinchasm19-Nov-06 22:34 
AnswerRe: Access 2000-Jet/VB6 - SQL Query (HELP- SQL Gurus) Pin
rah_sin20-Nov-06 0:02
professionalrah_sin20-Nov-06 0:02 
GeneralRe: Access 2000-Jet/VB6 - SQL Query (HELP- SQL Gurus) Pin
tintinchasm20-Nov-06 6:11
tintinchasm20-Nov-06 6:11 
QuestionENUM Pin
thathvamsi19-Nov-06 21:35
thathvamsi19-Nov-06 21:35 
AnswerRe: ENUM Pin
Leah_Garrett22-Nov-06 13:20
Leah_Garrett22-Nov-06 13:20 
So you want col1 to only be able to have a value equivalent to one of the values used in the e1 column called id? This sounds like a look-up table.

One way to do this would be to use foreign keys. You specify col1 as a foreign key to table e1. I have called col1 e1ID and made it an int.

So create the e1 table using a primary key:
CREATE TABLE e1
( id INT NOT NULL,
name VARCHAR(20) NOT NULL,
description VARCHAR(20) NOT NULL,
enabled BIT NOT NULL,
 CONSTRAINT [PK_e1] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]


Create the T1 table with a foreign key relationship to e1:
CREATE TABLE [T1](
	[T1ID] [int] NOT NULL,
	[E1ID] [int] NOT NULL,
	name VARCHAR(20) NOT NULL,
	description VARCHAR(20) NOT NULL,
	enabled BIT NOT NULL
) ON [PRIMARY]
GO
ALTER TABLE [T1]  WITH NOCHECK ADD  CONSTRAINT [FK_e1] FOREIGN KEY([E1ID])
REFERENCES E1 ([ID])
GO


Insert some rows into e1 for testing:
INSERT INTO [e1] ([id],[name],[description],[enabled])
     VALUES (1, 'Test1','Desc1',1)

INSERT INTO [e1] ([id],[name],[description],[enabled])
     VALUES (2, 'Test2','Desc2',1)

INSERT INTO [e1] ([id],[name],[description],[enabled])
     VALUES (3, 'Test3','Desc3',1)


Test inserting into T1. First insert will work, second will fail:
INSERT INTO T1 ([T1ID],[E1ID],[name],[description],[enabled])
     VALUES(1,2,'TTest1','TDesc1',1)

INSERT INTO T1 ([T1ID],[E1ID],[name],[description],[enabled])
     VALUES(1,8,'TTest8','TDesc8',1)

QuestionDB Owner - Diagrams Pin
MatthysDT19-Nov-06 19:59
MatthysDT19-Nov-06 19:59 
AnswerRe: DB Owner - Diagrams Pin
Leah_Garrett22-Nov-06 13:24
Leah_Garrett22-Nov-06 13:24 
QuestionSQL error Pin
walalawll19-Nov-06 15:31
walalawll19-Nov-06 15:31 
AnswerRe: SQL error Pin
Mike Dimmick20-Nov-06 0:52
Mike Dimmick20-Nov-06 0:52 
GeneralRe: SQL error Pin
walalawll29-Nov-06 21:22
walalawll29-Nov-06 21:22 
QuestionTimeout exception error Pin
Lior S19-Nov-06 4:04
Lior S19-Nov-06 4:04 
AnswerRe: Timeout exception error Pin
Colin Angus Mackay19-Nov-06 4:58
Colin Angus Mackay19-Nov-06 4:58 
AnswerRe: Timeout exception error Pin
ThaScorpion19-Nov-06 13:44
ThaScorpion19-Nov-06 13:44 
QuestionDatatype mismatch...column type date/time Pin
Skcheng18-Nov-06 17:09
Skcheng18-Nov-06 17:09 
AnswerRe: Datatype mismatch...column type date/time Pin
Rob Graham19-Nov-06 6:19
Rob Graham19-Nov-06 6:19 
GeneralRe: Datatype mismatch...column type date/time [modified] Pin
Skcheng19-Nov-06 8:57
Skcheng19-Nov-06 8:57 
GeneralRe: Datatype mismatch...column type date/time Pin
Skcheng19-Nov-06 11:06
Skcheng19-Nov-06 11:06 
AnswerRe: Datatype mismatch...column type date/time Pin
Scott Holt20-Nov-06 16:17
Scott Holt20-Nov-06 16:17 
GeneralRe: Datatype mismatch...column type date/time Pin
Skcheng20-Nov-06 16:29
Skcheng20-Nov-06 16:29 
QuestionSQL trigger , Pin
drequinox18-Nov-06 4:48
drequinox18-Nov-06 4:48 
QuestionExtracting date part in SQL server Pin
ThaScorpion17-Nov-06 14:41
ThaScorpion17-Nov-06 14:41 
AnswerRe: Extracting date part in SQL server Pin
Janani Divya17-Nov-06 18:00
Janani Divya17-Nov-06 18:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.