I have a problem with a board from a project:
This is the character table, the problem is that you should not be able to create 2 equal characters (same idStreamer and idViewer) but as they are not primary key this can happen.
Do you know how this can be solved?
You can just create a UNIQUE constraint on this tuple of columns to prevent the same pair of values to occur on several records:
ALTER TABLE mytable ADD CONSTRAINT myconstraint UNIQUE (idStreamer, idViewer);
Related
I have one column name Token and I'm generating random numbers and saving them in token but sometimes it saves duplicate tokens so I want to make it unique.
I want to know will it affect existing records.
If you try to add a unique constraint (or primary key constraint) to a column that contains non-unique values, the alter statement will just fail. You need to first update the column so all values are unique (or remove duplicates), and then alter the table.
ALTER table Student add primary key (studentID)
Use the Alter command to edit table's DDL and then add a primary key to it by specifying the column.
If the primary key already exist, then first you will have to drop it before defining another PK by -
ALTER table STUDENT drop CONSTRAINT <constraint_name>
Try doing this
ALTER table_namePersons ADD UNIQUE (Token);
After doing this if you'll try to insert a duplicate key you will have an error and catching it you can generate another token
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'd like to know how to make paired columns unique key?
I'm working on MySQL workbench, and right now I've set two columns (follow,following) to be UQ, I'm assuming this is Unique Key?
So when I tried to insert rows, I tried to insert
follow following
3 5
3 6
But follow is unique key. But i'd like to only do unique key for a pair only, not individual numbers.
Thanks
If i'm not mistaken you have only set a unique key for each column. Maybe you want compound column to be UNIQUE, try
ALTER TABLE tableName ADD CONSTRAINT tb_UQ UNIQUE (follow, following)
if you run the ALTER statement, the example data above is valid but if you try to insert another pair of 3, 5, it is guaranteed to fail.
If you created the table with follow as the PRIMARY key, then the RDBMS will enforce uniqueness based on that column alone.
As an alternate to JW's suggestion, you can change the PRIMARY KEY:
ALTER TABLE tableName DROP PRIMARY KEY;
ALTER TABLE tableName ADD CONSTRAINT tb_UQ PRIMARY KEY (follow, following);
Just wondering if this can be enforced by the database or not...
I have a table that has a foreign key, and another column that needs to be unique across rows where the foreign key is the same. Duplicate entries are allowed as long as the foreign keys are different.
Is there a way to do this? I can't seem to figure out a way to set a unique constraint that is based on some condition rather than applied to the entire table.
You can create UNIQUE constraint on both columns too.
ALTER TABLE myTableName
ADD CONSTRAINT tb_UQ UNIQUE (FKColumn, OtherColumn)
UPDATE 1
SQLFiddle Demo
You can add a unique constraint on two columns in MySQL:
alter table add unique index table(fk, othercolumn)
I have a table set up on a mysql database called "access" with three columns called:
rights_id, (PRIMARY KEY)
username,
name,
In the rights_id column the user can only input 3 different values ("1","2", or "3") 1 means resource, 2 means manager, and 3 means administrator. my problem occurs when there are more than one row with the same rights_id (ie: more than one administrator).It displays an error that tells me i can't have a duplicate entry for the PRIMARY KEY... i was wondering if there was a way to supress this error and allow me to do this? im using vb.net to interact with my MYSQL database running on a Windows 7 OS. Thanks!
rights_id is primary key. You can have only distinct values of primary keys in a table. So consider another primary key or do not use rights_id column this way. You should learn more about relational databases if you would like to use them.
In my opinion the best solution there is to add anothe column id which could be a primary key (you could also set multi-column primary key but this wouldn't fit your data in my opinion).
I'm not sure what "name" means in that table. If it's safe for me to ignore it . . .
If each username can have only one "rights_id", then the primary key should be username. If each username can have more than one "rights_id"--if user Catcall can have rights_id 1 and 2 at the same time--then your primary key should be the pair of columns (rights_id, username).
Since MySQL doesn't enforce CHECK constraints, you should have a separate table of rights id numbers, containing three rows.
create table rights_ids (
rights_id integer primary key
);
insert into rights_ids values (1);
insert into rights_ids values (2);
insert into rights_ids values (3);
Then you can set a foreign key constraint that will prevent any numbers besides those three from appearing in the table named "access". Something like
alter table access
add constraint foreign key (rights_id) references rights_ids (rights_id);
Create a compound PRIMARY KEY of rights_id and username (if usernames are unique that is).
No, you can't suppress that error. The issue is that rights_id is NOT your primary key.
The primary key must be able to uniquely identify a row in your table. If you can have more than 1 rights_id entry, then that is NOT able to fulfill the role of a primary key.
Read this wiki article about unique keys (a primary key is a specific type of unique key).
As Shef pointed out, you'll likely want to use a compound primary key of rights_id and username if that combination actually uniquely identifies a single row in the table.