I am new to postgres and building out schemas for an institute. I was sent the most current data given by the institute. I built out a schema and created primary keys and foreign keys based on the UUIDs. When building the schema I would wrangle the big csv file in R studio and pull out each table based on the unique traits. I would then upload the unique tables/csv files to DBeaver. In DBeaver I would then join the table IDs back onto each table based on the unique trait to populate the foreign key. Finally I would then remove redundant columns that were needed to join the foreign key and primary key and add primary and foreign key constraints.
The institute has now sent me new data for the schema. What is the appropriate method to upload new data to each table following wrangling in R studio and how does the database know how to populate foreign keys if the unique columns are not present in the tables any more?
Side not, this data base is not fully normalized on purpose. We wanted to keep the schema simple for ease of backend querying from people who do not know databases.
Building relationship/adding foreign key based on uniqueness.
update survey s
set visit_id =
(select v.visit_id
from visit v
where (v."date", v.survey_time, v.site) = (s."date", s.survey_time, s.site))
Related
I am attempting to create a relationship between two tables in Access 2013. The PROJ table has many fields, with the primary key being PID, which is a randomly generated auto_increment number. I would like to use this as the foreign key for another table (JDD) to create a link between them. The primary key for JDD is another non-randomly generated id, UII, and I want the the PROJ table to autopopulate the PIDs into the JDD table. However, logically, I would assume that since it is a randomly generated number that there is no way to connect them and have them auto-populate unless I manually attached the PID to each UII. Is there anyway around this? Access would let me create the relationship but I couldn't find the plus signs to auto-populate data in the Datasheet view.
Thanks!
I'm working on a project and I'm new to both web apps and SQL, so bear with me. I'm building an API and I want to make sure that my users only have access to certain rows in a specific table that have a foreign key to their customer id in another table, but have to be validated by user id in another table. (A single Customer has multiple Users and owns multiple Assets. For right now, all of the Customer's Users can access any Asset, but no Customers share an Asset or User.) The way I can think to do this is to do
SELECT * FROM [Asset] WHERE Id=#AssetId AND CustomerId=(SELECT CustomerId FROM [User] WHERE UserId=#UserId);
This is great, but with many entries in the Asset and User tables, this query could take up a ton of time. This is bad since every request made to my API that needs the Asset data should be doing this check. I could set up an index, and in fact UserId is a Secondary Key in User because it's a unique identifier from the auth provider, but I'm not sure if I should add an index for CustomerId in Asset. The Asset table should grow relatively slowly compared to some other tables (have a messaging record table for auditing purposes), but I'm not sure if that's the right answer, or if there's some simpler answer that's more optimized. Or is this kind of query so fast at scale that I have nothing to worry about?
For your particular case, it looks like the perfect context to build a junction table between the User table and the Asset table. Both field together will become the primary key. Individually, AssetId and UserId will be foreign keys.
Let's say the junction table is called AssetUser.
Foreign keys :
CONSTRAINT [FK_AssetUser_User] FOREIGN KEY ([UserId]) REFERENCES [User]([UserId])
CONSTRAINT [FK_AssetUser_Asset] FOREIGN KEY ([AssetId]) REFERENCES [Asset]([AssetId])
Primary key :
CONSTRAINT [PK_AssetUser] PRIMARY KEY([AssetId], [UserId]));
You shouldn't worry about scale too much unless you are going to have ALOT of data and/or the performance is critical in your application. If so, you have the option to use hadoop or to migrate to a NoSQL database.
I would like to normalize an existing database with its data, because there is one (external) key over many tables to refer to an external entity (3rd-party-system). As we now need to have that one as an entity directly in our database, I am asking my, how to do this.
I update the DDL for that database and created an update-script as well (for the DDL).
But what I the best way to migrate the data?
All table, that used the old varchar-key have now a not nullable foreign key to that new entity. How do I initialize this new field. Is this a step after updating the DDL (is this possible for not-null-fields?)? Or has this to be done while migrating the DDL (and how)?
Example:
Table NewEntity
+ Key (autogenerated)
+ SomeInfo
+ OldKeyThatWasJustAVarchar
Table Existing_1
- Key (autogenerated)
- SomeInfo_1
- SomeInfo_n
- OldKeyThatWasJustAVarchar ('logical' Reference)
+ NewForeignKeyToNewTable
Table Existing_2
- Key (autogenerated)
- ExampleFieldAnalogOtherTables
- OldKeyThatWasJustAVarchar ('logical' Reference)
+ NewForeignKeyToNewTable
The contents of all OldKeyThatWasJustAVarchar are unice, so an simple SQL would offer me the needed ID of NewEntity:
SELECT Key FROM NewEntity nw, Existing_n existing
WHERE nw.OldKeyThatWasJustAVarChar = existing.OldKeyThatWasJustAVarChar;
Well first of all making it an entity doesn't require you to change keys. The new table's natural key is OldKeyThatWasJustAVarchar, so you can simply use this as primary key. Then all you'd have to do would be to create foreign keys on the already existing references.
If you want to stick with your ID concept though, then don't forget to have a unique constraint on the natural key (OldKeyThatWasJustAVarchar that is). As to the process: You can simply create the new table and the NewForeignKeyToNewTable columns in the existing tables. Make these columns nullable. Create the foreign keys. Then fill the new table, then fill the NewForeignKeyToNewTable columns. Then make your new columns not-nullable. That's it.
I've created a simple contact database with three tables, Corporate, Contact, and Country. I've created a LightSwitch application on top of it and am trying to create a countrycode lookup. i.e. on the new Corporate screen, for countrycode field have a lookup list coming from the country table.
How can I do this? If I was writing a SQL query it would purely be:
SELECT CountryCode
FROM Country
In the query designer it makes you create filters, I don't want to add any filters! Also, I've created foreign keys, unique key constraints but LightSwitch doesn't seem to recognise them and complained when I was importing my tables?
* UPDATE *
Error below
Normally, if relationships are set up correctly, this would just happen for you automatically. LightSwitch is very good at this actually.
If I understand correctly, your data source is an external SQL database, that you've added into your LightSwitch application as an attached data source. If there was a relationship between the Client table & the Country table, then LightSwitch would create the look-up combo box in the Client screen automatically. But it's not enough to just have a foreign key column, you have to create a relationship between the two tables.
You don't mention what the columns are in your tables, or more importantly the column data types are. The Country table should have an integer primary key, plus a text column for the name. In the Client table, it should also have an integer primary key, plus an integer foreign key. The relationship should be created in the Client table between its Country foreign key column and the Country table's primary key column.
You mentioned that LightSwitch "complained" when you were importing your tables. What was the error message?
In SQL Server 2012, I have a User table to store application users. I also have an Organization table which has a LastModifiedBy field in which I would like to store the UserID of the last person to modify the values within the table via an ASPX page. Should I create a foreign key relationship between the LastModifiedBy field and the UserID field within the Users table?
I would also like to add a LastModifiedBy field to the Users table itself. Should/can I create a self referencing foreign key constraint on this table? If this is possible, is it a horrible idea?
Any perspectives on pros and cons of creating these foreign key constraints would be greatly appreciated.
What you are describing is what foreign keys are made for. These foreign keys are at the very core of relational databases. Even if they might give a slight performance impact when inserting new rows, because it has to check if the referenced key exists, it should not be avoided.