By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,263 Members | 2,640 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,263 IT Pros & Developers. It's quick & easy.

how do you create foreign key constraints in mySQL? Or Close to it?

P: n/a
create table if not exists nnet_produkt_varegruppe (
nnet_produkt_varegruppe_id int not null auto_increment,
primary key(nnet_produkt_varegruppe_id),
nnet_produkt_varegruppe_navn varchar(255) not null
);
create table if not exists nnet_produkt_farge (
nnet_produkt_farge_id int not null auto_increment,
primary key(nnet_produkt_farge_id),
nnet_produkt_farge_code varchar(5) not null,
nnet_produkt_farge_navn varchar(255) not null
);

alter table nnet_produkt_farge add index
ix_nnet_produkt_farge_code(nnet_produkt_farge.nnet _produkt_farge_code);

create table if not exists nnet_produkt_storrelse (
nnet_produkt_storrelse_id int not null auto_increment,
primary key(nnet_produkt_storrelse_id),
nnet_produkt_storrelse_navn varchar(255) not null
);

create table if not exists nnet_produkt (
nnet_produkt_id int not null auto_increment,
primary key(nnet_produkt_id),
nnet_produkt_varenr varchar(50) not null,
nnet_produkt_navn varchar(255) not null,
nnet_produkt_farge_code varchar(5),
nnet_produkt_storrelse_id int default 0,
nnet_produkt_kvalitet_id int default 0,
nnet_produkt_krage varchar(255),
nnet_produkt_innpris decimal(6,2) not null,
nnet_produkt_utpris decimal(6,2) not null,
nnet_produkt_netto decimal(6,2) not null,
nnet_produkt_forhandler_rabatt decimal(6,2) not null,
nnet_produkt_bonus decimal(6,2) default 0.00,
nnet_produkt_toppbonus decimal(6,2) default 0.00,
nnet_produkt_bto decimal(6,2) default 0.00,
nnet_produkt_quantity int default 0
);

alter table nnet_produkt add index
ix_nnet_produkt_varenr(nnet_produkt_varenr);
alter table nnet_produkt add index ix_nnet_produkt_navn(nnet_produkt_navn);
alter table nnet_produkt add index
ix_nnet_produkt_fargecode(nnet_produkt.nnet_produk t_farge_code);
alter table nnet_produkt add index
ix_nnet_produkt_storrelse_id(nnet_produkt_storrels e_id);
alter table nnet_produkt add index
ix_nnet_produkt_innpris(nnet_produkt_innpris);
alter table nnet_produkt add index
ix_nnet_produkt_utpris(nnet_produkt_utpris);
alter table nnet_produkt add index
ix_nnet_produkt_netto(nnet_produkt_netto);
alter table nnet_produkt add index
ix_nnet_produkt_forhandler_rabatt(nnet_produkt_for handler_rabatt);
alter table nnet_produkt add index
ix_nnet_produkt_bonus(nnet_produkt_bonus);
alter table nnet_produkt add index
ix_nnet_produkt_toppbonus(nnet_produkt_toppbonus);
alter table nnet_produkt add index ix_nnet_produkt_bto(nnet_produkt_bto);
alter table nnet_produkt add index
ix_nnet_produkt_quantity(nnet_produkt_quantity);
I cannot create the indices I need considering the schema structure I am
listing. How on earth do I create something like a foreign key constraints
if it doesn't let you?

Phil
Jul 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hm...

First thought...If i understood right...

Try using INNODB type of table in MySQL.....

hope this helps.....
"Phil Powell" <so*****@erols.com> wrote in message
news:pdy7b.146966$xf.64452@lakeread04...
create table if not exists nnet_produkt_varegruppe (
nnet_produkt_varegruppe_id int not null auto_increment,
primary key(nnet_produkt_varegruppe_id),
nnet_produkt_varegruppe_navn varchar(255) not null
);
create table if not exists nnet_produkt_farge (
nnet_produkt_farge_id int not null auto_increment,
primary key(nnet_produkt_farge_id),
nnet_produkt_farge_code varchar(5) not null,
nnet_produkt_farge_navn varchar(255) not null
);

alter table nnet_produkt_farge add index
ix_nnet_produkt_farge_code(nnet_produkt_farge.nnet _produkt_farge_code);

