foreign-keys

How to remove constraints from my MySQL table?

心已入冬 提交于 2019-11-27 17:02:03
I want to remove constraints from my table. My query is: ALTER TABLE `tbl_magazine_issue` DROP CONSTRAINT `FK_tbl_magazine_issue_mst_users` But I got an error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'constraint FK_tbl_magazine_issue_mst_users ' at line 1 Mysql has a special syntax for dropping foreign key constraints: ALTER TABLE tbl_magazine_issue DROP FOREIGN KEY FK_tbl_magazine_issue_mst_users I had the same problem and I got to solve with this code: ALTER TABLE `table_name` DROP FOREIGN

Why are foreign keys more used in theory than in practice?

落花浮王杯 提交于 2019-11-27 16:54:41
When you study relational theory foreign keys are, of course, mandatory. But in practice, in every place I worked, table products and joins are always done by specifying the keys explicitly in the query, instead of relying on foreign keys in the DBMS. This way, you could of course join two tables by fields that are not meant to be foreign keys, having unexpected results. Why do you think that is? Shouldn't DBMSs enforce that Joins and Products be made only by foreign keys? EDIT: Thanks for all the answers. It's clear to me now that the main reason for FKs is reference integrity. But if you

SQL DROP TABLE foreign key constraint

十年热恋 提交于 2019-11-27 16:45:36
If I want to delete all the tables in my database like this, will it take care of the foreign key constraint? If not, how do I take care of that first? GO IF OBJECT_ID('dbo.[Course]','U') IS NOT NULL DROP TABLE dbo.[Course] GO IF OBJECT_ID('dbo.[Student]','U') IS NOT NULL DROP TABLE dbo.[Student] marc_s No, this will not drop your table if there are indeed foreign keys referencing it. To get all foreign key relationships referencing your table, you could use this SQL (if you're on SQL Server 2005 and up): SELECT * FROM sys.foreign_keys WHERE referenced_object_id = object_id('Student') and if

Hibernate foreign key as part of primary key

别来无恙 提交于 2019-11-27 16:26:58
问题 I have to work with hibernate and not very sure how solve this problem, I've 2 table with a 1..n relationship like this: ------- TABLE_A ------- first_id (pk) second_id (pk) [other fields] ------- TABLE_B ------- first_id (pk)(fk TABLE_A.first_id) second_id (pk)(fk TABLE_A.second_id) third_id (pk) [other fields] How can I manage this with Hibernate??? I don't have idea how to manage the primary key for the second table... 回答1: There is an example which is completely similar to your case in

Django ForeignKey which does not require referential integrity?

喜你入骨 提交于 2019-11-27 16:25:43
问题 I'd like to set up a ForeignKey field in a django model which points to another table some of the time. But I want it to be okay to insert an id into this field which refers to an entry in the other table which might not be there. So if the row exists in the other table, I'd like to get all the benefits of the ForeignKey relationship. But if not, I'd like this treated as just a number. Is this possible? Is this what Generic relations are for? 回答1: This question was asked a long time ago, but

Foreign Key Creation issue in Oracle

依然范特西╮ 提交于 2019-11-27 16:21:23
When I try to create these two tables I get: "SQL Error: ORA-00904: "COLLECTIBLENUM": invalid identifier" I'm sure it's a noob error but I'm just not seeing it. Can someone please point out what I'm doing wrong? Thanks in advance. CREATE TABLE Collectibles( CollectibleNum Number(10) NOT NULL, CONSTRAINT collectibles_pk PRIMARY KEY(CollectibleNum)); Create table DiecastItems( DiecastName VARCHAR2(45) NOT NULL, DiecastCopy NUMBER(2) NOT NULL, DiecastScale VARCHAR2(25), ColorScheme VARCHAR2(25), DiecastYear NUMBER(4), CONSTRAINT diecastItem_pk PRIMARY KEY(DiecastName, DiecastCopy), CONSTRAINT

Django error. Cannot assign must be an instance

谁都会走 提交于 2019-11-27 15:58:56
问题 I get the following error when I try to run an insert into one of my tables. Cannot assign "1": "Team.department_id" must be a "Department" instance Admittedly I'm slightly unsure if I'm using the foreign key concept correctly. The insert I'm trying to run and a snippet from my models.py are given below. What I'm trying to do is that when someone wants to create a new team. They have to attach it to a department. Therefore the department ID should be in both sets of tables. new_team = Team(

What is the problem with foreign key cascade multiple paths and cycles?

江枫思渺然 提交于 2019-11-27 15:28:53
In MSSQL 2005 I just struck the infamous error message: Introducing FOREIGN KEY constraint XXX on table YYY may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Now, StackOverflow has several topics about this error message, so I've already got the solution (in my case I'll have to use triggers), but I'm curious as to why there is such a problem at all. As I understand it, there are basically two scenarios that they want to avoid - a cycle and multiple paths. A cycle would be where two tables have cascading

MySQL Relationships

半世苍凉 提交于 2019-11-27 14:54:55
I am trying to figure out how to structure this database. I have used Apple's core data before just fine, I'm just working on a different project now that requires MySQL. I am very new to MySQL so please go easy on me. :) For this example, let's say I have three tables, User , Device , and Location . Drawing it out, a Location can have many Device s, but the Device can only have one Location ; Each User has its primary key, UserID , of which I need to use to fetch the correct information. So how do I create a relationship like this here? I've heard of creating an index and a foreign key and I

Query to get all foreign key constraints in SQL Server 2000

和自甴很熟 提交于 2019-11-27 14:14:53
问题 I need a query for SQL Server 2000 to get a list of all foreign keys. Particularly all the foreign keys that point to a particular column. How do I write this query? 回答1: select * from sysobjects where xtype = 'F' That should do the trick and be compatible with SQL Server 2000, I hope! If you additionally need the table and column information in SQL Server 2000, it gets a bit more involved; you need to join the sysforeignkeys and syscolumns catalog views like so: select so.name 'foreign key