473,383 Members | 1,862 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,383 software developers and data experts.

How to create a table (many-to-many association) from the primary keys of tables?

In mysql, I need to create a table to store the many-to-many relationship of the other two tables.

Here is the problem:

the first table has three fields that act as the primary key of the table

the second table has only one field for the primary key

i know the association table will have the primary keys of both tables as its own primary key but it seems that I can't do that. It appears that the three fields primary key in the first table is giving me some trouble!

Is this allowed in MYSQL? If so, could someone please provide a sample sql code.

Thanks.
Mar 16 '09 #1
1 7586
Is this what you want?
CREATE TABLE C
(
AFiled_a INTEGER NOT NULL,
AFiled_b INTEGER NOT NULL,
AFiled_c INTEGER NOT NULL,
BFiled_a INTEGER NOT NULL,
PRIMARY KEY (AFiled_a, AFiled_b, AFiled_c, BFiled_a),
KEY (AFiled_a, AFiled_b, AFiled_c),
KEY (BFiled_a)
)
;


CREATE TABLE B
(
Filed_a INTEGER NOT NULL,
BContent TEXT,
PRIMARY KEY (Filed_a)
)
;


CREATE TABLE A
(
AFiled_a INTEGER NOT NULL,
AFiled_b INTEGER NOT NULL,
AFiled_c INTEGER NOT NULL,
AContent TEXT,
PRIMARY KEY (AFiled_a, AFiled_b, AFiled_c)
)
;





ALTER TABLE C ADD CONSTRAINT FK_C_A
FOREIGN KEY (AFiled_a, AFiled_b, AFiled_c) REFERENCES A (AFiled_a, AFiled_b, AFiled_c)
;

ALTER TABLE C ADD CONSTRAINT FK_C_B
FOREIGN KEY (BFiled_a) REFERENCES B (Filed_a)
;
Mar 18 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: blindsey | last post by:
Is there a tool that can take an Access database and generate SQL "CREATE TABLE" statements for all the tables in it?
2
by: Tony | last post by:
Hi, I have a table with two primary keys as below. Table: Mouse Primary key 1: MouseLine Primary key 2: MouseNumber Field 3: DateOfBirth In a particular form, if I know MouseLine and...
115
by: LurfysMa | last post by:
Most of the reference books recommend autonum primary keys, but the Access help says that any unique keys will work. What are the tradeoffs? I have several tables that have unique fields. Can...
5
by: abctech | last post by:
Hi all My frontend is Java and backend is MySql. The tables that I create have auto incrementing primary keys. This is the syntax of my create statement :- Create table ABC(ID Int not...
1
by: Johnsi Rani | last post by:
I have already created the tables in access.I want to have the create table query for the existing tables. plz help me
2
by: Danny | last post by:
Hello, We imported a bunch of tables from a database and realized that the primary keys weren't copied to the destination db. In order to re- create the keys, we need to know which tables have...
1
jamesd0142
by: jamesd0142 | last post by:
select 'ALTER TABLE ' + primary_table + ' drop CONSTRAINT ' + primary_key_name + ' FOREIGN KEY (' + foreign_column_1 + ')' from sysfkeys order by foreign_table How can I change this code to...
6
by: babamc4 | last post by:
I have a main form (mainformlung) with 5 subforms (followupacute, followuplate, biochemresults, haemresults and pftresults). I have copied Allen Browne's Audit Trail code (thanks to Allen Browne)...
1
by: =?Utf-8?B?Rm9lZg==?= | last post by:
I have a BindingSource with an underlying table with two primary keys. To set the position with one primary key works fine as shown in the code snippet below: Dim index As Integer =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.