MySQL foreign keys and composed primary key

General discussions about code engineering

Moderator: Moderators

MySQL foreign keys and composed primary key

Postby Dan Jungels » Wed Aug 01, 2007 9:11 am

Hello,

Is it possible to have composed primary keys with MagicDraw? When I add the
PK stereotype to several attributes in a table, the generated DDL script
(for MySQL) looks like

CREATE TABLE table1
(
t1 bigint PRIMARY KEY,
t2 bigint PRIMARY KEY,
t3 bigint,
t4 bigint
);

which is refused by MySQL... It seems to only accept a syntax like PRIMARY
KEY ( `t1` , `t2` ).

In addition, my foreign keys are not present in the generated script. Is
there any flag I have to put, to get them generated?

Thanks in advance,

Dan


Dan Jungels
 
Posts Rating:

Re: MySQL foreign keys and composed primary key

Postby Tomas Juknevicius » Thu Aug 02, 2007 11:41 pm

Hello Dan,

This is not a right way to create composite keys in MagicDraw.
To create composite key, rightclick the table/DDL/Set Primary Key Constraint.
This will open a simple dialog for you where you can choose columns for your key.


Sincerely,
--
Tomas Juknevicius
System Analyst
No Magic Lithuanian Development Center
Savanoriu 363-IVa., LT-49425, Kaunas
Phone: +370-37-324032; Fax: +370-37-320670
e-mail: Tomas_dot_Juknevicius_at_nomagic_dot_com
WWW: http://www.nomagic.com

Tomas Juknevicius
 
Posts Rating:

Re: MySQL foreign keys and composed primary key

Postby Dan Jungels » Mon Aug 06, 2007 3:03 am

Thanks Thomas, it works.

Do you have also a solution for my second problem (how to export the foreign
key constraints in the MySQL DDL script)?

Best regards,

Dan


"Tomas Juknevicius" <TomasJkn> wrote in message
news:2129345.1186126473156.JavaMail.tadjar@peleda...
> Hello Dan,
>
> This is not a right way to create composite keys in MagicDraw.
> To create composite key, rightclick the table/DDL/Set Primary Key
> Constraint.
> This will open a simple dialog for you where you can choose columns for
> your key.


Dan Jungels
 
Posts Rating:

Re: MySQL foreign keys and composed primary key

Postby czdcontato » Mon Nov 19, 2012 1:13 am

Hi,
I have exactly this problem: MySQL, Foreign keys are missing in generated code.
Does anyone knows what can be happening?
Thanks
CzD
czdcontato
Forum Newbie
Forum Newbie
 
Posts: 7
Posts Rating:0
Joined: Mon Nov 19, 2012 1:02 am

Re: MySQL foreign keys and composed primary key

Postby PauliusGrigaliunas » Tue Dec 11, 2012 2:31 am

Hello,

FK you can create in two ways : creating constraint with ForeignKey stereotype or FK relations.

If you create relation, dialog will pop up asking to select appropriate columns.
For constraint you will have to manually create required tag values.

Attaching screen shot for created fk.

Kind regards
Paulius Grigaliunas
You do not have the required permissions to view the files attached to this post.
PauliusGrigaliunas
Customer Support
Customer Support
 
Posts: 59
Posts Rating:0
Joined: Mon Jul 19, 2010 1:37 am

Re: MySQL foreign keys and composed primary key

Postby PauliusGrigaliunas » Tue Dec 11, 2012 2:39 am

Result script looks like this:

#@(#) script.ddl

DROP TABLE IF EXISTS bb;
DROP TABLE IF EXISTS b;
DROP TABLE IF EXISTS aa;
DROP TABLE IF EXISTS a;
CREATE TABLE a
(
p_a PRIMARY KEY
);

CREATE TABLE aa
(
p_aa PRIMARY KEY
);

CREATE TABLE b
(
p_b PRIMARY KEY,
fk_b,
CONSTRAINT fk FOREIGN KEY(fk_b) REFERENCES a (p_a)
);

CREATE TABLE bb
(
p_bb PRIMARY KEY,
fk_bb,
CONSTRAINT fk_constraint FOREIGN KEY(fk_bb) REFERENCES aa (p_aa)
);
PauliusGrigaliunas
Customer Support
Customer Support
 
Posts: 59
Posts Rating:0
Joined: Mon Jul 19, 2010 1:37 am

Re: MySQL foreign keys and composed primary key

Postby czdcontato » Tue Dec 11, 2012 6:34 am

Oh ok, I'll try it and tell you if it worked for me.
Thank you
:-)
czdcontato
Forum Newbie
Forum Newbie
 
Posts: 7
Posts Rating:0
Joined: Mon Nov 19, 2012 1:02 am

Re: MySQL foreign keys and composed primary key

Postby czdcontato » Sat Dec 15, 2012 8:32 am

Awesome, it worked! I was creating FK selecting fields (blue contour) while drawing it, and it wasn't working.
But drawing FK reference when blue contour is on whole table, it's shown a dialog asking refering and referenced fields :)
So tags is shown on diagram with respective values and code is generated as well.
Thank you!
czdcontato
Forum Newbie
Forum Newbie
 
Posts: 7
Posts Rating:0
Joined: Mon Nov 19, 2012 1:02 am


Return to Code engineering

Who is online

Users browsing this forum: No registered users and 0 guests