Foriegn Key Restraint

Jan 25, 2000

Here is a table structure, with 2 contrainst applied to the same columns. Can this possible cause a problem.

CREATE TABLE dbo.app_person
(
appid int NOT NULL,
source char(1) NOT NULL,
first char(16) NULL,
mi char(1) NULL,
last char(24) NULL,
suffix char(4) NULL,
ssn char(9) NULL,
dob datetime NULL,
marital_status decimal(1,0) NULL,
housing char(1) NULL,
mortgage_holder char(20) NULL,
mortgage_payment decimal(9,2) NULL,
auto_holder char(20) NULL,
auto_account char(16) NULL,
auto_balance decimal(9,2) NULL,
auto_payment decimal(9,2) NULL,
de_datetime datetime NULL,
de_clerk char(10) NULL,
excl_other_income bit DEFAULT 0 NOT NULL,
pre_housing char(1) NULL,
same_area bit DEFAULT 0 NOT NULL,
income_type decimal(2,0) NULL,
other_income_type decimal(2,0) NULL,
relationship decimal(2,0) NULL,
line_of_work bit DEFAULT 0 NOT NULL,
selected_bureau_id decimal(1,0) NULL,
excl_income bit DEFAULT 0 NOT NULL,
CONSTRAINT PK_app_person PRIMARY KEY NONCLUSTERED (appid,source),
CONSTRAINT IX_lnapp100 UNIQUE NONCLUSTERED (appid,source),
CONSTRAINT FK_lnapp100_lnapp000 FOREIGN KEY (appid) REFERENCES dbo.app_main (appid)
)

View 1 Replies


ADVERTISEMENT

Dropping An Old Foreign Key Restraint?

Jul 6, 2007

This forum looked like the only one where I may get an answer to this question.



I built a database with a set of tables in Sql Server 2005 sp2.

I later had to refactor the data which led to removing one table, renaming another table, and moving some data around.

Now when I attempt to write my test code and execute my initialization logic (which clears data out of the tables) I get a foreign key restraint error when trying to delete one of my records. However the FK restraint is an old one that refers to a table that no longer exists. I have looked at all of my tables in the current design and the constraint referenced in the exception does not show up on any of the tables.



How things changed: I had DesktopItems tie to a DataEntry table with a foreign key constraint. I now have DesktopItems point to WorkItem ( the orgininal table renamed ) and it is on the delete of items from DesktopItems that I get the old FK constraint. The really odd part about this is if I go into the table and manually delete the record, I do NOT get the constraint error!!!



In the database that I'm getting the error, I implemented the changes by deleting all tables and SPROCS and then executing scripts to rebuild the new design.



Is there a system SPROC that I can execute to show and delete this item?

View 1 Replies View Related

Foriegn Key

Jan 17, 2006

please tell me how to make a foreign key in Ms SQL7

View 3 Replies View Related

Primary Key And Foriegn Key?

Dec 25, 2006

What is a Primary Key and Foriegn Key and how do they relate? Also, if I have 18 tables...how do I know the tables relate to another. Thanks.

View 4 Replies View Related

Primary Key + Foriegn Key

Nov 12, 2007

help needed,

sorry for such a basic querry please help me.

i have created 2 tables
both of them has different primary keys
now in first table there is also a refernece for primary key of second table

1) how do i make these both primary keys autoincrement
2) how do i make refernce to primary key of second table

like
table1
itemid itemrate itemcode itemdesscriotion

table2
productid itemid purchaseinvoice invoice amount

also kindly direct me to some quick and easy tutorials on SQL


thanks all for your help.

View 1 Replies View Related

Error While Trying To Save A Foriegn Key Relationship

Feb 6, 2008

Hi
  I have a table called ClientPlan and in which PlanId is the Primary key. another table called ClientSources1 and i have a column called planId.. and i am trying to set that as Foriegn key so that if i delete a record in the ClientPlan table i want the record to be deleted in Clientsources too.. i have tried settting the Foreign in the clientsources table for ClientPlan to Cascade update and delete.. but that doesnt work...and i get the error below... 