create table if not exists nnet_produkt_storrelse (
nnet_produkt_storrelse_id int not null auto_increment,
primary key(nnet_produkt_storrelse_id),
nnet_produkt_storrelse_navn varchar(255) not null
);

create table if not exists nnet_produkt (
nnet_produkt_id int not null auto_increment,
primary key(nnet_produkt_id),
nnet_produkt_varenr varchar(50) not null,
nnet_produkt_navn varchar(255) not null,
nnet_produkt_farge_code varchar(5),
nnet_produkt_storrelse_id int default 0,
nnet_produkt_kvalitet_id int default 0,
nnet_produkt_krage varchar(255),
nnet_produkt_innpris decimal(6,2) not null,
nnet_produkt_utpris decimal(6,2) not null,
nnet_produkt_netto decimal(6,2) not null,
nnet_produkt_forhandler_rabatt decimal(6,2) not null,
nnet_produkt_bonus decimal(6,2) default 0.00,
nnet_produkt_toppbonus decimal(6,2) default 0.00,
nnet_produkt_bto decimal(6,2) default 0.00,
nnet_produkt_quantity int default 0
);

alter table nnet_produkt add index
ix_nnet_produkt_varenr(nnet_produkt_varenr);
alter table nnet_produkt add index ix_nnet_produkt_navn(nnet_produkt_navn); alter table nnet_produkt add index
ix_nnet_produkt_fargecode(nnet_produkt.nnet_produk t_farge_code);
alter table nnet_produkt add index
ix_nnet_produkt_storrelse_id(nnet_produkt_storrels e_id);
alter table nnet_produkt add index
ix_nnet_produkt_innpris(nnet_produkt_innpris);
alter table nnet_produkt add index
ix_nnet_produkt_utpris(nnet_produkt_utpris);
alter table nnet_produkt add index
ix_nnet_produkt_netto(nnet_produkt_netto);
alter table nnet_produkt add index
ix_nnet_produkt_forhandler_rabatt(nnet_produkt_for handler_rabatt);
alter table nnet_produkt add index
ix_nnet_produkt_bonus(nnet_produkt_bonus);
alter table nnet_produkt add index
ix_nnet_produkt_toppbonus(nnet_produkt_toppbonus);
alter table nnet_produkt add index ix_nnet_produkt_bto(nnet_produkt_bto);
alter table nnet_produkt add index
ix_nnet_produkt_quantity(nnet_produkt_quantity);
I cannot create the indices I need considering the schema structure I am
listing. How on earth do I create something like a foreign key constraints if it doesn't let you?

Phil


Jul 16 '05 #2

P: n/a
wish I knew the answer to this one...
I believe foreign key constriants work with innodb tables but myisam tables
are the default.
I'm working on a project in which I really need to enforce referential
integrity but I'm not sure how to assault the learning curve on the innodb
setup, if innodb support is even possible at all through my web/database
hosting service.
r


"Phil Powell" <so*****@erols.com> wrote in message
news:pdy7b.146966$xf.64452@lakeread04...
create table if not exists nnet_produkt_varegruppe (
nnet_produkt_varegruppe_id int not null auto_increment,
primary key(nnet_produkt_varegruppe_id),
nnet_produkt_varegruppe_navn varchar(255) not null
);
create table if not exists nnet_produkt_farge (
nnet_produkt_farge_id int not null auto_increment,
primary key(nnet_produkt_farge_id),
nnet_produkt_farge_code varchar(5) not null,
nnet_produkt_farge_navn varchar(255) not null
);

alter table nnet_produkt_farge add index
ix_nnet_produkt_farge_code(nnet_produkt_farge.nnet _produkt_farge_code);

create table if not exists nnet_produkt_storrelse (
nnet_produkt_storrelse_id int not null auto_increment,
primary key(nnet_produkt_storrelse_id),
nnet_produkt_storrelse_navn varchar(255) not null
);

create table if not exists nnet_produkt (
nnet_produkt_id int not null auto_increment,
primary key(nnet_produkt_id),
nnet_produkt_varenr varchar(50) not null,
nnet_produkt_navn varchar(255) not null,
nnet_produkt_farge_code varchar(5),
nnet_produkt_storrelse_id int default 0,
nnet_produkt_kvalitet_id int default 0,
nnet_produkt_krage varchar(255),
nnet_produkt_innpris decimal(6,2) not null,
nnet_produkt_utpris decimal(6,2) not null,
nnet_produkt_netto decimal(6,2) not null,
nnet_produkt_forhandler_rabatt decimal(6,2) not null,
nnet_produkt_bonus decimal(6,2) default 0.00,
nnet_produkt_toppbonus decimal(6,2) default 0.00,
nnet_produkt_bto decimal(6,2) default 0.00,
nnet_produkt_quantity int default 0
);

alter table nnet_produkt add index
ix_nnet_produkt_varenr(nnet_produkt_varenr);
alter table nnet_produkt add index ix_nnet_produkt_navn(nnet_produkt_navn); alter table nnet_produkt add index
ix_nnet_produkt_fargecode(nnet_produkt.nnet_produk t_farge_code);
alter table nnet_produkt add index
ix_nnet_produkt_storrelse_id(nnet_produkt_storrels e_id);
alter table nnet_produkt add index
ix_nnet_produkt_innpris(nnet_produkt_innpris);
alter table nnet_produkt add index
ix_nnet_produkt_utpris(nnet_produkt_utpris);
alter table nnet_produkt add index
ix_nnet_produkt_netto(nnet_produkt_netto);
alter table nnet_produkt add index
ix_nnet_produkt_forhandler_rabatt(nnet_produkt_for handler_rabatt);
alter table nnet_produkt add index
ix_nnet_produkt_bonus(nnet_produkt_bonus);
alter table nnet_produkt add index
ix_nnet_produkt_toppbonus(nnet_produkt_toppbonus);
alter table nnet_produkt add index ix_nnet_produkt_bto(nnet_produkt_bto);
alter table nnet_produkt add index
ix_nnet_produkt_quantity(nnet_produkt_quantity);
I cannot create the indices I need considering the schema structure I am
listing. How on earth do I create something like a foreign key constraints if it doesn't let you?

Phil

Jul 16 '05 #3

P: n/a
r
The recent 4.xx release of mysql supports fkey and ref integrity from within
innodb tables which are natively supported (no special installatin stuff
necessary). Here's an example of a schema I'm working on for swim meet
volunteers. Note that once workers exist for a particular timeslot, the
slot cannot be deleted (leaving orphans) nor can workers be assigned to
nonexistant slots.
referential integrity.

CREATE TABLE IF NOT EXISTS Slots_HeatSheetWorkers(
HS_ID SMALLINT UNSIGNED NOT NULL,
HS_Date DATE NOT NULL,
HS_TimeStart VARCHAR(8) NOT NULL,
HS_TimeEnd VARCHAR(8) NOT NULL,
HS_Workers_Needed TINYINT UNSIGNED,
PRIMARY KEY(HS_ID)
) TYPE=INNODB;

CREATE TABLE IF NOT EXISTS Workers_HeatSheet(
HS_ID SMALLINT UNSIGNED,
INDEX WHS_H (HS_ID),
FOREIGN KEY (HS_ID) REFERENCES Slots_HeatSheetWorkers(HS_ID)
ON DELETE RESTRICT,
WHS_ID SMALLINT UNSIGNED AUTO_INCREMENT,
NameFirst VARCHAR(255) NOT NULL,
NameLast VARCHAR(255) NOT NULL,
EMail VARCHAR(255) NOT NULL,
PRIMARY KEY(WHS_ID)
) TYPE=INNODB;






"Ygor" <so*****@anywhere.com> wrote in message
news:Tu********************@comcast.com...
wish I knew the answer to this one...
I believe foreign key constriants work with innodb tables but myisam tables are the default.
I'm working on a project in which I really need to enforce referential
integrity but I'm not sure how to assault the learning curve on the innodb
setup, if innodb support is even possible at all through my web/database
hosting service.
r


"Phil Powell" <so*****@erols.com> wrote in message
news:pdy7b.146966$xf.64452@lakeread04...
create table if not exists nnet_produkt_varegruppe (
nnet_produkt_varegruppe_id int not null auto_increment,
primary key(nnet_produkt_varegruppe_id),
nnet_produkt_varegruppe_navn varchar(255) not null
);
create table if not exists nnet_produkt_farge (
nnet_produkt_farge_id int not null auto_increment,
primary key(nnet_produkt_farge_id),
nnet_produkt_farge_code varchar(5) not null,
nnet_produkt_farge_navn varchar(255) not null
);

alter table nnet_produkt_farge add index
ix_nnet_produkt_farge_code(nnet_produkt_farge.nnet _produkt_farge_code);

create table if not exists nnet_produkt_storrelse (
nnet_produkt_storrelse_id int not null auto_increment,
primary key(nnet_produkt_storrelse_id),
nnet_produkt_storrelse_navn varchar(255) not null
);

create table if not exists nnet_produkt (
nnet_produkt_id int not null auto_increment,
primary key(nnet_produkt_id),
nnet_produkt_varenr varchar(50) not null,
nnet_produkt_navn varchar(255) not null,
nnet_produkt_farge_code varchar(5),
nnet_produkt_storrelse_id int default 0,
nnet_produkt_kvalitet_id int default 0,
nnet_produkt_krage varchar(255),
nnet_produkt_innpris decimal(6,2) not null,
nnet_produkt_utpris decimal(6,2) not null,
nnet_produkt_netto decimal(6,2) not null,
nnet_produkt_forhandler_rabatt decimal(6,2) not null,
nnet_produkt_bonus decimal(6,2) default 0.00,
nnet_produkt_toppbonus decimal(6,2) default 0.00,
nnet_produkt_bto decimal(6,2) default 0.00,
nnet_produkt_quantity int default 0
);

alter table nnet_produkt add index
ix_nnet_produkt_varenr(nnet_produkt_varenr);
alter table nnet_produkt add index

ix_nnet_produkt_navn(nnet_produkt_navn);
alter table nnet_produkt add index
ix_nnet_produkt_fargecode(nnet_produkt.nnet_produk t_farge_code);
alter table nnet_produkt add index
ix_nnet_produkt_storrelse_id(nnet_produkt_storrels e_id);
alter table nnet_produkt add index
ix_nnet_produkt_innpris(nnet_produkt_innpris);
alter table nnet_produkt add index
ix_nnet_produkt_utpris(nnet_produkt_utpris);
alter table nnet_produkt add index
ix_nnet_produkt_netto(nnet_produkt_netto);
alter table nnet_produkt add index
ix_nnet_produkt_forhandler_rabatt(nnet_produkt_for handler_rabatt);
alter table nnet_produkt add index
ix_nnet_produkt_bonus(nnet_produkt_bonus);
alter table nnet_produkt add index
ix_nnet_produkt_toppbonus(nnet_produkt_toppbonus);
alter table nnet_produkt add index ix_nnet_produkt_bto(nnet_produkt_bto); alter table nnet_produkt add index
ix_nnet_produkt_quantity(nnet_produkt_quantity);
I cannot create the indices I need considering the schema structure I am
listing. How on earth do I create something like a foreign key

constraints
if it doesn't let you?

Phil


Jul 17 '05 #4

P: n/a
Change table type from MyISAM to InnoDB to have more feature, like foreign
key

Savut

"r" <no***@nowhere.com> wrote in message
news:w8********************@comcast.com...
The recent 4.xx release of mysql supports fkey and ref integrity from
within
innodb tables which are natively supported (no special installatin stuff
necessary). Here's an example of a schema I'm working on for swim meet
volunteers. Note that once workers exist for a particular timeslot, the
slot cannot be deleted (leaving orphans) nor can workers be assigned to
nonexistant slots.
referential integrity.

CREATE TABLE IF NOT EXISTS Slots_HeatSheetWorkers(
HS_ID SMALLINT UNSIGNED NOT NULL,
HS_Date DATE NOT NULL,
HS_TimeStart VARCHAR(8) NOT NULL,
HS_TimeEnd VARCHAR(8) NOT NULL,
HS_Workers_Needed TINYINT UNSIGNED,
PRIMARY KEY(HS_ID)
) TYPE=INNODB;

CREATE TABLE IF NOT EXISTS Workers_HeatSheet(
HS_ID SMALLINT UNSIGNED,
INDEX WHS_H (HS_ID),
FOREIGN KEY (HS_ID) REFERENCES Slots_HeatSheetWorkers(HS_ID)
ON DELETE RESTRICT,
WHS_ID SMALLINT UNSIGNED AUTO_INCREMENT,
NameFirst VARCHAR(255) NOT NULL,
NameLast VARCHAR(255) NOT NULL,
EMail VARCHAR(255) NOT NULL,
PRIMARY KEY(WHS_ID)
) TYPE=INNODB;






