foreign-keys

Drop all foreign keys in a table

余生长醉 提交于 2019-12-06 06:19:12
I had this script which worked in sql server 2005 -- t-sql scriptlet to drop all constraints on a table DECLARE @database nvarchar(50) DECLARE @table nvarchar(50) set @database = 'dotnetnuke' set @table = 'tabs' DECLARE @sql nvarchar(255) WHILE EXISTS(select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where constraint_catalog = @database and table_name = @table) BEGIN select @sql = 'ALTER TABLE ' + @table + ' DROP CONSTRAINT ' + CONSTRAINT_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where constraint_catalog = @database and table_name = @table exec sp_executesql @sql END It does not work in

How do I create a table with a two primary keys of two foreign keys?

心不动则不痛 提交于 2019-12-06 06:18:57
create table Machine ( Machine_ID int primary key, Machine_Name varchar(30) Machine_Title varchar(30) ) create table Part ( Part_ID int primary key, Part_Name varchar(30), Part_Description varchar(30) ) //How do I make this table below? create table Machine-Part ( Machine_ID int foreign key references (Machine.Machine_ID), Part_ID int foreign key references (Part.Part_ID) Factory_Note varchar(30); ) Mysql complains there is a problem with syntax? Desired Result: have Table 'Machine-Part' use 'Machine_ID' & 'Part_ID' as both primary keys (which are both foreign keys). If you declare the

How can I ask Hibernate to create an index on a foreign key (JoinColumn)?

岁酱吖の 提交于 2019-12-06 06:17:32
This is my model. class User{ @CollectionOfElements @JoinTable(name = "user_type", joinColumns = @JoinColumn(name = "user_id")) @Column(name = "type", nullable = false) private List<String> types = new ArrayList<String>(); } You can imagin there would be a table called "user_type", which has two columns, one is "user_id", the other is "type". And when I use hbm2ddl to generate the ddls, I can have this table, along with the foreign key constraint on "user_id". However, there is no index of this for this column. And I need this index. How can I let hibernate to generate this index for me? Thank

How to set up custom string foreign key in Rails 4?

独自空忆成欢 提交于 2019-12-06 06:16:07
问题 How to set up associations to set up properly has_one using string foreign_key? class Pharmaceutic < ActiveRecord::Base has_one :pharmaceutic_group, foreign_key: "code" end class PharmaceuticGroup < ActiveRecord::Base belongs_to :pharmaceutic, primary_key: 'code' end >> Pharmaceutic.last.pharmaceutic_group Pharmaceutic Load (0.3ms) SELECT `pharmaceutics`.* FROM `pharmaceutics` ORDER BY `pharmaceutics`.`id` DESC LIMIT 1 PharmaceuticGroup Load (0.3ms) SELECT `pharmaceutic_groups`.* FROM

Foreign Keys Slowing down Delete

孤街醉人 提交于 2019-12-06 05:20:59
问题 I have a table X which has an auto-incremented ID column as its primary key. I have other tables A, B, C and D that compliment the info in table X. Each of these have to contain a column that references the ID from table X. I have done that and in my code (Java) and I have a way of returning the ID of each entry to table X and using that when inserting to the other tables. All is working well. Now, I've been advised to assign those ID columns on tables A, B, C and D as FOREIGN KEYS because

SQL - NULL foreign key

匆匆过客 提交于 2019-12-06 03:48:42
Please have a look at the database design below: create table Person (id int identity, InvoiceID int not null) create table Invoice (id int identity, date datetime) Currently all persons have an invoiceID i.e. the InvoiceID is not null . I want to extend the database so that some person does not have an Invoice . The original developer hated nulls and never uses them. I want to be consistent so I am wondering if there are other patterns I can use to extend the database to meet this requirement. How can this be approached without using nulls? Please note that the two tables above are for

ORA-00904: “ID”: invalid identifier

徘徊边缘 提交于 2019-12-06 03:28:25
Trying to create a table with a foreign key. I keep getting ORA-00904 error. What am I doing wrong. Is it because table of the foreign key is not yet created ? CREATE TABLE ingredients( ingredient_id number(2,0), ingredient VARCHAR2(55) NOT NULL, quantity_required VARCHAR2(15) NOT NULL, optional_ingredient VARCHAR2(30) NOT NULL, CONSTRAINT pk_ingr_id PRIMARY KEY(ingredient_id), CONSTRAINT fk_ingredient_list FOREIGN KEY(id) REFERENCES ingredient_list(id) ); Take a look at the following line: CONSTRAINT fk_ingredient_list FOREIGN KEY(id) REFERENCES ingredient_list(id) Your table has no column

Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?

安稳与你 提交于 2019-12-06 03:17:38
问题 I've got a couple django models that look like this: from django.contrib.sites.models import Site class Photo(models.Model): title = models.CharField(max_length=100) site = models.ForeignKey(Site) file = models.ImageField(upload_to=get_site_profile_path) def __unicode__(self): return self.title class Gallery(models.Model): name = models.CharField(max_length=40) site = models.ForeignKey(Site) photos = models.ManyToManyField(Photo, limit_choices_to = {'site':name} ) def __unicode__(self):

Django model with Foreign Key and ManyToMany relations to same model

偶尔善良 提交于 2019-12-06 03:17:22
问题 I have a django model as follows: class Subscription(models.Model): Transaction = models.ManyToManyField(Transaction, blank=True, null=True) User = models.ForeignKey(User) ...etc... I am trying to add a ManyToMany field to the User model as follows: SubUsers = models.ManyToManyField(User, blank=True, null=True) but I get this error when I run syncdb: AssertionError: ManyToManyField(<django.db.models.fields.related.ForeignKey object at 0x19ddfd0>) is invalid. First parameter to ManyToManyField

[MySQL]: DELETE rows from two dependent tables

无人久伴 提交于 2019-12-06 03:06:57
问题 I am attempting to delete all rows in two dependent tables based on a third tables ID. Table structure: Transaction -Transaction_ID (primary) -Timestamp Purchase -Item_ID -Transaction_ID -Purchase_ID (primary) Item -Item_ID (primary) -Client_ID I would like to delete all rows from transaction/purchase that match the Client_ID in item. Sounds simple enough... even I can wrap my novice mind around that... DELETE dbName.t FROM dbName.Transaction t JOIN dbName.Purchase p ON p.Transaction_ID = t