I know how to add a primary key when creating the table, but not how to add it to an existing table. Do I need to create a column and then change the type to a primary key, or use something like this:
ALTER TABLE table-name MODIFY column-name PRIMARY KEY;
You can add primary key the table by;
ALTER TABLE table_name
ADD PRIMARY KEY(primary_key_column);
If you want composite primary key(primary key of more than on column) then,
ALTER TABLE table_name
ADD PRIMARY KEY(column_1, column_2);
If you have already a primary key and you want to drop existing and add another
then,
ALTER TABLE table_name
DROP PRIMARY KEY, ADD PRIMARY KEY(primary_key_column);
Related
I wonder how to add new column ( set as primary key and set default value) in existing table ? I tried
ALTER TABLE table_name ADD ( column_name VARCHAR (10));
ALTER TABLE table_name ALTER COLUMN column_name SET DEFAULT 'value1';
ALTER TABLE table_name ADD PRIMARY KEY(column_name);
>> ERROR 1138 (22004): Invalid use of NULL value
I saw couple posts but it requires to delete all existing data in the table which I don't want to. Is there other way to add new column as primary key without delete data in table?
My current table:
My new table that I want to create:
Thanks
Doing this gives ERROR since whenever you add a new column in a table which already has 1 or more rows then the new column will get NULL values in all of its tuples which is contradictory to the rule which says PRIMARY KEY CAN NOT CONTAIN NULL.
Also, if you provide DEFAULT value, then also duplicate entries aren't allowed in the primary key!
So just by adding a new column in a non-empty table by giving default and declaring it primary key at the same time will not work.
Now here comes AUTO_INCREMENT to rescue, add column by incrementing and declarig it as primary key:
ALTER TABLE table_name ADD COLUMN new_column INT AUTO_INCREMENT PRIMARY
KEY ;
This works fine now...
Thanks for asking.
Your column might have Null values in it, and also try dropping the primary key constraint first if there is any.
try this DDL:
ALTER TABLE table_name ADD ( column_name VARCHAR (10) SET DEFAULT 'value1');
ALTER TABLE table_name ADD PRIMARY KEY(column_name);
Your column might have null values
If your table doesn't have a primary key and would like to add a new column and make it as a primary key, use the below query and use auto increment so it will be unique
ALTER TABLE old_table ADD pk_column INT AUTO_INCREMENT PRIMARY KEY;
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.
I have two different table having 20k entries each and mistakenly I have made summaryId as primary key and foreign key in the same table but now I want to remove the primary key constraint which is auto increament too. When I try drop primary key syntax it returns me an error :
#1025 - Error on rename of '.\tg#sql-a38_7f' to '.\tg\rest_web_availability_summary_pm' (errno: 150)
I tried the below query.
ALTER TABLE 'table_name' DROP PRIMARY KEY
If anybody has any idea please tell me how to remove primary key.
The problem is, that your field is auto_increment. You should remove auto_increment first and then drop the primary key.. so try this:
ALTER TABLE `mytable` CHANGE COLUMN `id` `id` INT(11) NOT NULL, DROP PRIMARY KEY;
Redefining the column without auto_increment removes it
I had the same problem, turned out that as it was referenced by other fields, mysql required the column to be unique, so I first added a unique constraint, and lived happily ever after:
alter table `mytable` add unique key `key` (`fieldname`);
alter table `mytable` drop primary key; -- which is fieldname...
As mentioned, you need remove the FKs before. On MySQL, do like this:
ALTER TABLE `table_name` DROP FOREIGN KEY `id_name_fk`;
ALTER TABLE `table_name` DROP INDEX `id_name_fk`;
i am working on mysql server.where i have created a table, named question . column/attributes of this table are (course,subject,year,question)
i want to create a primary key(or composite key) consists of (course+subject+year). i.e. for a particular course+subject+year combination there can be only one question.there will be only one row with the combination of (course+subject+year),creation of another row won't be possible.
i have done it by :
primary key(course,subject,year);
but it's not working.still i can create two rows with same combination of course,subject,year.
can anyone tell me how can i create a composite key propery????
the syntax is CONSTRAINT constraint_name PRIMARY KEY(col1,col2,col3) for example ::
CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
the above example will work if you are writting it while you are creating the table for example ::
CREATE TABLE person (
P_Id int ,
............,
............,
CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
);
to add this constraint to an existing table you need to follow the following syntax
ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (P_Id,LastName)
if it is mysql you are looking at, you should do something similar to
ALTER TABLE table_name ADD PRIMARY KEY (a, b, c);
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);