I have a Mysql table having the following structure:
As you can see there is a composite primary key constraint between the fields: word_id and preposition_id.
I want to remove the Primary Key constraint from word_id without touching the preposition_id field, and without losing data from the linked tables (Foreign Key tables). How can I do it?
Regards.
There is no syntax available to modify a constraint and drop only "a half" of the primary key.
You must drop the whole primary key, and then recreate it from scrach.
Just:
ALTER TABLE tablename DROP PRIMARY KEY;
and then:
ALTER TABLE tablename ADD PRIMARY KEY ( preposition_id );
You need first to drop all foreign keys thar reference the primary key in this table.
Data in tables will be preserved.
Related
I've created a table for accounts/users with a primary key (UsersID, AccountsID) like below. Should I add the index for the Users table?
create table AccountsUsers
(
AccountsID int unsigned not null,
UsersID int unsigned not null,
Roles bigint unsigned null,
primary key (UsersID, AccountsID),
constraint AccountsUsers_Accounts_ID_fk
foreign key (AccountsID) references Accounts (ID)
on update cascade on delete cascade,
constraint AccountsUsers_Users_ID_fk
foreign key (UsersID) references Users (ID)
on update cascade on delete cascade
)
engine=InnoDB
;
create index AccountsUsers_Accounts_ID_fk
on AccountsUsers (AccountsID)
;
MySQL will create the necessary indexes for the foreign key automatically, if necessary.
In the case of your foreign key on UsersId, it can use the left column of your primary key. It doesn't need to create a new index for that foreign key.
In the case of your foreign key on AccountsId, MySQL will create a new index automatically. It can't use the fact that AccountsId is part of your primary key, because it isn't the left-most column.
After you do the CREATE TABLE, run SHOW CREATE TABLE AccountsUsers and you should see the new index it created for AccountsId.
From the documentation
MySQL requires indexes on foreign keys and referenced keys so that
foreign key checks can be fast and not require a table scan. In the
referencing table, there must be an index where the foreign key
columns are listed as the first columns in the same order. Such an
index is created on the referencing table automatically if it does not
exist. This index might be silently dropped later, if you create
another index that can be used to enforce the foreign key constraint.
index_name, if given, is used as described previously.
In other words, if you don't already have the required indexes on the columns of your referencing table (AccountsUsers), MySQL will create them for you.
If the columns in the referenced tables (Accounts and Users) are not indexed you will get an error. Your's look like they will be Primary Keys on their respective tables, so you should be fine.
I need to add a primary key on a table "usernames"
I have 3 columns in it :
userid int(10)
username char(20)
user char(50)
and the primary key is set on 'username' field and i used it as a foreign key to link it to another table. Now i need to add primary key on 'userid' field also... so i tried out :
alter table `usernames` drop primary key, add primary key(userid,username);
and i get an error saying
ERROR 1553 (HY000): Cannot drop index 'PRIMARY":needed in a foreign key constraint
is there any possible way to do this ??
There is:
Drop the FK constraint
Drop PK Constraint
Create New PK
Add Unique Constraint on the name column
Recreate FK
Raj
I assume you use MySql (despite you tag your question as Sql Server).
You can decide to:
Disable all check and try to remove primary key but the new must have the same name
or
Drop foreign key constraints referred to your primary key and then remove your primary key and finally re-add foreign keys
You have to drop the foreign key in order to modify the primary key
As implied by the name, a table can only have at most one PRIMARY key. Whilst it can have other UNIQUE keys, which have a similar effect, they really ought not be used for foreign-key relations.
So, you have two choices:
Retain your existing schema (perhaps username is a perfectly good natural key such that you need not bother with a synthetic one, in which case you can consider dropping the userid column altogether); or
Making userid your PRIMARY key, in which case username should not be used for foreign key relations. I outline below a method for doing this "offline" (where the database is guaranteed not to be altered by any other process during the transition); should you be working "online", you will need to add further steps to ensure integrity is preserved:
Add a new userid column for the foreign key in all child tables and drop the existing foreign key constraint:
ALTER TABLE foo
ADD COLUMN userid INT(10),
DROP FOREIGN KEY fk_name;
Change the primary key in your usernames tables (should you wish for the database to enforce a uniqueness constraint over username, you can define a UNIQUE key instead):
ALTER TABLE usernames
DROP PRIMARY KEY,
ADD PRIMARY KEY (userid),
ADD UNIQUE KEY (username); -- optional
Update the child tables to contain the relevant userid from the parent:
UPDATE foo JOIN usernames USING (username)
SET foo.userid = usernames.userid;
Add the new foreign key constraints and drop the old username columns from the child tables:
ALTER TABLE foo
ADD FOREIGN KEY (userid) REFERENCES usernames (userid),
DROP COLUMN username;
I want to change primary key of table, initially it was id, now i want to change it to userid
smsusers(id,fname,lname,userid)
Here id is varchar type
adn userid is int type
for this i am trying following query
ALTER TABLE smsusers DROP PRIMARY KEY
which is showing this error
#1025 - Error on rename of '.\xrcwrn_sms\#sql-ae0_6f' to
'.\xrcwrn_sms\smsusers' (errno: 150)
id of smsusers is associated with many tables as foreign key.
How to change the primary key.
Here is an example:
ALTER TABLE `database`.`table`
DROP PRIMARY KEY,
ADD PRIMARY KEY (`userid`);
The message is telling you that you can't drop the primary key yet because it is referenced by one or more foreign keys. You need to identify and drop the foreign keys first, then drop the primary key.
ERROR NO:150 means Foreign key definition problem. I think that some other table has a foreign key constraint depending on this PK, so you need to drop that first and rebuild it later.
I tried this link. This is working properly.
My steps are
CREATE INDEX id_pk_unique ON smsusers (id)
ALTER TABLE parent DROP PRIMARY KEY;
ALTER TABLE parent ADD PRIMARY KEY (userid);
I have to make one of the foreign keys unique. The problem is, I am getting the following message from the phpMyAdmin:
The following indexes appear to be equal and one of them should be removed: consignmentnumber_id_UNIQUE, fk_consignments_consignmentnumbers2
So my question is this: should I be bothered? Is it really important not to have such indexes?
Every column with an key (primary, foreign) needs an index. Same with column being unique. You probably created two indexes (one when creating FK and one on Unique constraint). If this is the case just drop one of those indexes.
It is overhead for the DB to maintain two equivalent indexes.
mysql > create unique index index_bar_id on foos(bar_id);
mysql > alter table foos add constraint index_bar_id foreign key (bar_id) references bars (id);
Read more at http://sixarm.com/about/mysql-create-indexes-foreign-keys-constraints.html
For the future, if you want to make your foreign key unique, you can simply modify your foreign key column like this:
ALTER TABLE your_table
MODIFY COLUMN your_fk_column [INT, VARCHAR etc.][NOT NULL] UNIQUE;
Just so you know, it seems like you can also have UNIQUE foreign keys:
CREATE TABLE user(
uid INT NOT NULL AUTO_INCREMENT,
username VARCHAR(16) NOT NULL UNIQUE,
email_id INT NOT NULL UNIQUE,
FOREIGN KEY (email_id) REFERENCES email(uid)
ON DELETE CASCADE
ON UPDATE CASCADE,
PRIMARY KEY (uid));
I have encountered a problem in that I already have a composite primary key in a MYSQL table. But now I have added another column to that table and due to some requirement changes, I have to modify that composite primary key in such a way that I need to add that previously mentioned column to that composite primary key list. Can anyone tell me how to alter that table without dropping existing composite primary key. I am doing this in a Rails project
You can't alter the primary key. You have to drop and re-add it:
ALTER TABLE MyTable
DROP PRIMARY KEY,
ADD PRIMARY KEY (old_col1, old_col2, new_col);
but if a key no exist?
example:
ALTER TABLE xxxx ADD id INT NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY(id,id2,id3);