foreign-keys

Find the referenced table name using table, field and schema name

北城以北 提交于 2019-11-28 14:31:18
I have a requirement where I need to find the referenced table name (Primary key table name) by a particular field in a table (Foreign key table) using this field name, table name (where this field resides) and the schema name (where the table and thereby the field resides) For example: Schema1.TableA Id (Integer, PK) Name varchar Schema2.TableB Id (integer, PK) A_Id (integer, FK referencing TableA.Id) Name varchar I need to pass A_Id , TableB and Schema2 to a function and get Schema1.TableA as result. I am using Postgres 8.3. Erwin Brandstetter If you don't need this to be portable to another

Can I use a counter in a database Many-to-Many field to reduce lookups?

跟風遠走 提交于 2019-11-28 14:28:15
I am trying to figure out the fastest way to access data stored in a junction object. The example below is analagous to my problem, but with a different context, because the actual dataset I am dealing with is somewhat unintuitive in its relationships. We have 3 classes: User , Product , and Rating . User has a many-to-many relationship to Product with Rating as the junction/'through' class. The Rating object stores the answers to several questions which are integer ratings on a scale of 1-5 (Example questions: How is the quality of the Product , how is the value of the Product , how user

MySQL Error Code 1452 Foreign Key Constraint

不羁的心 提交于 2019-11-28 13:51:29
I'm receiving an error when I attempt to create two tables. There was a multivalued dependency, so I separated the tables and came up with this: CREATE TABLE NAME ( NameID Integer NOT NULL AUTO_INCREMENT, Name varChar(255) NOT NULL, CONSTRAINT NAME_PK PRIMARY KEY(NameID) ); CREATE TABLE PHONE ( NameID Integer NOT NULL, PhoneNumber varChar(15) NOT NULL, NumType varChar(5) NOT NULL, CONSTRAINT PHONE_FK FOREIGN KEY(NameID) REFERENCES NAME(NameID), CONSTRAINT PHONE_PK PRIMARY KEY(NameID) ); But when attempting to add values with this code: INSERT INTO NAME (NameID, Name) VALUES (default, 'John Doe

What is the meaning of self referencing foreign key?

*爱你&永不变心* 提交于 2019-11-28 13:38:25
I went over a legacy database and found a couple of foreign keys that reference a column to itself. The referenced column is the primary key column. ALTER TABLE [SchemaName].[TableName] WITH CHECK ADD CONSTRAINT [FK_TableName_TableName] FOREIGN KEY([Id]) REFERENCES [SchemaName].[TableName] ([Id]) What is the meaning of it? ALTER TABLE [SchemaName].[TableName] WITH CHECK ADD CONSTRAINT [FK_TableName_TableName] FOREIGN KEY([Id]) REFERENCES [SchemaName].[TableName] ([Id]) This foreign key is completely redundant and pointless just delete it. It can never be violated as a row matches itself

Performance of string comparison vs int join in SQL

旧街凉风 提交于 2019-11-28 13:28:32
It's accepted that searching a table on an int column is faster than on a string column (say varchar). However, if I have a Shirt table with a Color column, would it be more performant to create a Color table with the primary key on that table being the foreign key on the Shirt table? Would the join negate the performance advantage of having the value in the Color column on Shirt being an int instead of a string value such as "Green" when searching for green Shirts? Compared to the other operations being performed, it is unlikely that there is much performance difference between the two

Introducing FOREIGN KEY constraint 'c_name' on table 't_name' may cause cycles or multiple cascade paths

╄→尐↘猪︶ㄣ 提交于 2019-11-28 13:15:27
问题 I have a database table called Lesson : columns: [LessonID, LessonNumber, Description] ...plus some other columns I have another table called Lesson_ScoreBasedSelection : columns: [LessonID,NextLessonID_1,NextLessonID_2,NextLessonID_3] When a lesson is completed, its LessonID is looked up in the Lesson_ScoreBasedSelection table to get the three possible next lessons, each of which are associated with a particular range of scores. If the score was 0-33, the LessonID stored in NextLessonID_1

How do I find relations between tables that are long-distance related? MySQL

江枫思渺然 提交于 2019-11-28 13:03:37
问题 I have a problem with finding relations between tables ps_product and ps_carrier from a prestashop database. The schema is available at http://doc.prestashop.com/display/PS16/Fundamentals+of+PrestaShop+Development. I need to make an update by joining these two tables in my shop but I'm struggling with finding good keys. How do I compose my query? 回答1: Tables represent business relationships/associations. The "relation[ship]s" you mention are FKs (foreign keys), and which are not needed for

FK on a single column referencing a column from composite PK

房东的猫 提交于 2019-11-28 12:25:48
问题 Not able to create /find the logic to apply FK on a column in child table referencing a column from composite PK of parent table. create table product(prod_id number, prod_name varchar2(20), price number, constraint PK12 primary key(prod_id,prod_name)); Table created. create table purchase(prod_id number, purchase_price number, constraint FK12 foreign key(prod_id) references product(prod_id)); create table purchase(prod_id number, purchase_price number, constraint FK12 foreign key(prod_id)

How to correctly add Foreign Key constraints to SQLite DB using SQLAlchemy [duplicate]

点点圈 提交于 2019-11-28 12:19:38
This question already has an answer here: Sqlite / SQLAlchemy: how to enforce Foreign Keys? 7 answers I'm very new to SQLAlchemy and I'm trying to figure it out. Please have in mind the following test setup: class Nine(Base): __tablename__ = 'nine' __table_args__ = (sqlalchemy.sql.schema.UniqueConstraint('nine_b', name='uq_nine_b'), ) nine_a = sqlalchemy.Column(sqlalchemy.dialects.sqlite.INTEGER(), primary_key=True, autoincrement=False, nullable=False) nine_b = sqlalchemy.Column(sqlalchemy.String(20), nullable=False) class Seven(Base): __tablename__ = 'seven' __table_args__ = (sqlalchemy.sql

SQL join following foreign key: statically check that LHS is key-preserved

匆匆过客 提交于 2019-11-28 11:50:46
Often you join two tables following their foreign key, so that the row in the RHS table will always be found. Adding the join does not affect the number of rows affected by the query. For example create table a (x int not null primary key) create table b (x int not null primary key, y int not null) alter table a add foreign key (x) references b (x) Now, assuming you set up some data in these two tables, you can get a certain number of rows from a: select x from a Adding a join to b following the foreign key does not change this: select a.x from a join b on a.x = b.x However, that is not true