I have a table with only two attributes (DeliveryPerson and DeliveryTime). Each person can deliver a “product” at a specific Delivery Time. As you can see below John for example has delivered three products at different delivery times.
According to my task, I must put this table in 3NF, but I am confused because I cannot set “deliveryPerson” as a primary key because there are repeated values in this column. Is there any way of setting up this table to satisfy 3NF? If that is not possible, is it correct to have a table like this in a DB without a Primary key?
Thank you very much!
Normalisation is not about adding Primary Keys to a table where you've already decided the columns, it's about deciding what tables and columns you need in the first place. The inability to define a Primary Key on this table is the problem you've been asked to solve; the solution will involve creating new tables.
Rather than looking at the table, look at the data you're trying to model:
There are four (and probably any number of) delivery people
Each delivery person can have one or more (maybe even zero) delivery times
A normalised database will represent each of those separately. I'll leave the details for you to work out, rather than feeding you the full answer.
There are plenty of tutorials available which will probably explain it better than me.
I am looking for a solution to detect and delete all records of a table "UniqueKeys" which are not referenced anymore from records in any other table. As my question seems not clear, I have rephrased it.
Challenge:
If there is a table called "UniqueKeys" which consists of an ID and a uniqueIdentifier column and if there are dozens of tables which references the ID field of the "UniqueKeys" table - and now there are some records in the "UniqueKeys" table whose ID are not used in any of these other tables' references, I want to be able to detect and delete them with a SQL query without hard-code the joins to all of these other tables.
The found solutions so far included explicitly writing joins with each of the "other" tables which I want to avoid here.
Like this: Other SO answer
The goal: should be a generic solution so that at any time Devs can add additional foreign tables and this solution should continually be able (without modification) to detect any references to table "X" (and avoid the deletion of such affected records).
I know that I simply could programmatically (in the programming language of my choice) iterate through all given records of table "UniqueKeys" and use exception handling to simply continue when a given record cannot be deleted because of an active constraint.
This is what I am currently doing - and it yields the desired result - but imho this is a very ugly approach.
As I am no SQL expert, show me how to better re-phrase the above if that will help a better understanding of what I am trying to achieve.
I have a very bizarre situation where I have a tons of tables in a database and I need to find which column combination make entries distinct.
There is no documentation, and for reasons, we can't use the "official" primary key which is an SID.
The "non-official" primary key can be a combination of 1 to 300 columns.
Do you guys have any idea on how I can output the right column list for each table in the database?
I feel like doing select distinct for each columns combination is a bit overkill, since some table have 300 columns!
Any tips or insight would be appreciated!
Thank you.
I have many to many relation, I want to change it to one to many relation.
Table A which has two FKs will be dropped.
one of the Fks in table A will be moved to table B.
FKs is not nullable.
My approach so far is like this:
Add nullable column(FK) to table B as.
insert data into table B from Table A.
Alter column to be NOT NULL.
Now my question:
Can I achieve that without step 1?
Is there any another "better" approach?
I see marc_s has already mentioned this in a comment, but for people viewing this question in the future I'll say it again here.
There is no way to do this in the current version of SQL Server. Even if there were a shortcut, it would likely be nothing more than "syntactic sugar," as they say, to do exactly what you're talking about.
Because I have a poor memory, I want to write a simple application to store table column information, especially the meaning of table columns. Now I have a problem with my table design. I plan to make the table have the following columns:
id, table_name, column_name, data_type, is_pk, meaning
But this design can’t express the foreign key relationship. For example: table1.column1 and table2.column3 and table8.column5 all have the same data type and the same meaning, how can I modify my table design to express this information(relationship)?
Great thanks!
PS:
In fact, recently I'm working on a legacy application. The database is poorly designed. The foreign key relationship is not expressed on the database layer but on the application layer. Now my boss not allow us to modify the database. We just need to make the application working. So I can't do some work on the database directly.
Depending on your DBMS, you could probably use comments on the table / column to record the meaning of each one of those columns. Most DBMS allow you to perform some kind of annotation.
If you must have it in your table you have a few choices.
Free text If this is just to serve as a memory aid, it doesn't really need to be machine readable. This makes it easier for you to read / use directly.
fk_id Store the ID of the field this foreign key maps into. You could then define a view that pulls in the meaning column from this foreign key.
Meaning Table Store meaning as an ID into a seperate table and use a view to make it easier to work with.
Create a document Keep it in a document instead. That way you can print it out and have it handy.
You could try designing a full de-normalized schema for this, but I'd argue thats seriously over-thinking something that's just meant as a memory aid.
I would just add a column to your design "FK_Column_ID" that will hold a reference to column ID in case of a FK constraint.
The other way will be to create a duplicate of your DB as DBDefinitions or something like that.
Almost all DBMS allow you to attach descriptions or comments to table, index, and column definitions.
Oracle:
COMMENT ON COLUMN employees.job_id IS 'abbreviated job title';
If you specify foreign key relationships as part of the schema, the database will keep track of them, and will display them for you.
It is not possible to define a compound foreign key relationship with a single additional column. I would suggest that you create a second table to define the foreign keys, perhaps with the following columns:
id, fk_name, primary_table_id, foreign_table_id
and add a fk_id column to relate the fields used in the foreign key relationship. This works for both the single column foreign key and the compound foreign key.
Alternatively, and with some attempt at diplomacy, tell your boss that if you can't fix the root cause of an issue, then the time required to complete the project will be much longer than expected. First you will take some time to implement a work around which will not perform adequately, then you will take more time to implement the fix you should have implemented in the first place (which in this case is fixing the database.)
If you're not allowed to edit the database then presumably you're creating this in another standalone DBMS. I don't think is something you can acheive simply and you may well be better of just writing it up in a text document.
I think that you need more than one table. If you create a table of tables:
id, table_name, meaning
And then a table of columns:
id, column_name, datatype, meaning
You can then create a link table:
table_id, column_id, is_pk, meaning
This will enable you to have the same column linked to more than one table - thus expressing your foreign keys. As I said above though - it may be more effort than its worth.
FWIW, I do this quite often and the best "simple application" I've found is a spreadsheet.
I use a page for table/column defs, and extra pages as I need them for things like FK relationships, lookup values etc.
One of the great things about a spreadsheet for this app, is adding columns to the sheet as you need them, & removing them when you don't.
The indexing ability of a spreadsheet is also v. useful when you have a large number of tables to work with.
I know this does not answer your question directly, but how about using a database diagram?
I also have a poor memory (age I guess) and I always have an up to date diagram on my wall.
You can show all the tables, fields and foreign keys and also add comments.
I use the PowerAMC (aka PowerDesigner from Sybase) database designer, it also generates the SQL script to create the database, perhaps not very useful for legacy databases, although it will reverse engineer the database and create the diagram automatically (it can take some time to make the diagram readable).
I don't see a reason why you should implement some app to store some info there. You can as well use smth like OneNote or any other available organizer, development wiki, etc.: there are tons of ways to store info in such a way that it comes handy when you look up for it in future.
If you can make some inner changes, you can change keys' constraints names to readable pattern, like table1_colName_table2_colName.
And at least you can make some diagram, whether hand-made or using some design application.
If all this doesn't solve your problem, some more details are needed on what exactly you need to solve :)