I need to alter my primary key (id) to a composite key in MySql.
Although I know how to achieve that, what I want to know is, will it affect the existing data that I have in my table?
The existing PK is not auto incremented.
Related
I am using mysql to create a database.
I have one base table named GP, with its Primary Key a composite Primary Key(SAT_ID, DATE).
I want to create multiple tables with the same Primary Key (SAT_ID,DATE), but would like to avoid data redundancy.
Is there a way to create a Primary Key for the children tables (for example ID INT NOT NULL AUTO_INCREMENT) that references the composite Primary Key (SAT_ID,DATE), so that I can avoid having the same composite Primary Key (SAT_ID,DATE) in every other table ?
I know the question can seem silly but there is something I don't understand about composite keys and data redundancy.
Thanks
#pepper's solution (suggested in the comments) works just fine:
You could modify your GP table to have an autoincrement ID as PK and
an unique index on (SAT_ID, DATE), then you can use ID as foreign
key in your other tables
I'm learning mySQL, still on basic stuff.
My teacher has said that when writing, the best codes have first, all the tables; then, ALTER TABLE queries inserting keys to the tables. That way, we can properly name the keys. I know for sure he does that to foreign keys. He has taught this with primary keys examples as well; however, when providing files with answers for exercises he proposed, he typed the primary keys inside the tables, and later only altered the foreign keys.
How should I do it then? Always insert primary keys inside the tables, alter the foreign keys later? Or should I alter both primary and foreign keys? I'm currently trying to do he latter, and bumping into auto_increment issues for the primary keys.
Thank you for your insight!
You can't rename a primary key, so it makes no sense to do it later in an ALTER statement.
You're running into issues with auto_increment, because an auto_increment column also has to be (part of) the primary key. So you can not specify an auto_increment column but not make it primary key at the same time.
The thing is, this question is actually obsolete, as you can name your foreign keys also when creating the table. Which is for me the way that is prefered. Everything done in one statement. It would look like this:
CREATE TABLE foo (
id int auto_increment primary key,
bar int,
constraint my_fancy_fk_name foreign key (bar) references other_table(whatever_column)
);
We have mysql database with auto increment column(id) as primary key for all tables and which is referred in another table as foreign key. We have planned to use index for some of the columns in order to increase the accessing time of the table.
Is it necessary to create index for auto increment column(id) which is referred in another table as foreign key, or by default mysql using index mechanism internally for the column that is referred as foreign key. In any case, If we use index on foreign key, whether it will faster the accessing time ?
Thanks in advance.....
The primary key is used as a unique index, therefor you do not need to add an extra one.
If you want to be sure, you can always add the keywords EXPLAIN in front of one of your query to show the execution plan. It will display "use index" in the last column.
I'm trying to write a mysql statement that upserts into a table without having to use the primary key.
I know of the on duplicate key command but I can't use it here since I'm checking for the uniqueness of two columns that aren't primary keys. I know it would be better to just make these two primary keys, but I can't since this was the schema that was given.
The schema looks like this:
tbl_order_detail
key_order_detail
key_order
key_product
some_other_keys
If the key_order,key_product pair is unique then I do a regular insert.
If they aren't unique then I update the row.
Any suggestions?
ON DUPLICATE KEY UPDATE works not only with primary keys, but with any unique constraints.
So just create a composite unique index for (key_order, key_product) columns and use it.
I am using mysql.
When I add a foreign key to a table it is done without any errors.
When the table structure is described i see MUL under the key field like I see the the PRI for the columns set as the primary key.
But when I try to violate the foreign key constraint by adding a value absent in the parent table in the child table it is correctly inserted without any errors.With my little knowledge in SQL this is against the rules of the foreign key.
Any Ideas why this is happening or how I can fix it?
Probably you use the default myisam engine. It doesnt support foreign keys...
In this case put "engine=innodb" at the end of he create tables.