Creating a custom Serial Number in Primary Key in mvc4 - mysql

I am building a website in mvc4 and i want to ask that as i am registering a new user than the user-profile table automatically updated with user-id as auto increment in the 1,2,3,4,5,...sequence but i want this primary key to auto-increment in some other way such as psk1,psk2,psk3...................can i have this ?? if Yes how it is possible?
I have try using custom membership provider but i am not successful by using that method so can any one have better option other than this?

I can think of three ways of creating unique primary keys
Autoincrement by identity.
This is the method that you are already familiar with, leading to automatic generation of primary keys of type 1,2,3,....
Using UUID.
This is a global unique identified which you can create in .NET and pass it as value in your database record.
Custom creation of primary key
If there is a special need, you could set your data table column to be primary Key of type varchar and create either in the .NET domain, or in the SQL server using a trigger. There, you could find the last inserted ID, parse it, increment the integer part and form the new unique primary key. .
Hope I helped!

Related

Create MySQL table without Primary Key

I have a table that does not require a primary key. It consists of 4 columns which are email,date,time,message. Each time a user logs in, logs out, or does any particular action that is important, I log the email, date, time and action (message). Currently the table is setup with email as the Primary Key but I am unable to insert more than one record with the same email. I suppose I could use time as the PK but there is the possibility that two actions fall on the same time. How can I just use the table without a PK? I have tried turning it off for the email column but it does not allow me to.
Yes as you have defined email field as your primary, it can hold unique data only and no duplication allowed.
So you have two options:
1: Remove email field as a primary key
2: Add new integer field as a Primary key with auto increment (I would prefer this one)
You could use a natural primary key that would be a combination of Email + Date + Time + Action. That combination would be unique. It is impossible for the same user to do 2 different actions at the same time. That will help you to keep integrity of your information.
Hope this helps you.
To make a decision on a table' primary key one may start with considering these points (applicable to innodb):
How the data is going to be accessed after it is written (if you don't query it, why store it?). If you care about read performance you should query your data by the primary key, since for innodb primary key is the only possible clustered index.
The data is stored ordered by the primary key, so if you care about write performance, you should write data ideally ordered by your primary key, which always happens automatically if you have an auto_increment. Also table for which you don't explicitly specify a primary key are going to have a hidden auto_increment field which you won't be able to access, i.e. you get less for the same cost.

Choice of primary key for mysql database for fast updates

I need to create a table which stores 'events' from different clients, each event has a event_id. The event_id is unique for a specific client, implies that the combination of event_id(integer) and client(varchar) can be made a primary key. I intend to use this table as a data provider for my Java application which uses hibernate. The use cases will be adding of events, updating of events and processing the events to generate reports.
I want to ensure fast and accurate update, which requires fetching of the exact row and updating it in hibernate.
Please advice what should be the primary key:
Create a primary_key using event_id and client column
Create a additional id column with auto_increment and create a unique index using event_id and client
I am confused whether to create a id with auto_increment column or not.
Based on comments from JB Nizet
Prefer non-functional, single-column, purely technical, autogenerated primary keys.
Because the rest of your application will be able to reference the client event by a single numerical ID rather than by the combination of two information's, one of them being textual. Because if you add a third information to the functional key of an event later, or change the value of your textual client IDs, you won't have to alter all the tables having a foreign key to the client event table. Because accessing an event by a single-column, numeric PK is faster than accessing it by a composite, textual one. Because the Hibernate mapping and the code using it will be much easier to write, etc.

MySQL Primary Key Field Type

I am working with some transactional data and having issues with my primary key field. When importing data from my order manager system and Google analytics, the OrderID field has the following two value formats:
123456
002-0130775-1483424
How do I format this field to be a primary key?
Thank you in advance for your help! :)
(Running MySQL 5.5.27)
You should create an auto-increment primary key field that contains arbitrary integers (an 'internal' key)... you can put your actual "key" data in another field titled OrderNumber or something similar and put a unique index on it.
Yes, keeping your primary key internal helps insulate you from change. Anything that could be exposed as data should be re-considered as a key.
As an option, you can set up a syntetic primary key (for your internal references only) and an indexed CHAR(20) column (probably with a unique index attached for consistency purposes) – this appears to be a better solution as one day you might want to integrate with another system which might have collisions with the existing data.
See, primary key should be bigint in nature, However you can modify the way you want.
MYSQL 5.5.27 have some important changes from its earlier version ,please follow the link
https://dev.mysql.com/doc/refman/5.5/en/news-5-5-27.html

