Deciding between unique, primary, or increments in Laravel Migrations - sqlite

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.

Related

How to make values unique in cassandra

I want to make unique constraint in cassandra .
As i want to all the value in my column be unique in my column family
ex:
name-rahul
phone-123
address-abc
now i want that i this row no values equal to rahul ,123 and abc get inserted again on seraching on datastax i found that i can achieve it by doing query on partition key as IF NOT EXIST ,but not getting the solution for getting all the 3 values uniques
means if
name- jacob
phone-123
address-qwe
this should also be not inserted into my database as my phone column has the same value as i have shown with name-rahul.
The short answer is that constraints of any type are not supported in Cassandra. They are simply too expensive as they must involve multiple nodes, thus defeating the purpose of having eventual consistency in first place. If you needed to make a single column unique, then there could be a solution, but not for more unique columns. For the same reason - there is no isolation, no consistency (C and I from the ACID). If you really need to use Cassandra with this type of enforcement, then you will need to create some kind of synchronization application layer which will intercept all requests to the database and make sure that the values are unique, and all constraints are enforced. But this won't have anything to do with Cassandra.
I know this is an old question and the existing answer is correct (you can't do constraints in C*), but you can solve the problem using batched creates. Create one or more additional tables, each with the constrained column as the primary key and then batch the creates, which is an atomic operation. If any of those column values already exist the entire batch will fail. For example if the table is named Foo, also create Foo_by_Name (primary key Name), Foo_by_Phone (primary key Phone), and Foo_by_Address (primary key Address) tables. Then when you want to add a row, create a batch with all 4 tables. You can either duplicate all of the columns in each table (handy if you want to fetch by Name, Phone, or Address), or you can have a single column of just the Name, Phone, or Address.

Is it safe to use SYS_GUID() as unique ID in an Oracle table?

I have following table DDL. Can I safely use ROWKEY to identify a row uniquely? I don't want to use Sequence/On-Insert trigger thing.
CREATE
TABLE T_SEGMENT
(
SEGMENT_NAME VARCHAR2(15),
ROWKEY VARCHAR2(50) DEFAULT sys_guid()
)
If you are asking "is sys_guid() guaranteed to return a unique value", yes. Well, almost yes-- part of the GUID is the result of a random number generator so it's theoretically possible that you'd get a duplicate, it's just incredibly unlikely.
Of course, if you are using a GUID to uniquely identify a row, it would make sense to define the rowkey as the primary key and it would make sense for that key to be the first column in the table.

What are the steps to take to determine a primary key

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.

Insert or ignore every column

I have a problem with a sqlite command.
I have a table with three columns: Id, user, number.
The id is continuing. Now if I put a user and a number inside my list, my app should compare if such a user with this number already exist. The problem is, if I use a standard "insert or ignore" command, the Id column is not fixed, so I will get a new entry every time.
So is it possible just two compare two of three columns if they are equal?
Or do I have to use a temporary list, where are only two columns exist?
The INSERT OR IGNORE statement ignores the new record if it would violate a UNIQUE constraint.
Such a constraint is created implicitly for the PRIMARY KEY, but you can also create one explicitly for any other columns:
CREATE TABLE MyTable (
ID integer PRIMARY KEY,
User text,
Number number,
UNIQUE (User, Number)
);
You shouldn't use insert or ignore unless you are specifying the key, which you aren't and in my opinion never should if your key is an Identity (Auto number).
Based on User and Number making a record in your table unique, you don't need the id column and your primary key should be user,number.
If for some reason you don't want to do that, and bearing in mind in that case you are saying that User,Number is not your uniqueness constraint then something like
if not exists(Select 1 From MyTable Where user = 10 and Number = 15)
Insert MyTable(user,number) Values(10,15)
would do the job. Not a SqlLite boy, so you might have to rwiddle with the syntax and wrap escape your column names.

Choosing primary key value in LINQ to SQL

I'm creating an asp.net site that used linq to sql to create, edit and delete cars and race results. Each car has it's own number which has been set as the primary key. Each result has a result number, and there is a many-to-one relationship between the results and cars.
To create a new car object I use the Car DataContext, which automatically updates the database as requires using the DataContext.SubmitChanges() function. However it won't update the primary key, instead choosing a new one by incrementing the largest current value.
Since each car's number is important, is there any way to choose the primary key value using this method? Or should I make the car ID separate and use a separate piece of code to make sure the ID is unique?
As you aluded to in your question, keeping the Car number separate from its Id is the way to go. The reason for this is that it is possible that two cars could at some point have the same number, in addition to the fact that the database is choosing its own value for the Id anyway.
Just add another field to your Car table to record its number and you should be good to go.
See Update primary key value using entity framework for more information.

Resources