What are the steps to take to determine a primary key - erd

I am having some confusion on how to determine a primary key in regards to an ERD model.
Say for example,
I created the following table to keep track of employees salary.
Sal_His(Emp#, Salary, Reason, Raise-Date)
How would I determine which key would become the primary key?

A primary key can also be a combination of multilple fields.
In your case, Emp# and Raise-Date together might form the primary key.
EDIT At the logical level, those two fields form a compound primary key. That primary key indentifies uniquely each row of the table (unless an employee can have multiple raises per day) and is irreducible because none of those fields alone is sufficient to uniquely identify your records.
When you get to the physical level, you might want to introduce a surrogate primary Key (an ID) and create a unique index on the two columns (RaiseDate, Emp#).
You can find more information about the benefits and drawbacks of this approach here.

Related

Dynamodb key made up of 3 fields

Say I have an RDBMS table with a composite primary key e,g field1,field2,field3 which uniquely identify a record in the table. How can I model this on Dynamodb as it appears the primary on Dynamodb can only be made up of two fields (e.g a partition key and sort key)
You may need to somehow combine them into one value (such as concatenation with a field delimiter). For e.g. field1_field2_field3 as the partition key. If you happen to need sorting, you may also use sort key. You would also be able to search on bases for these fields for e.g. field1_ or field2 or _field3
Refrence: https://aws.amazon.com/blogs/database/choosing-the-right-dynamodb-partition-key/

Deciding between unique, primary, or increments in Laravel Migrations

I have a table of of data where the primary unique element is called a CRN, which denotes "course registration number", and it's an integer value.
In this table there are around 4000 classes, and each class has a unique CRN.
My question is, in my schema builder for migrations, should this I set the CRN as a unique, a primary, or an increments?
According to my usage, a unique makes the most sense.
The CRN is indeed unique, and never changes. I have a script that updates the other columns that corresponds to this CRN, and having CRN as unique will prevent additional rows of the same CRN created.
A bug I ran into without setting any of these constraints of unique/primary/increments is, when I re-ran my script that updates the enrollment numbers, for example, for a CRN, a new row is generated instead of it being updated. So now I have two rows of data of the same CRN.
İf your key, CRN, is surrogate key then you should make it unique ,primary and increment beacuse it is surrogate key and has no meaning with your object.If it is natural key then you dont have to make it incremented just make it unique.
If it is surrogate then create it with $table->increments('id') and this will make it primary,unique and incremented.If it is natural key
$table->string('crn')->unique();
$table->primary('crn');
And i think your 'crn' seems natural key.

Sqlite, is Primary Key important if I don't need auto-increment?

I only use primary key integer ID for it's "auto-increment function".
What if I don't need an "auto-increment"? Do I still need primary key if I don't care the uniqueness of record?
Example: Lets compare this table:
create table if not exists `table1`
(
name text primary key,
tel text,
address text
);
with this:
create table if not exists `table2`
(
name text,
tel text,
address text
);
table1 applies primary key and table2 don't. Is there any bad thing happen to table2?
I don't need the record to be unique.
SQLite is a relational database system. So it's all about relations. You build relations between tables on keys.
You can have tables without a primary key; it is not necessary for a table to have a primary key. But you will almost always want a primary key to show what makes a record unique in that table and to build relations.
In your example, what would it mean to have two identical records? They would mean the same person, no? Then how would you count how many persons named Anna are in the database? If you count five, how many of them are unique, how many are mere duplicates? Such queries can be done properly, but get overly complicated because of the lacking primary key. And how would you build relations, say the cars a person drives? You would have a car table and then how to link it to the persons table in your example?
There are cases when you want a table without a primary key. These are usually log tables and the like. They are rare. Whenever you are creating a table without a primary key, ask yourself why this is the case. Maybe you are about to build something messy ;-)
You get auto-incrementing primary keys only when a column is declared as INTEGER PRIMARY KEY; other data types result in plain primary keys.
You are not required to declare a PRIMARY KEY.
But even if you do not do this, there will be some column(s) used to identify and look up records.
The PRIMARY KEY declaration helps to document this, enforces uniqueness, and optimizes lookups through the implicit index.

Is it okay to use the same primary key in every table?

I'm designing a health care form database. We use a variety of forms, and the user id and the year are the unique identifiers. Currently I have one table per form, each with a user id and a year for the primary key: ex, table health_form_1, pk (user_id, year) various form-specific columns. table health_form_2, pk (user_id, year) various form-specific columns.
I feel weird looking at a set of tables that all have the same primary key. Is there a better way to do this?
Database tables shouldn't map to your forms. Rather, the tables should map to real-world entities that your system is modeling.
For instance, if you are working on a medical billing system, then you might have tables like:
Patient
Clinician
Invoice
etc...
Each of these tables would have its own primary key.
The problem with that approach is that if the business requirement ever changes (e.g., the user can create the same form more than once in one year), you are in the position of having to change what your primary key is, which can be especially problematic when it is also used as a foreign key elsewhere.
Instead, I would create a surrogate autoincrement primary key for each table, and create a unqiue index or constraint on the UserID and Yeart columns instead.
Additionally, many ORMs work much better with a single PK, and it can make your queries more succinct.

Query that allow duplicate values in a table but not for same foreign key reference

I am using SQL Server2005 with asp.net. I want validation at server side to restrict duplicate entries, Here i am using two tables companies and Branches. In Branches Table i had maintain a foreign key of CompanyId. In Branches the BranchName can be duplicate but not for the Particular CompanyId.
Companies Table:
Columns: CompanyId (Primary Key), CompanyName
Branches Table :
Columns: BranchId(Primary Key), BranchName, CompanyId (Foreign Key).
Company Id can be Repeat multiple times, one to many Relationship.
Which query I use to that allow duplicate but not for the same CompanyId?
You want a constraint that enforces uniqueness against both the CompanyID and BranchName columns. This can either by the primary key for the table (as Tim has recommended), or a UNIQUE constraint:
ALTER TABLE Branches ADD
CONSTRAINT UQ_BranchNamesWithinCompanies UNIQUE (BranchName,CompanyID);
You can decide which order to put the columns within the constraint, based on how frequently searches are performed in the table based on the two columns. I.e. you're actually creating an index on these columns, so you may as well use it to improve some query performance.
The above ordering was based on a guess that you might search for branch names without reference to a particular company. If you're always searching within a company, and are performing prefix searches (e.g. CompanyID=21 and BranchName like 'Lon%'), then you'd want to reverse the order of the columns.
You could create a composite primary key from BranchName+CompanyId.
http://weblogs.sqlteam.com/jeffs/archive/2007/08/23/composite_primary_keys.aspx

Resources