473,387 Members | 1,722 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,387 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 4037
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.