Using a foreign key in Oracle SQL to display text data - sql

New to Oracle SQL and I assume that this is a very straight forward question but I can't find a direct answer. I have an employees table in my database. One of the columns is 'emp_state' which is to show the State in which the employee resides. I also have a 'Customer' table and an 'office' table, which also both have a 'State' field. To reduce the amount of redundant data in my database, I have created a 'State' table, which will have all the states with a corresponding ID. I want to reference this table in the other tables as mentioned. When setting up my table, what should I define as the foreign key in the relevant tables? Should it be 'State_id' or 'State_name'? I want the state name to appear in any queries that are run, rather than the state ID which would be meaningless to a user.
Thank you!

You should use state_id. If is much faster to join on a INT then a VARCHAR.
If you need the name if a state the you should JOIN on the table and get it
If you are planing to update the state name then you need to update all the relate tables. If you have a id then you just need to update the name column.
You would probably not have the same state name twice. But what if you do? Then you must change it or append some strange number on the state name.
So bottom line: Always use the id

Related

MS Access duplication

I got my query in MS Access duplicating its errors. The data is being entered from a Windows Forms app, and the data is in two separate tables. I'm using a basic query to show each row in the table. Name table and score table.
Now the issue is that the data is duplicating itself.
Image of the access table
This is the SQL query:
SELECT DISTINCT score.score, name.Name
FROM name, score;
Table Schema
What SQL code could I use to stop the duplication error?
Thanks in advance
Need to add a long integer number field StudID in Scores table then do data entry to add the appropriate student identifier value into each Scores record. This will establish a primary/foreign key relationship between tables that can be used in query with a LEFT or RIGHT JOIN on key fields.
This assumes a student can have multiple scores (for what?). If there can be multiple scores, then probably also need a date field ScoreDate and then possibly should be a Subject field.
If student can have only 1 score, then don't need Scores table - have a field Score in Students table.
Strongly advise not to use reserved words as names for anything. Name is a reserved word.

SQL Referencing a particular table chosen from a row value in another table

I've got a database called SimpleCredentials in which there is a table called dbo.Properties which has (PK) UserID and then some other attributes like Name, Date of Birth etc. There is another Primary Key attribute called ExtendedCredentials which is a string dbo.UserID. So, for example the user with UserID = S-1-5-21-2177 will have the string dbo.S15212177 in their ExtendedCredentials column.
I've got another database called ExtendedCredentials. For every User there is a unique table in that database. Using the previous example, there will be a table called dbo.S15212177.
So, if I have 100 users there will be 100 rows in the dbo.Properties table in the SimpleCredentials database, and there will be 100 unique tables incorporating their UserID in the ExtendedCredentials database.
I want to create an entity relationship diagram, and eventually a MS SQL schema, but how do I represent the multiplicity of dbo.UserIDs and their relationship to their dbo.UserID string attribute in dbo.Properties?
Am I getting something fundamentally wrong here?
You may ask why I don't have a single database called ExtendedProperties with a single table in which each row is the UserID PK and the various extended properties are contained in columns. The simple answer is that some properties are themselves tables. Not every user has the same attributes in those tables. And I can't know ahead of time (a priori) what the full set of user extended property attributes is. So each user gets a table of their own.
Is there a better way to do this?

which is faster select+ update or delete+insert in sql?

I just got stuck in a problem, where there are two ways of solving this.
Let me first explain the case,
I have a DB table consisting of some columns say id, name, address, priority. Here name and address is not unique but name + address + priority is unique.
Input provided to me is name and list of addresses. Now, what I have to do is to arrange name and address in the same order as given in input in my DB table.
There are two ways of solving:
selecting on the basis of name and address and make update queries for those data which are changed and execute them.
delete the data corresponding to name and address from table and insert the data with new priority.
I know that one update is faster than delete + insert but here in this case there is one select query too.
My intuition is that 1st method will be more fast but I don't have any technical details about it.
Am I missing something?

T-SQL - Using Column Name in where condition without any references when having multiple tables that are engaged using multiple joins

I am a newbie for T-Sql, I came across a SP where multiple tables are engaged using multiple joins but the where clause contain a column field without any table reference and assigned for an incoming variable,like
where 'UserId = #UserId'
instead - no table reference like
'a.UserId = #Userid'`
Can any please do refer to me any material that clears my mind regarding such issue.
If the query works it means that there is only one Column with the name UserId, if there are multiple columns with the same name you have to reference the table too.
If you don't specify the table reference you will get
Ambiguous column name 'UserId'. error
Which means there are more then 2 tables with a column name UserId.
Anyway, always try and use the reference table.

SQL - Selecting a field from another table using a primary key in a trigger

I have two tables in my database, one is Transactions and the other is TransactionHistories. The latter is essentially an auditing table, whereby a trigger executes on insert, update and delete on Transactions to capture a screenshot of the data.
I am successfully retrieving all of the data stored in the Transactions table where the columns match, but the difficulty comes where I am trying to retrieve data from another table using a foreign key. For instance:
The transaction table has a field "TransactionType_TransactionTypeId", but in the audit table we wish to store its 'name' equivalent as "TransactionTypeName". This needs to be populated from the "TransactionTypes" table, which has the fields "TransactionTypeId" and "Name".
I am struggling to write a query to retrieve this as we wish. I am trying something similar to the following but having little success:
SELECT #TransactionTypeName=Name
FROM TransactionTypes
WHERE inserted.TransactionType_TransactionTypeId=TransactionTypes.TransactionTypeId;
I'm assuming that is a syntactic nightmare. If someone could point me in the right direction I would be extremely grateful!
well to get a name you should do the following
select #TransactionTypeName = TT.Name
from inserted as i
left outer join TransactionTypes as TT on TT.TransactionTypeId = i.TransactionType_TransactionTypeId
but you have to know that inserted table can have more than one row, and you are getting value for only one row.