0

I have a table T1 with some fields like

id,key_id ,name,date etc..
  • id is a auto incrementing field
  • key_id is a value obtained from id like prefix/id
  • Prefix is constant for all the rows and just id changes which is the id for the corresponding row..

It looks like something like this

$sql = " ALTER TABLE T1 AUTO_INCREMENT = 1234567890, 
           ADD id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT ";

UPDATE T1
   SET key_id = CONCAT('12.345','/',id)

Now due to some reasons i want to create a new table T2 which would be the parent of T1 and id would be the primary key T2 and Key_id would become primary key of T1. T2 should also include other two fields like key_id and name..

ok the main reason for parent table is : i might create another table T3 which will also have id and id should have different values for T1 and T3 so i want to maintain a master table which has id as a primary key and other child tables will fetch the ids only from master table..

How can I do this?

1 Answer 1

1

Why would you want to store a redundant data in another column "key_id"?

Since the prefix is constant, the id is enough. Perform the concatenation in your code.

Also, that way, you can have id as primary key for both tables and get the key_id by prefixing the "Prefix".

Sign up to request clarification or add additional context in comments.

7 Comments

key_id is likely only for display to users, and can be changed at will (user whim) while not affecting referential integrity. Been there, done that...
ok the main reason for parent table is : i might create another table T3 which will also have id and id should have different values for T1 and T3 so i want to maintain a master table which has id as a primary key and other child tables will fetch the ids only from master table..
@OMG: If it can be changed at will, how will you maintain it as unique? OP wants it as primary key for another table.
@Sterex: Unique constraint; key_id shouldn't be the PK if the approach is what I'm talking about.
@razz: Can you please explain the logic behind having different values for primary keys (key_id) which are based off the same value(id)? From what I understand (until now), the database design is a bit screwed up.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.