foreign-keys

Can you have a Foreign Key onto a View of a Linked Server table in SQLServer 2k5?

淺唱寂寞╮ 提交于 2019-11-29 04:43:51
I have a SQLServer with a linked server onto another database somewhere else. I have created a view on that linked server create view vw_foo as select [id], [name] from LINKEDSERVER.RemoteDatabase.dbo.tbl_bar I'd like to to the following alter table [baz] add foo_id int not null go alter table [baz] with check add constraint [fk1_baz_to_foo] foreign key([foo_id]) references [dbo].[vw_foo] ([id]) go But that generates the error: "Foreign key 'fk1_baz_to_foo' references object 'dbo.vw_foo' which is not a user table." If I try and put the foreign key directly onto the table using the following

How to use BULK INSERT when rows depend on foreign keys values?

余生长醉 提交于 2019-11-29 04:08:41
My question is related to this one I asked on ServerFault . Based on this, I've considered the use of BULK INSERT . I now understand that I have to prepare myself a file for each entities I want to save into the database. No matter what, I still wonder whether this BULK INSERT will avoid the memory issue on my system as described in the referenced question on ServerFault. As for the Streets table, it's quite simple! I have only two cities and five sectors to care about as the foreign keys. But then, how about the Addresses? The Addresses table is structured like this: AddressId int not null

Enforce a foreign-key constraint to columns of same table

自古美人都是妖i 提交于 2019-11-29 04:03:16
How to enforce a constraint of foreign key on columns of same table in SQL while entering values in the following table: employee : empid number, manager number (must be an existing employee) instanceOfObject CREATE TABLE TABLE_NAME ( `empid_number` int ( 11) NOT NULL auto_increment, `employee` varchar ( 100) NOT NULL , `manager_number` int ( 11) NOT NULL , PRIMARY KEY (`empid_number`), CONSTRAINT `manager_references_employee` FOREIGN KEY (`manager_number`) REFERENCES (`empid_number`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 Hope it helps! Oracle call this a self-referential integrity constraint.

Linq To SQL Without Explicit Foreign Key Relationships

懵懂的女人 提交于 2019-11-29 03:38:14
I am working with a few legacy tables that have relationships, but those relationships haven't been explicitly set as primary/foreign keys. I created a .dbml file using "Linq To Sql Classes" and established the proper Case.CaseID = CaseInfo.CaseID association. My resulting class is CasesDataContext. My Tables (One to many): Case ------------------ CaseID (int not null) MetaColumn1 (varchar) MetaColumn2 (varchar) MetaColumn3 (varchar) ... CaseInfo ------------------ CaseInfoID (int) CaseID (int nulls allowed) CaseInfoMeta (varchar) ... I'm new to LinqToSQL and am having trouble doing..

How to get EF6 to honor Unique Constraint (on FK) in Association/Relationship multiplicity?

为君一笑 提交于 2019-11-29 03:30:19
2019 Update / TLDR; switch to Entity Framework Core (or whatever else) While missing some "Features", EF Core properly honors Alternate Keys (aka Unique Constraints) in addition to Primary Keys and thus does a much better job of honoring Relational Algebra. YMMV otherwise; at least it supports many more SQL schemes correctly. This support added was in the (very outdated) EF Core 1.0 release .. a bit disappointing that the original EF never had this design(ed!) flaw addressed. This may be related to my other question - which seems to be that either: Entity Framework is a terrible Relational

Deleting a row with a self-referencing foreign key

大城市里の小女人 提交于 2019-11-29 03:22:59
I have a MySQL table whose definition is as follows: CREATE TABLE `guestbook` ( `Id` int(10) unsigned NOT NULL, `ThreadId` int(10) unsigned NOT NULL, PRIMARY KEY (`Id`), KEY `ThreadId` (`ThreadId`), CONSTRAINT `guestbook_ibfk_1` FOREIGN KEY (`ThreadId`) REFERENCES `guestbook` (`Id`) ) ENGINE=InnoDB; and currently there's only 1 row in the table: mysql> select * from guestbook; +-----+----------+ | Id | ThreadId | +-----+----------+ | 211 | 211 | +-----+----------+ The problem is that there's no way to delete this row without breaking the constraint. mysql> delete from guestBook; ERROR 1451

MySQL many-to-many relationship with FOREIGN KEYS

99封情书 提交于 2019-11-29 02:51:57
问题 I am trying to create a many-to-many relationship in my MySQL database. I have three tables: Films , Genres and Films_Genres . I am using the following code to set them up: CREATE TABLE Films ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), Title VARCHAR(255) ), CREATE TABLE Genres ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), Name VARCHAR(255) ), CREATE TABLE Films_Genres ( film_id INT NOT NULL, genre_id INT NOT NULL, PRIMARY KEY (film_id, genre_id), FOREIGN KEY (film_id) REFERENCES

How to use MySQL index columns?

天大地大妈咪最大 提交于 2019-11-29 02:35:36
问题 When do you use each MySQL index type? PRIMARY - Primary key columns? UNIQUE - Foreign keys? INDEX - ?? For really large tables, do indexed columns improve performance? 回答1: Primary The primary key is - as the name suggests - the main key of a table and should be a column which is commonly used to select the rows of this table. The primary key is always a unique key (unique identifier). The primary key is not limited to one column, for example in reference tables (many-to-many) it often makes

Foreign keys in MySQL?

孤人 提交于 2019-11-29 02:31:18
问题 I have been slowly learning SQL the last few weeks. I've picked up all of the relational algebra and the basics of how relational databases work. What I'm trying to do now is learn how it's implemented. A stumbling block I've come across in this, is foreign keys in MySQL. I can't seem to find much about the other than that they exist in the InnoDB storage schema that MySQL has. What is a simple example of foreign keys implemented in MySQL? Here's part of a schema I wrote that doesn't seem to

How to deal with mutually recursive inserts

江枫思渺然 提交于 2019-11-29 02:26:42
I have a model that defines mutually recursive tables: Answer questionId QuestionId text Question text correct AnswerId What do I need to do to actually insert a question? I need to know what the correct answer is first. But to insert an answer, I need to know what question it answers. I'm running Postgres, if it matters. The DDL is: CREATE TABLE answer ( id integer NOT NULL, -- answer id text character varying NOT NULL, -- answer text question_id bigint NOT NULL -- question id ); CREATE TABLE question ( id integer NOT NULL, -- question id question character varying NOT NULL, -- question text