ClientPlan' table saved successfully'ClientSources1' table- Unable to create relationship 'FK_ClientSources1_ClientPlan'.  Introducing FOREIGN KEY constraint 'FK_ClientSources1_ClientPlan' on table 'ClientSources1' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.Could not create constraint. See previous errors.
 
any help will be appreciated.
Regards
Karen

View 2 Replies View Related

HELP! Synchronize Db And Maintain Foriegn Key Relations?

Jan 30, 2007

I have to synchronize 2 databases hourly but am having difficulty maintaining foreign key relations. These tables use auto-increment columns as primary keys, with child records in other tables related with foreign keys. I can't change the way the local software uses primary or foreign keys as it is hardcoded in the local app. (microsoft retail management system)..(however the web-remote app is easily customized). I am using CDB synchronizer to sync the two databases because the remote one is mysql.

Example tables layout:
Items table has auto-increment primary key 'id'
TransactionEntry table has its own auto-increment primary key 'id' and a foreign key 'item_id'

Example of how remote and local database foreign key relations are incorrect after sync using CDB synchronizer:
8:00am -first installation of database-'item' tables auto-increment 'id' columns match with id last record value of '6'

locally the following products are added:

11001 short sleeve t---gets added with primary key in 'item' table 'id' of '7'

11002 long sleeve t----gets added with primary key in 'item' table 'id' '8'

remotely the following products are added:

21001 hipster jeans- --gets added with primary key in 'item' table 'id' of '7'

31001 overalls---gets added with primary key in 'item' table 'id' '8'

remotely someone orders 21001..so TransactionEntry table records sale of "item_id" of '7', but after synch with our local server,

product with "item_id" of '7' is "short sleeve t".

9:00 -synch takes place...item_id foreign key isn't accurate because of independent auto-increment values..

whenever a product is ordered, the TransactionEntry table will record the product's ID column thats available in it's own local copy... after synch, the 'item_id' field will not match the 'Item' table id field and the data about the transaction's product is lost.

I have read of solutions involving staging/temporary tables to cascade update foreign keys before synching into main database, but hopefully there is a more elegant solution for this. If this is only way, will it be reliable? foreign key mix-match seems like could cause havoc.

View 2 Replies View Related

Loading Tables With Foriegn Key Contraints

Aug 31, 2006

Hi,

I am having trouble loading tables (within the same data flow) that have a foriegn key relationship defined between them. For instance:

Table A is a parent (one side of the relationship) to Table B (many side of the relationship).

I am trying to load Table A first within the data flow and then Table B after, but I get the following error:

[OCMD EntityRole Insert [2666]] Error: An OLE DB error has occurred. Error code: 0x80040E2F. An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80040E2F Description: "The statement has been terminated.". An OLE DB record is available. Source: "Microsoft SQL Native Client" Hresult: 0x80040E2F Description: "The INSERT statement conflicted with the FOREIGN KEY constraint "FK_EntityRole_Entity". The conflict occurred in database "ODS", table "dbo.Entity", column 'EntityGuid'.".

I am currently using OLE DB commands to perform the inserts, I load table A and move on to then load Table B, I can see the records in Table A before trying to load Table B but for some reason Table B load still fails.

I was thinking maybe this has something to do with the transaction setting or Isolation level but have played with this to no avail (currently everything is the default - supported/serializable). Also, I am thinking maybe because the OLE DB commands are creating two seperate connections (they are using the same connection manager) the second one is unable to see the transactions from the other (first) connection (Table A)?

Is there a way around this without dropping (disabling) forigen keys before the load and adding them back in after? Would like to avoid this?

I would also like to avoid reading the data source multiple times. Everything I need is in the one source so I would like to populate multiple tables from the one source data stream instead of reading the same data 2,3 or 4 times etc.

Seems to me there must be a simple explanation/solution for this but I'm stuck at this point?

P.S.

I was intially using OLE DB destinations (because they are much faster) and was having the same issue, which made sense because the OLE DB destinations do not let you pass the data stream on so I had to multi cast to the destinations so they were loading at the same time. I would rather use the OLE DB destinations so if you have any ideas around how I could do this using those components that would be appreciated too!

