I'm creating a database and I'm not sure about one thing. I have an autoincrement primary key which I want to use as a foreign key. Should the foreign key also be autoincrement?
The foreign key field must not be auto-increment. Auto-increment values imply that the field is not a foreign-key.
The purpose of auto-increment attributes is to generate a unique identity for new rows in the current table. The purpose of foreign keys is to uniquely identify rows in another table. They are very different things and you should read more about both and properly understand the difference.
Auto-increment allows a unique number to be generated when a new record is inserted into a table.
Related
I have AUTO_INCREMENT PRIMARY KEY and another column that I can't set UNIQUE because unlike standard RDBMS like MySQL or PostgreSQL, MemSQL only allow only one of them, not both.
Is there workaround to prevent duplicate rows without sacrificing the auto_increment column?
I can use unique as primary key and use atomic counter in other product/service like Redis/atomic variable, but when I need to update the unique column I have to delete it first then reinsert, which is bad/unpreferred way for me..
MemSQL does support multiple unique keys together with a primary key. However, MemSQL requires that the columns in each unique or primary key must be a superset of the columns in the shard key - i.e. that all values that would be considered duplicate under each unique key have the same shard key, so that they get mapped to the same partition. This further implies that all the unique/primary keys must share at least one column in common.
For your case, it is not possible to have both a unique/primary key on the autoincrement column and the other column. But you can have a unique/primary key on the other column, without a unique key on the auto_increment column - just define it as a non-unique key. The automatically generated values will still be unique. Do note that then the table won't be able to enforce uniqueness if you manually insert values that are duplicate with other auto_increment values.
There is a simple table, with 3-rd foreign keys. How to make it impossible to re-record with the same values for these three keys? Create a complex key based on them?
And how to do it in the Workbench environment, just specify additionally each foreign key as a primary key?
If i get your question, you are looking to enforce uniqueness in the columns (user_id,position_id,organization_id).
Assuming that at least one of the columns is (not null). If you were to create a unique index on the three columns it should work.
CREATE UNIQUE INDEX index_name
ON Employers(user_id,position_id,organization_id);
As far as I'm aware, you can only assign primary keys and unique columns to foreign keys... yet I have a table that has a primary key between two columns:
alter table NAME add constraint PK primary key(VALUE1, VALUE2)
I'm trying to make Value1 a foreign key in another table, but it's not recognizing it as a primary key or unique - obviously because the primary key is shared between two values... So what do I do from here? I'm pretty new to SQL syntax...
You are correct that you can only assign primary keys and unique columns to foreign keys. I am not much aware of the business requirement here but ideally, you should be having a third table which has the VALUE1 as a primary key. If not you should create one.
The main idea is that you can't link a foreign key to a value that can hold duplicates on the referenced table. So if your main table has a compound key (more than 1 column), linking the foreign key to one (or many but not all) of it's columns would be linking the table to more than one row (since that column might have duplicates by itself).
If you really need to establish the link between the two then you have a problem, either:
Your primary key isn't really 2 or more columns. You can read about normalizing your database (in standard normal forms) to solve this.
Your relationship between the tables isn't 1 to N (it's N to M). You can't add a foreign key, you will have to create a 3rd table with both primary keys to link them.
I am designing my database with MySQL. I will use surrogate key for all my tables. But I also want to keep the nature unique key really unique.
For example in my Customer table, I have a surrogate key which is an auto-increment number. There is also a customerUserName field which should be unique.
How can I enforce this to make sure each customerUserName field are different?
You can add a unique key constraint to your customerUserName field.
It will be something like this
ALTER TABLE Customer ADD CONSTRAINT UQ_Customer_customerUserName UNIQUE (customerUserName)
I have a table that has a primary key and a unique key.
From another table, is it possible to reference this unique key just the way Primary Key's and Foreign Keys are referenced & used ?
Would like to define this in the SQL definitions so that data correlations between these two tables are automatically cross referenced.
Foreign keys specifically state they only need a unique and not-null key to work. Using a primary key just means it's a unique non-null key.
So, yes you can! You can even reference unique multi-column groups if you need to.
Search for the word "unique" within this page, you'll find further explanations.