使用 CONCAT 的 SQL 语法
我有一个表 T1,其中包含一些字段,例如
id,key_id ,name,date etc..
id
是一个自动递增字段key_id
是从 id 获取的值,如 prefix/idPrefix
是常量对于所有行,只是 id 发生变化,这是相应行的 id..
它看起来像这样
$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)
现在由于某些原因,我想创建一个新表 T2,它将是 T1 的父表,而 id 将是主表密钥 T2 和 Key_id 将变为T1 的主键。 T2 还应该包括其他两个字段,如 key_id 和 name..
好吧,父表的主要原因是:我可能会创建另一个表 T3,它也将有 id,并且 id 应该对 T1 和 T3 有不同的值,所以我想维护一个主表以 id 作为主键的表,其他子表将仅从主表中获取 id。
我该怎么做?
I have a table T1 with some fields like
id,key_id ,name,date etc..
id
is a auto incrementing fieldkey_id
is a value obtained from id like prefix/idPrefix
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么要在另一列“key_id”中存储冗余数据?
由于前缀是不变的,所以 id 就足够了。在代码中执行串联。
另外,这样,您可以将 id 作为两个表的主键,并通过添加前缀“Prefix”来获取 key_id。
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".