"Ygor" <so*****@anywhere.com> wrote in message
news:Tu********************@comcast.com...
wish I knew the answer to this one...
I believe foreign key constriants work with innodb tables but myisam

tables
are the default.
I'm working on a project in which I really need to enforce referential
integrity but I'm not sure how to assault the learning curve on the
innodb
setup, if innodb support is even possible at all through my web/database
hosting service.
r


"Phil Powell" <so*****@erols.com> wrote in message
news:pdy7b.146966$xf.64452@lakeread04...
> create table if not exists nnet_produkt_varegruppe (
> nnet_produkt_varegruppe_id int not null auto_increment,
> primary key(nnet_produkt_varegruppe_id),
> nnet_produkt_varegruppe_navn varchar(255) not null
> );
>
>
> create table if not exists nnet_produkt_farge (
> nnet_produkt_farge_id int not null auto_increment,
> primary key(nnet_produkt_farge_id),
> nnet_produkt_farge_code varchar(5) not null,
> nnet_produkt_farge_navn varchar(255) not null
> );
>
> alter table nnet_produkt_farge add index
> ix_nnet_produkt_farge_code(nnet_produkt_farge.nnet _produkt_farge_code);
>
> create table if not exists nnet_produkt_storrelse (
> nnet_produkt_storrelse_id int not null auto_increment,
> primary key(nnet_produkt_storrelse_id),
> nnet_produkt_storrelse_navn varchar(255) not null
> );
>
> create table if not exists nnet_produkt (
> nnet_produkt_id int not null auto_increment,
> primary key(nnet_produkt_id),
> nnet_produkt_varenr varchar(50) not null,
> nnet_produkt_navn varchar(255) not null,
> nnet_produkt_farge_code varchar(5),
> nnet_produkt_storrelse_id int default 0,
> nnet_produkt_kvalitet_id int default 0,
> nnet_produkt_krage varchar(255),
> nnet_produkt_innpris decimal(6,2) not null,
> nnet_produkt_utpris decimal(6,2) not null,
> nnet_produkt_netto decimal(6,2) not null,
> nnet_produkt_forhandler_rabatt decimal(6,2) not null,
> nnet_produkt_bonus decimal(6,2) default 0.00,
> nnet_produkt_toppbonus decimal(6,2) default 0.00,
> nnet_produkt_bto decimal(6,2) default 0.00,
> nnet_produkt_quantity int default 0
> );
>
> alter table nnet_produkt add index
> ix_nnet_produkt_varenr(nnet_produkt_varenr);
> alter table nnet_produkt add index

ix_nnet_produkt_navn(nnet_produkt_navn);
> alter table nnet_produkt add index
> ix_nnet_produkt_fargecode(nnet_produkt.nnet_produk t_farge_code);
> alter table nnet_produkt add index
> ix_nnet_produkt_storrelse_id(nnet_produkt_storrels e_id);
> alter table nnet_produkt add index
> ix_nnet_produkt_innpris(nnet_produkt_innpris);
> alter table nnet_produkt add index
> ix_nnet_produkt_utpris(nnet_produkt_utpris);
> alter table nnet_produkt add index
> ix_nnet_produkt_netto(nnet_produkt_netto);
> alter table nnet_produkt add index
> ix_nnet_produkt_forhandler_rabatt(nnet_produkt_for handler_rabatt);
> alter table nnet_produkt add index
> ix_nnet_produkt_bonus(nnet_produkt_bonus);
> alter table nnet_produkt add index
> ix_nnet_produkt_toppbonus(nnet_produkt_toppbonus);
> alter table nnet_produkt add index ix_nnet_produkt_bto(nnet_produkt_bto); > alter table nnet_produkt add index
> ix_nnet_produkt_quantity(nnet_produkt_quantity);
>
>
> I cannot create the indices I need considering the schema structure I
> am
> listing. How on earth do I create something like a foreign key

constraints
> if it doesn't let you?
>
> Phil
>
>




Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.