Home » Community » Newbie corner » Defining composite primary key
Defining composite primary key [message #45080] |
Fri, 28 August 2015 16:47  |
Giorgio
Messages: 218 Registered: August 2015
|
Experienced Member |
|
|
Hi there,
in the database I am managing, several tables have composite primary key. I tried to describe those keys in the .sch file adding the PRIMARY KEY keyword to each field the key is composed by, but it did not work. I searched the forum and found only this http://www.ultimatepp.org/forums/index.php?t=msg&goto=53 38& thread. Maybe the issue has been already addressed in other threads, as it is a common situation in db. Could anyone point me to a solution?
Regards,
Giorgio
|
|
|
Re: Defining composite primary key [message #45081 is a reply to message #45080] |
Fri, 28 August 2015 20:29   |
|
Giorgio wrote on Fri, 28 August 2015 16:47Hi there,
in the database I am managing, several tables have composite primary key. I tried to describe those keys in the .sch file adding the PRIMARY KEY keyword to each field the key is composed by, but it did not work. I searched the forum and found only this http://www.ultimatepp.org/forums/index.php?t=msg&goto=53 38& thread. Maybe the issue has been already addressed in other threads, as it is a common situation in db. Could anyone point me to a solution?
Regards,
Giorgio
Hi Giorgio,
As you probably noticed in the linked thread, there is also DUAL_PRIMARY_KEY(k1, k2). For postgres, there is even PRIMARY_KEY_LIST(name, keys). Not sure why it is not implemented for other backends.
If you need more then two columns in your composite key (and you're not using postgres), then you can introduce your own macro. It is relatively easy just look at how your backend implements DUAL_PRIMARY_KEY. E.g. For MySql, look at MySql/MySqlSchema.h:
#define DUAL_PRIMARY_KEY(k1, k2) INLINE_ATTRIBUTE(", primary key (" #k1 ", " #k2 ")")
So you can just define similary for four columns (must be defined before including sch_schema.h, sch_header.h or sch_source.h):
#define QUADRUPLE_PRIMARY_KEY(k1, k2, k3, k4) INLINE_ATTRIBUTE(", primary key (" #k1 ", " #k2 ", " #k3 ", " #k4 ")")
I haven't tested this right now, but I have made similar augmentations for my code in the past and it worked just fine If you hit any problem, just post what code you got so far and where you got stuck.
Best regards,
Honza
|
|
|
|
Goto Forum:
Current Time: Fri May 09 20:26:39 CEST 2025
Total time taken to generate the page: 0.02914 seconds
|