Thanks!

View 3 Replies View Related

WHY DO FORIEGN KEYS HAVE TO BE UNIQUE OR HAVE A CONSTRAINT?

Oct 9, 2007

How do I go about protecting rows from deletion in this scenerio?
Rule 1 The Administrator Users Account may not be deleted
Rule 2 All Groups have Administrator as a member, and the Administrator cannot be removed.
Rule 3 All Groups have the Administrators Group as a member, and the Administrators Group cannot be removed.



Four tables:

Users Table (
UID bigint Identity seeded with 1234 Primary key
UserID varchar(30) NOT NULL UNIQUE

)
INSERT FIRST RECORD (this record needs to be protected from deletion)
UID = 1234
UserID='Admininstrator'

INSERT FIRST RECORD (this record and others can be deleted)
UID = 1235
UserID='Test User 1'

Groups Table (
GID bigint Identity seeded with 1234 Primary key
GroupName varchar(30) NOT NULL UNIQUE
)

INSERT FIRST RECORD (this record needs to be protected from deletion)
GID = 1234
UserID='Admininstrators'

INSERT SECOND RECORD (this record and others can be deleted)
GID = 1235
UserID='Test Group 1'

Group_Members Table (
GID bigint NOT NULL //points to the group's ID and can't be unique
UID bigint NOT NULL //points to the members UserID and can't be unique
)
INSERT FIRST RECORD (this record needs to be protected from deletion because UID points to the Administrator)
GID = 1234
UID = 1234

INSERT SECOND RECORD (this record and others can be deleted because UID does not point to the Administrator.)
GID = 1234
UID = 1235


Group_Group_Members Table (
GID bigint NOT NULL //points to the group's ID and can't be unique
GGID bigint NOT NULL //points to the group members GID and can't be unique
)
INSERT FIRST RECORD (this record needs to be protected from deletion because GGID points to the Administrators Group.)
GID = 1234
GGID = 1234

INSERT SECOND RECORD (this record and others can be deleted because GGID does not point to the Administrators Group.)
GID = 1234
GGID = 1235


I have tried using foriegn keys, constraints an every thing else, but I hit a brick wall because FK requires the ke to be primary (btw is UNIQUE).
Any help would be appreciated.

View 2 Replies View Related

Data Table Foriegn Key Problem

Aug 26, 2007

I am just learning how to program in SQL, so please be patient with me


ok, here it is, i am getting the following errors with my script when i try to execute, and although i realize that this is not the best style to write my script in, i am trying to learn why this is erroring. the errors are:


Msg 1769, Level 16, State 1, Line 9

Foreign key 'Employee2Job_Title' references invalid column 'Title' in referencing table 'Employee'.

Msg 1750, Level 16, State 0, Line 9

Could not create constraint. See previous errors.



now i realize that the second error is because of the first, and that if i can fix the first, the second will go away. Thanks for the help.

Here is my script



use inventory

go

CREATE TABLE Job_Title

(Job_Title_Title char(25) NOT NULL ,

Job_Title_EEO1_Classification char(25) ,

Job_Title_Job_Description Varchar(45) ,

Job_Title_Exempt_Status Varchar(15)

, PRIMARY KEY (Job_Title_Title)

);

CREATE TABLE Employee

(Employee_Emp_ID integer NOT NULL ,

Employee_last_name varchar(15) ,

Employee_first_name varchar(15) ,

Employee_address varchar(30) ,

Employee_city varchar(15) ,

Employee_state char(2) ,

Employee_Telephone_area_code char(3) ,

Employee_Telephone_number char(8) ,

Employee_EEO1_Classification char(25) ,

Employee_Hire_Date char(8) ,

Employee_Salary char(6) ,

Employee_Gender Varchar(1) ,

Employee_Age char(2) ,

Employee_Title char(25)

, PRIMARY KEY (Employee_Emp_ID)

, constraint Employee2Job_Title FOREIGN KEY (Title

) REFERENCES Job_Title

);



Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

