foreign-keys

Mysql - Add auto_increment to primary key

纵然是瞬间 提交于 2019-11-28 06:28:50
I have a strange problem with mysql. I am trying to alter a table's column which is a primary key and has an auto_increment constraint defined on it. This is also a foreign key reference for multiple other tables. I need to change the length of this column in both , parent and all children. set foreign_key_checks=0; alter table Parent modify Identifier smallint(10) unsigned; alter table Child_1 modify FK_Identifier smallint(10) unsigned; alter table Child_2 modify FK_Identifier smallint(10) unsigned; alter table Child_3 modify FK_Identifier smallint(10) unsigned; alter table Child_4 modify FK

Why do Rails migrations define foreign keys in the application but not in the database?

邮差的信 提交于 2019-11-28 04:54:13
If I define a Customer and Order model in which a Customer "has many" Orders and the Order "belongs to" the Customer , in Rails we talk about Order having a foreign key to the Customer through customer_id but we don't mean that this is enforced in the database. Because Rails does not define this as a database-level constraint, there is a risk of your data's integrity being violated, perhaps outside the application (or inside if you receive simultaneous requests?), unless you enforce the constraint in the database manually. Why does Rails not define the foreign key at the database level or is

SQLite foreign key examples

血红的双手。 提交于 2019-11-28 04:46:05
I am not an expert in sql / sqlite.. suppose we have two tables: CREATE TABLE child ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, ); CREATE TABLE MyTableB( dog TEXT, FOREIGN KEY(dogList) REFERENCES child(id) ); how will the INSERT? is correct my createTable operations? I would like to have: a child can have more than one dog a dog can have more children EDIT What if I wanted all the children and for each child a list of dogs associated with that child? Many-To-Many In order to support a child having zero or more dogs and a dog belonging to zero or more children, your database table

Mysql: adding foreign key does not give warning/error on MyISAM tables

那年仲夏 提交于 2019-11-28 04:43:36
问题 Here is a table I made: mysql> show create table notes; +-------+----------------------------------------------------+ | Table | Create Table | +-------+----------------------------------------------------+ | notes | CREATE TABLE `notes` ( `id` int(11) NOT NULL auto_increment, `note` text NOT NULL, `status` enum('active','hidden','deleted','followup','starred') default NULL, `created` datetime NOT NULL, `last_updated` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

Are foreign keys indexed automatically in SQL Server?

非 Y 不嫁゛ 提交于 2019-11-28 04:37:14
Would the following SQL statement automatically create an index on Table1.Table1Column, or must one be explicitly created? Database engine is SQL Server 2000 CREATE TABLE [Table1] ( . . . CONSTRAINT [FK_Table1_Table2] FOREIGN KEY ( [Table1Column] ) REFERENCES [Table2] ( [Table2ID] ) ) jons911 SQL Server will not automatically create an index on a foreign key. Also from MSDN: A FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table; it can also be defined to reference the columns of a UNIQUE constraint in another table. A FOREIGN KEY constraint can

Bidirectional foreign key constraint

我的梦境 提交于 2019-11-28 03:58:39
问题 I'm thinking of designing a database schema similar to the following: Person ( PersonID int primary key, PrimaryAddressID int not null, ... ) Address ( AddressID int primary key, PersonID int not null, ... ) Person.PrimaryAddressID and Address.PersonID would be foreign keys to the corresponding tables. The obvious problem is that it's impossible to insert anything into either table. Is there any way to design a working schema that enforces every Person having a primary address? 回答1: "I

How to persist an enum using NHibernate

五迷三道 提交于 2019-11-28 03:54:18
Is there a way to persist an enum to the DB using NHibernate? That is have a table of both the code and the name of each value in the enum. I want to keep the enum without an entity, but still have a foreign key (the int representation of the enum) from all other referencing entities to the enum's table. Why are you guys over complicating this? It is really simple. The mapping looks like this: <property name="OrganizationType"></property> The model property looks like this: public virtual OrganizationTypes OrganizationType { get; set; } The Enum looks like this: public enum OrganizationTypes {

Foreign keys in mongo?

断了今生、忘了曾经 提交于 2019-11-28 03:44:51
How do I design a scheme such this in MongoDB? I think there are no foreign keys! You may be interested in using a ORM like Mongoid or MongoMapper. http://mongoid.org/docs/relations/referenced/1-n.html In a NoSQL database like MongoDB there are not 'tables' but documents. Documents are grouped inside Collections. You can have any kind of document – with any kind of data – in a single collection. Basically, in a NoSQL database it is up to you to decide how to organise the data and its relations, if there are any. What Mongoid and MongoMapper do is to provide you with convenient methods to set

SqlAlchemy - Filtering by Relationship Attribute

折月煮酒 提交于 2019-11-28 03:38:14
I don't have much experience with SQLAlchemy and I have a problem, which I can't solve. I tried searching and I tried a lot of code. This is my Class (reduced to the most significant code): class Patient(Base): __tablename__ = 'patients' id = Column(Integer, primary_key=True, nullable=False) mother_id = Column(Integer, ForeignKey('patients.id'), index=True) mother = relationship('Patient', primaryjoin='Patient.id==Patient.mother_id', remote_side='Patient.id', uselist=False) phenoscore = Column(Float) and I would like to query all patients, whose mother's phenoscore is (for example) == 10 As

Creating composite foreign key constraint

被刻印的时光 ゝ 提交于 2019-11-28 03:16:55
问题 I am trying to create a composite foreign key relationship/constraint. All tables are empty. I have this table: CREATE TABLE [dbo].[ChemSampleValueTest]( [SampleNumber] [int] NOT NULL, [ParameterID] [int] NOT NULL, [Value] [numeric](18, 6) NOT NULL, [Accuracy] [varchar](50) NULL, [ResultGroupID] [int] NOT NULL, [QAState] [nvarchar](32) NOT NULL, CONSTRAINT [PK_SampleValueTest] PRIMARY KEY CLUSTERED ( [SampleNumber] ASC, [ParameterID] ASC, [ResultGroupID] ASC ) ) ON [PRIMARY] and this table: