Currently, we have a 'messages' table with a column called "content_id" that creates a 1:1 relationship with anything a message can attach to, which are multiple other types of table data.
For instance, a message can be attached to another table row with a primary key that is an ID, and another message might be associated with another table's row with primary key of datatype varchar(36), or a uuid. There may be more tables to which a message could be assigned to one of their rows, with any other data type.
Lastly, a message can only be associated to a SINGLE ONE of any of those rows of various primary key data type.
Is there a better solution to this than a single column of varchar(36) in the messages table that can hold ints OR uuids?
If I use a join table for each relationship, I could put a UNIQUE index on the two columns that join them to make the same relationship. Is that architecture more efficient?
The DB is postgres, if that matters...
Related
Is it correct to have a table in SQL with just foreign key columns? All columns are foreign keys, except the primary key ID.
Here is the scenario in my mind. I wanted to be sure first and then implemented it.
I have three tables => Personnel, Position, Place. Personnel table has general information of employees, Position table has different job positions and tasks in a company, Place table has info about different places in the city. So, an Agent is one of the personnel with one of the job tasks who should go to do the task in one of the places. Agents could change every week, select between personnel and give them almost randomly tasks and places.
I'll give you an example where it is perfectly correct and desired:
when you have many to many relationship, say Table1 and Table2 the best practice states that you should have table, eg. Table1Table2 with just two columns: Table1Id and Table2Id, both foreign keys to respective tables and together they make primary key.
Having said that, it is perfectly correct, as long as it satisfies your design.
To say anything more, you should share your database schema.
you should add a foreign key on the child table referencing the parent table so it is correct that all fields be foreign key but you have to have primary key on that table.
Sometimes, there are certain tables in an application with only one column in each of them. Data of records within the respective columns are unique. Examples are: a table for country names, a table for product names (up to 60 characters long, say), a table for company codes (3 characters long and determined by the user), a table for address types (say, billing, delivery), etc.
For tables like these, as the records are unique and not null, the only column can be used as the primary key, technically speaking.
So my question is, is it good enough to use that column as the primary key for the table? Or, is it still desirable to add another column (country_id, product_id, company_id, addresstype_id) as the primary key for the table? Why?
Thanks in advance for any advice.
there is always a debate between using surrogate keys and composite keys as primary key. using composite primary keys always introduces some complexity to your database design so to your application.
think that you have another table which is needed to have direct relationship between your resulting table (billing table). For the composite key scenario you need to have 4 columns in your related table in order to connect with the billing table. On the other hand, if you use surrogate keys, you will have one identity column (simplicity) and you can create unique constraint on (country_id, product_id, company_id, addresstype_id)
but it is hard to say this approach is better then the other one because they both have Pros and Cons.
You can check This for more information
This may seem like a simple question, but I am stumped:
I have created a database about cars (in Oracle SQL developer). I have amongst other tables a table called: Manufacturer and a table called Parentcompany.
Since some manufacturers are owned by bigger corporations, I will also show them in my database.
The parentcompany table is the "parent table" and the Manufacturer table the "child table".
for both I have created columns, each having their own Primary Key.
For some reason, when I inserted the values for my columns, I was able to use the same value for the primary key of Manufacturer and Parentcompany
The column: ManufacturerID is primary Key of Manufacturer. The value for this is: 'MBE'
The column: ParentcompanyID is primary key of Parentcompany. The value for this is 'MBE'
Both have the same value. Do I have a problem with the thinking logic?
Or do I just not understand how primary keys work?
Does a primary key only need to be unique in a table, and not the database?
I would appreciate it if someone shed light on the situation.
A primary key is unique for each table.
Have a look at this tutorial: SQL - Primary key
A primary key is a field in a table which uniquely identifies each
row/record in a database table. Primary keys must contain unique
values. A primary key column cannot have NULL values.
A table can have only one primary key, which may consist of single or
multiple fields. When multiple fields are used as a primary key, they
are called a composite key.
If a table has a primary key defined on any field(s), then you cannot
have two records having the same value of that field(s).
Primary key is table-unique. You can use same value of PI for every separate table in DB. Actually that often happens as PI often incremental number representing ID of a row: 1,2,3,4...
For your case more common implementation would be to have hierarchical table called Company, which would have fields: company_name and parent_company_name. In case company has a parent, in field parent_company_name it would have some value from field company_name.
There are several reasons why the same value in two different PKs might work out with no problems. In your case, it seems to flow naturally from the semantics of the data.
A row in the Manufacturers table and a row in the ParentCompany table both appear to refer to the same thing, namely a company. In that case, giving a company the same id in both tables is not only possible, but actually useful. It represents a 1 to 1 correspondence between manufacturers and parent companies without adding extra columns to serve as FKs.
Thanks for the quick answers!
I think I know what to do now. I will create a general company table, in which all companies will be stored. Then I will create, as I go along specific company tables like Manufacturer and parent company that reference a certain company in the company table.
To clarify, the only column I would put into the sub-company tables is a column with a foreign key referencing a column of the company table, yes?
For the primary key, I was just confused, because I hear so much about the key needing to be unique, and can't have the same value as another. So then this condition only goes for tables, not the whole database. Thanks for the clarification!
I need to store a foreign key in a table that doesn’t directly reference a table. I explain. Here I’d like to do something similar to inheritance, but it’s actually not. I have – for a given record in my table – two important fields: the arbitrary or generic key, and a field representing the type of what such a key would refer to. The idea is storing an integer, then regarding the type of the key, joining the corresponding table.
Is it even possible? What are the alternatives? I don’t want inheritance – I’m not using an ODBMS.
A foreign key in a "child" table must reference a single "parent" table. The parent must be specified at the time when you define your key. All rows in your child table must reference the same parent table - there can be no row-by-row differentiation.
Note, however, that a column does not need to be a foreign key in order to be used in a join. A foreign key constraint will prevent insertions of incorrect keys into the child table, and deal with deletions in a parent table by cascading into the child or throwing an error. If you do not care for this functionality, you could store your "foreign key" column normally, and use it in outer joins, like this:
select *
from child c
left outer join referenced1 r1 on c.fk = r1.pk AND c.code = 'first'
left outer join referenced2 r2 on c.fk = r2.pk AND c.code = 'second'
left outer join referenced3 r3 on c.fk = r3.pk AND c.code = 'third'
The above example assumes that your "foreign key" consists of two columns - the fk that indicates what row you reference, and code that indicates what table you want.
Multi-table foreign key (or - to be more specific - "something like foreign key") in one column is possible if you resign from creating FK constraint and store related table name in another column. It'll work some way, unless you care about your data integrity. In fact, some people consider this as one of SQL antipatterns. :-)
Of course you can then create some trigger procedure and check every time if new/modified value exists in "related" table. It's not elegant though.
I think that the better way is to create foreign key in "related" tables (one table for every type):
main_table:
id
...(other columns)
table_type_first:
main_table_id
foreign_key_for_type_1
...(other columns)
table_type_second:
main_table_id
foreign_key_for_type_2
...(other columns)
It's also not brilliant, and maybe it's not exactly what you need (you don't have type column, "entity" type is dependent on existance of record in "type" tables), but it provides more data integrity.
I have two tables A,B like
A(A_pk_id,A_name), B(A_pk_id,B_pk_id,B_Name);
Here A_pk_id is primary key of table A, And table B has composite primary key comprising of two fields A_pk_id(Table's primary key),B_pk_id,
Now whenever i tried to define relationship between these two tables [on A_pk_id] MS Access 2007 set their relationship type 1 to 1, but i want it to be 1 to many, cardinality of table must be set to 1.
Can any body guide how i could accomplish my goal.
Regards
Ahsan
If you're getting a 1:1 relationship, it means you're joining two fields that both have a unique index on them (regardless of whether both are PK or not). The many side needs to have a non-unique index.