(1, 'Edelman', 'Glenn', '175 Bishop Lane', 'La Jolla', 'CA', 619, '555-0199', 'Sales Workers', 10/7/2003, 21500.00, 'M', 64, 'Cashier')

Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

(2, 'McMullen', 'Eric', '762 Church Street', 'Lemon Grove', 'CA', 619, '555-0133', 'Sales Workers', 11/1/2002, 13500.00, 'M', 20, 'Bagger')

Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

(3, 'Slenj', 'Raj', '123 Torrey Drive', 'North Clairmont', 'CA', 619, '555-0123', 'Officials & Managers', 6/1/2000, 48000.00, 'M', 34, 'Assistant Manager')

Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

(4, 'Broun', 'Erin', '2045 Parkway Apt 2b', 'Encinitas', 'CA', 760, '555-0100', 'Sales Workers', 3/12/2003, 10530.00, 'F', 24, 'Bagger - 30 hours/wk')

Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

(5, 'Carpenter', 'Donald', '927 Second Street', 'Encinitas', 'CA', 619, '555-0154', 'Office/Clerical', 11/1/2003, 15000.00, 'M', 18, 'Stocker')

Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

(6, 'Esquivez', 'David', '10983 North Coast Highway Apt 902', 'Encinitas', 'CA', 760, '555-0108', 'Operatives (Semi skilled)', 7/25/2003, 18500.00, 'M',25, 'Asst. - Butchers & Seafood Specialists')

Insert INTO Employee (Emp_ID, Last_name, First_name, Address, City, State, Telephone_area_code, Telephone_number, EEO1_Classification, Hire_date, Salary, Gender, Age, Title) Values

(7, 'Sharp', 'Nancy', '10793 Montecino Road', 'Ramona', 'CA', 858, '555-0135', 'Sales Workers', 7/12/2003, 21000.00, 'F', 24, 'Cashier')

Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

('Accounting Clerk', 'Office/Clerical', 'Computes, Classifies, records, and verifies numerical data for use in maintaining accounting records.', 'Non-Exempt')

Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

('Assistant store manager', 'Officials & Mangers', 'Supervises and coordintes activities of workers in department of food store. Assist store manager in daily operations of store.', 'Exempt')

Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

('Bagger', 'Sales Worker', 'Places customer orders in bags. Performs carry out duties for customers.', 'Non-Exempt')

Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

('Cashier', 'Sales Worker', 'Operates Cash register to itemize and total customers purchases in grocercy store', 'Non-Exempt')

Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

('Computer Support Specialist', 'Technician', 'Installs, Modifies, and makes minor repairs to personal computer hardware and software systems and provides technical assistance and training to system users.', 'Non-Exempt')

Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

('Director of Finance & Accounting', 'Officials & Mangers', 'Plans and directs finance and accounting activites for Kudlser Fine Foods.', 'Non-Exempt')

Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

('Retail Asst. Bakery & Pastry', 'Craft Workers (Skilled)', 'Obtains or prepares Bakery and Pastry items requested by customers in retail food store.', 'Non-Exempt')

Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

('Retail Asst. Butchers and Seafood Specialists', 'Operatives (Semi skilled)', 'Obtains or prepares Meat and Seafood items requested by customers in retail food store.', 'Non-Exempt')

Insert into Job_Title (Title, EEO1_Classification, Job_Description, Exempt_Status) Values

('Stocker', 'Office/Clerical', 'Stores, prices, and restocks merchandise displays in store.', 'Non-Exempt')

View 3 Replies View Related

Can I Add A Foriegn Key On A Indexed View To Reference Other Table

Nov 8, 2006

Is there anybody familiar with indexed view here?

Can I add a foriegn key on a indexed view to reference other table?

Thanks!

View 1 Replies View Related

SS 2005 Express- Allowing Null Values And Foriegn Key Constraints

Dec 28, 2006

Hello,I have a foreign key constraint between two tables (Appointments andMissedAppointmentReasons) and I'd like to allow null values in Appointmentstable for the field containing the MissedAppointmentReason, but currently, Iget a Foreign Key Constraint Error when I try to add a record to theAppointments table.Any ideas how I can have the null values but still maintain the Foreign KeyConstraint?Thanks!Rick

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved