开发者

SQL Syntax using CONCAT

开发者 https://www.devze.com 2023-03-18 22:14 出处:网络
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

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 prima开发者_开发问答ry 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?


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".

0

精彩评论

暂无评论...
验证码 换一张
取 消