How to define a deferred constraint in MySQL

is it possible to create a primary key in a existing table on a column that have repeated value? I want is previous record not validate but new record will validate with this.Is it possible in mysql. I know it is possible in Oracle (here is an example) but don't have idea about mysql.
The link you posted as a comment to Nerd-Herd's answer uses deferred constraints. Those constraints are checked at the end of the transaction rather than at the time the statement is executed.
MySQL does not support deferred constraints
If you absolutely need deferred constraints and want to stick with an open source database you will need to migrate to PostgreSQL.
No it can not be. It violates what Primary Key means. But if you want to have a composite primary key, it may be possible
A primary key is always a unique identifier, if you make it non unique it stops being an identifier, why do you want to repeat it? If you have multiple entries that have a field that repeats, that field is not your primary key, however, you can combine it with another field that will give you a primary key (not very recommendable, but you can make this field plus a timestamp field your combined primary key).
In this case what I would recommend is make an autoincrement key and just use this field that repeats as a normal field, maybe ad an index to it to improve searches. You can still look for records on any field, just because it's not your primary key it doesn't mean you are not going to be able to search and get it. The idea of a primary key is that it will get you 1 and only 1 record, not 1 or more.

Problem with hibernate trigger-generated ids (MySQL)

I'm using before and after insert triggers to generate ids (primary key) of the form "ID_NAME-000001" in several tables. At the moment, the value of the hibernate generator class of these pojos is assigned. A random string is assigned to the object to be persisted and when it's inserted by hibernate, the trigger assigns a correct id value.
The problem with this approach is that I'm unable to retrieve the persisted object because the id only exists in the database, not in the object I just saved.
I guess I need to create a custom generator class that could retrieve the id value assigned by the trigger. I've seen an example of this for oracle (https://forum.hibernate.org/viewtopic.php?f=1&t=973262) but I haven't been able to create something similar for MySQL. Any ideas?
Thanks,
update:
Seems that this is a common and, yet, not solved problem. I ended up creating a new column to serve as a unique key to use a select generator class.
Hope this won't spark a holy war for whether using surrogate key or not. But it's time to open the conversation here.
Another approach would be just, use the generated key as surrogate key and assign a new field for your trigger assigned id. The surrogate key is the primary key. You have the logically named key (such as the "ID_NAME-000001" in your example). So your database rows will have 2 keys, the primary key is surrogate key (could be UUID, GUID, running number).
Usually this approach is preferable, because it can adapt to new changes better.
Say, you have these row using surrogate key instead of using the generated id as natural key.
Surrogate key:
id: "2FE6E772-CDD7-4ACD-9506-04670D57AA7F", logical_id: "ID_NAME-000001", ...
Natural key:
id: "ID_NAME-000001", ...
When later a new requirement need the logical_id to be editable, auditable (was it changed, who changed it when) or transferable, having the logical_id as primary key will put you in trouble. Usually you cannot change your primary key. It's horribly disadvantage when you already have lots of data in your database and you have to migrate the data because of the new requirement.
With surrogate key solution, it'll be easy, you just need to add
id: "2FE6E772-CDD7-4ACD-9506-04670D57AA7F", logical_id: "ID_NAME-000001", valid: "F", ...
id: "0A33BF97-666A-494C-B37D-A3CE86D0A047", logical_id: "ID_NAME-000001", valid: "T", ...
MySQL doesn't support sequence (IMO autoincrement isn't comparable to sequence). It's different from Oracle/PostgreSQL's sequence. I guess that's the cause why it's difficult to port the solution from Oracle database to MySQL. PostgeSQL does.