473,246 Members | 1,433 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

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

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
4 4031
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: js | last post by:
I am trying to create a primary key constraint on a view in the following statement. However, I got an error ORA-00907: missing right parenthesis. If the CONSTRAINT clause is removed, then the...
0
by: Morten Gulbrandsen | last post by:
USE company; DROP TABLE IF EXISTS EMPLOYEE; CREATE TABLE EMPLOYEE ( # PK SSN CHAR(9) NOT NULL, # FK SUPERSSN CHAR(9), DNO INT NOT NULL DEFAULT 1, CONSTRAINT EMPPK
0
by: John Towell | last post by:
------=_NextPart_000_0008_01C360DD.C2475700 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit We are trying to load a set of data using Hibernate (O/R mapping tool)....
0
by: Morten Gulbrandsen | last post by:
Hello, starting from some software database spesification, defined in some Enhanced entity relationship diagram, resulting in all kinds of relationships, 1:1 1:Many Many:1
10
by: Phil Powell | last post by:
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...
2
by: Jeff Silverman | last post by:
I am working on my first database and I think I want to build a table with a foreign key in it. The O'Reilly book on MySQL says that MySQL does not support foreign keys, but it still talks about...
5
by: kutty | last post by:
Hi All, I am loading data to a child table from a text file. the text files also contains data not referenced by parent key. while loading the data if one row fails to satisfies the constraint...
27
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
2
by: jarea | last post by:
I have read quite a bit about this error but I have yet to find the solution to my problem. I am trying to execute the following mysql statement: alter table line_items add...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.