Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a foreign key to system tables

I want to create table with to columns:

IdRole
IdProcedure

the idea is that IdProcedure is a FK to sys.objects.

When I create this query:

SELECT * 
FROM sys.objects 
WHERE type='p'

it works fine, but this one:

ALTER TABLE dbo.CORE_ProcedureXRole
ADD CONSTRAINT FK_SysProcedure
FOREIGN KEY (IdProcedure)
REFERENCES sys.objects(object_id)

tells me:

Foreign key 'FK_SysProcedure' references invalid table 'sys.objects'.

like image 409
davibq Avatar asked May 11 '12 22:05

davibq


People also ask

How do you make a foreign key table?

To create a new table containing a foreign key column that references another table, use the keyword FOREIGN KEY REFERENCES at the end of the definition of that column. Follow that with the name of the referenced table and the name of the referenced column in parentheses.

How do I add a foreign key to an existing table in access?

Select one or more tables or queries and then click Add. After you have finished adding tables and queries to the Relationships document tab, click Close. Drag a field (typically the primary key) from one table to the common field (the foreign key) in the other table.

Can a foreign key be a table?

A FOREIGN KEY is a field (or collection of fields) in one table, that refers to the PRIMARY KEY in another table. The table with the foreign key is called the child table, and the table with the primary key is called the referenced or parent table.

Can we create foreign key on same table?

If you mean "can foreign key 'refer' to a primary key in the same table?", the answer is a firm yes as some replied.


1 Answers

sys.objects isn't a table. It's a system view backed by data stored in proprietary SQL Server format. If you want to make sure that the stored name is correct, add a TRIGGER for update and insert to handle the checking.

like image 127
Code Different Avatar answered Sep 30 '22 10:09

Code Different