I'm creating a database for a courtier company and I have 5 relations
CREATE TABLE Customer
(
cid int(7) NOT NULL,
cfname char(25) NOT NULL,
clname char(25) NOT NULL,
aptnum int(100) NOT NULL,
street char(50) NOT NULL,
pobox int(10) NOT NULL,
area char(50) NOT NULL,
country char(50) NOT NULL,
phone int(12) NOT NULL,
PRIMARY KEY (cid)
)ENGINE=INNODB;
CREATE TABLE Orderr
(
orderid int(8) NOT NULL,
origin char(100) NOT NULL,
destination char(100) NOT NULL,
eta date NOT NULL,
weight int(100) NOT NULL,
priority enum('F','R') NOT NULL,
task enum('P','D') NOT NULL,
odate date NOT NULL,
cnum int(12) NOT NULL,
cpin int(8) NOT NULL,
custid int(7) NOT NULL,
PRIMARY KEY (orderid),
FOREIGN KEY (custid) REFERENCES Customer(cid)
)ENGINE=INNODB;
CREATE TABLE History
(
histid int(6) NOT NULL,
orderid int(8) NOT NULL,
status enum('D','O','R') NOT NULL,
current_loc char(50) NOT NULL,
PRIMARY KEY(histid),
FOREIGN KEY (orderid) REFERENCES Orderr(orderid)
)ENGINE=INNODB;
CREATE TABLE Driver
(
driverid int(6) NOT NULL,
dfname varchar(25) NOT NULL,
dlname varchar(25) NOT NULL,
dob date NOT NULL,
phone int(10) NOT NULL,
vehicle int(6) NOT NULL,
PRIMARY KEY (driverid)
)ENGINE=INNODB;
CREATE TABLE Vehicle
(
vid int(6) NOT NULL,
num_plate varchar(6) NOT NULL,
vtype enum('T','B','P') NOT NULL,
driverr int(6) NOT NULL,
orders int(8) NOT NULL,
PRIMARY KEY (vid),
FOREIGN KEY (driverr) REFERENCES Driver(driverid),
FOREIGN KEY (orders) REFERENCES Orderr(orderid)
)ENGINE=INNODB;
I am trying to create a VIEW for the Customer giving access to specific columns, here is the query
CREATE VIEW CustView AS
SELECT cfname, clname, aptnum, street, pobox, area, country, origin, destination, weight, priority, task, eta, odate, dfname, dlname, driver.phone
FROM Customer, Orderr, Vehicle, Driver
WHERE Customer.cid=Orderr.custid AND Vehicle.driverr=Driver.driverid AND Orderr.orderid=Vehicle.orders;
When I run SELECT * FROM CustView I do not get the desired output. What changes if any should I make to my query or perhaps to my relations?
Thanks.
As the previous posters remarked, I can only guess. But from your table and view definition, I get the suspicion that you didn't model the relation between Vehicle and Order properly. A Vehicle can be used for many Orders, right? In this case, the Vehicle id must be a foreign key in the Order table. Your design is just the other way round.
Not related to your question, I am suspicious of your History table. The history of orders fulfilled comprises just those orders which are in the past, right? So the history would be just a view that selects orders by past values of some of the dates contained therein.
Related
For some reason my code won't let me create the projects table due to the foreign key error. I have tried a few different things and just cant seem to get it to work, ive tried looking on here for solutions but cant seem to do it. Any help would be much appreciated.
CREATE TABLE PEOPLE (
NAME VARCHAR(32) NOT NULL,
GENDER ENUM('Male', 'Female', 'Other'),
DOB DATE NOT NULL,
SALARY VARCHAR(16) NOT NULL,
PROJECT VARCHAR(32) NOT NULL,
BUSINESS_NAME VARCHAR(32) NOT NULL,
PRIMARY KEY(NAME, PROJECT)
);
CREATE TABLE PEOPLE_EMAILS (
NAME_ID VARCHAR(32) NOT NULL,
EMAIL VARCHAR(64) NOT NULL,
PRIMARY KEY(EMAIL),
FOREIGN KEY(NAME_ID) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PEOPLE_PHONE (
NAME_ID2 VARCHAR(32) NOT NULL,
PHONE_NUMBER VARCHAR(32) NOT NULL,
PRIMARY KEY (PHONE_NUMBER),
FOREIGN KEY(NAME_ID2) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PROJECTS (
PROJECT_NAME VARCHAR(32) NOT NULL,
PROJECT_LOCATION VARCHAR(32) NOT NULL,
BUDGET VARCHAR(16) NOT NULL,
FOREIGN KEY(PROJECT_NAME) REFERENCES PEOPLE(PROJECT)
);
Presumably, you want the foreign key the other way around. You would expect people to reference projects instead of projects to reference people.
This means that you need to create the projects table first, and then the people table. Also, you need a proper primary key on projects so you can reference it in people (I assumed project_name).
CREATE TABLE PROJECTS (
PROJECT_NAME VARCHAR(32) NOT NULL,
PROJECT_LOCATION VARCHAR(32) NOT NULL,
BUDGET VARCHAR(16) NOT NULL,
PRIMARY KEY (PROJECT_NAME)
);
CREATE TABLE PEOPLE (
NAME VARCHAR(32) NOT NULL,
GENDER ENUM('Male', 'Female', 'Other'),
DOB DATE NOT NULL,
SALARY VARCHAR(16) NOT NULL,
PROJECT VARCHAR(32) NOT NULL,
BUSINESS_NAME VARCHAR(32) NOT NULL,
PRIMARY KEY(NAME, PROJECT),
FOREIGN KEY(PROJECT) REFERENCES PROJECTS(PROJECT_NAME)
);
CREATE TABLE PEOPLE_EMAILS (
NAME_ID VARCHAR(32) NOT NULL,
EMAIL VARCHAR(64) NOT NULL,
PRIMARY KEY(EMAIL),
FOREIGN KEY(NAME_ID) REFERENCES PEOPLE(NAME)
);
CREATE TABLE PEOPLE_PHONE (
NAME_ID2 VARCHAR(32) NOT NULL,
PHONE_NUMBER VARCHAR(32) NOT NULL,
PRIMARY KEY (PHONE_NUMBER),
FOREIGN KEY(NAME_ID2) REFERENCES PEOPLE(NAME)
);
Demo on DB Fiddle
I am building a financial app, and it requires the following tables:
tax
item, which can have one tax
invoice, which can have many items
To the question:
An invoice must have the ability to contain 3 categories of emails:
Recipient emails
CC emails
BCC emails
So far I just have a recipients/cc/bcc column, but I don't think it's good because I'd basically have to concatenate the emails together and separate them by a comma or something.
I also thought about a generic email table, but then I'd have to create invoice_recipient_email, invoice_cc_email and invoice_bcc_email tables respectively, in order to link the emails back to the particular invoice ID as well as categorize them by the 3 types.
Can someone advice me on my second solution, or provide a better way to do this?
Here is my current schema:
CREATE TABLE tax (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
rate INT(3) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE item (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
description VARCHAR(1000) NOT NULL,
quantity INT(10) NOT NULL,
price INT(10) NOT NULL,
CONSTRAINT `f_tax_item_tax_id` FOREIGN KEY (`tax_id`) REFERENCES `tax` (`id`),
PRIMARY KEY (id)
);
CREATE TABLE invoice (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
invoice_number VARCHAR(255) NOT NULL,
recipients VARCHAR(1000) NOT NULL,
cc VARCHAR(1000) NOT NULL,
bcc VARCHAR(1000) NOT NULL,
discount INT(10) NOT NULL,
note VARCHAR(500) NOT NULL,
terms VARCHAR(2000) NOT NULL,
due_date TIMESTAMP NOT NULL,
PRIMARY KEY (id)
);
I would add a single recipients table with id (pk), invoice_id (fk), email, recipient_type fields, where recipient type could have one of the following values: to, cc, or bcc. The recipient type field would tell you how to use the email address, therefore you would not need 3 separate tables to hold the 3 different recipient types.
1 record would hold only 1 email address.
You can normalize your emails into a single table and put a flag on it to categorize its type.
CREATE TABLE invoice (
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
invoice_number VARCHAR(255) NOT NULL,
discount INT(10) NOT NULL,
note VARCHAR(500) NOT NULL,
terms VARCHAR(2000) NOT NULL,
due_date TIMESTAMP NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE email(
id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
invoice_id INT(10) not null references invoice(id),
email_type int not null,
addresses varchar(1000),
PRIMARY KEY (id)
);
You can define your email types in another table as well. You can look at this approach implemented in a production environment for phone numbers in MSSSQL but same concept. We however normalize the records to one phone number per record which I would also recommend you do.
CREATE TABLE [dbo].[ActorPhones](
[PKId] [int] IDENTITY(0,1) NOT NULL Primary Key,
[FKActorId] [int] NOT NULL References Actor(PKID),
[FKPhoneTypeId] [int] NOT NULL,
[Number] [varchar](20) NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[PhoneTypes](
[PKId] [int] IDENTITY(0,1) NOT NULL Primary Key,
[PhoneTypeName] [varchar](50) NOT NULL References ActorPhones(PKId),
[PhoneTypeDescription] [varchar](250) NULL,
[ModifiedDate] [datetime] NULL,
[ModifiedBy] [varchar](50) NULL
)
I am reverse engineering a database in MySQL Workbench but it doesn't seem to be importing or recognising the foreign keys. Which means I cant get it to draw the relationships.
While I am trying to get this to work I am using a snippet of a couple of tables, so its nothing complex.
Here are the 2 demo tables that I am trying to get to work:
CREATE TABLE users (
UserID varchar(32) NOT NULL,
OrgID varchar(6) NOT NULL,
RegionID int NULL,
LocationID int NULL,
Name nvarchar(60) NULL,
Crypt varchar(32) NULL,
Security int NULL,
Status int NULL,
PRIMARY KEY ( UserID ),
CONSTRAINT fk_OrgID_Users FOREIGN KEY (OrgID) REFERENCES Organisations(OrgID)
);
CREATE TABLE Organisations(
OrgID varchar(6) NOT NULL,
Name nvarchar(70) NOT NULL,
Address1 varchar(50) NULL,
Address2 varchar(50) NULL,
Address3 varchar(50) NULL,
Town varchar(20) NULL,
County varchar(20) NULL,
PostCode varchar(10) NULL,
NotificationEmail varchar(500) NOT NULL,
PRIMARY KEY ( OrgID )
)
But in MySQL Workbench, there are no relationships or foreign keys being created. If I look at the table data, and the foreign keys tab, theres nothing there?
I think I'm creating the foreign keys correctly, so is there a reason its not importing them?
On further research there were several things that I needed to do for this to work.
I had to make sure that the tables reverse engineered from MySQL were defined as InnoDB.
And I also had to remove the foreign key constraint from the create table command and create the foreign keys after the tables were created, like so:
CREATE TABLE Users (
UserID varchar(32) NOT NULL,
OrgID varchar(6) NOT NULL,
RegionID int NULL,
LocationID int NULL,
Name nvarchar(60) NULL,
Crypt varchar(32) NULL,
Security int NULL,
Status int NULL,
PRIMARY KEY ( UserID )
) ENGINE=InnoDB;
CREATE TABLE Organisations(
OrgID varchar(6) NOT NULL,
Name nvarchar(70) NOT NULL,
Address1 varchar(50) NULL,
Address2 varchar(50) NULL,
Address3 varchar(50) NULL,
Town varchar(20) NULL,
County varchar(20) NULL,
PostCode varchar(10) NULL,
NotificationEmail varchar(500) NOT NULL,
PRIMARY KEY ( OrgID )
) ENGINE=InnoDB;
ALTER TABLE Users ADD CONSTRAINT fk_OrgID_Users FOREIGN KEY (OrgID) REFERENCES Organisations(OrgID);
This then worked fine and the relationships were visible and connected in MySQL Workbench
I am trying TPCH DDL queries on memsql. I am new to memsql. I am not able to convert 5 TPCH ddl sql queries to memsql queries. Not able to achieve foreign key relationship using memsql's FOREIGH SHARD KEY concept. Please help me to covert below 5 out of 8 table creation queries into memsql queries. Tried hard but face lot of different issues.
CREATE TABLE REGION ( R_REGIONKEY INTEGER NOT NULL PRIMARY KEY,
R_NAME CHAR(25) NOT NULL,
R_COMMENT VARCHAR(152)
);
CREATE TABLE NATION ( N_NATIONKEY INTEGER NOT NULL PRIMARY KEY,
N_NAME CHAR(25) NOT NULL,
N_REGIONKEY INTEGER NOT NULL REFERENCES REGION(R_REGIONKEY),
N_COMMENT VARCHAR(152)
);
CREATE TABLE PART ( P_PARTKEY INTEGER NOT NULL PRIMARY KEY,
P_NAME VARCHAR(55) NOT NULL,
P_MFGR CHAR(25) NOT NULL,
P_BRAND CHAR(10) NOT NULL,
P_TYPE VARCHAR(25) NOT NULL,
P_SIZE INTEGER NOT NULL,
P_CONTAINER CHAR(10) NOT NULL,
P_RETAILPRICE DECIMAL(15,2) NOT NULL,
P_COMMENT VARCHAR(23) NOT NULL
);
CREATE TABLE SUPPLIER ( S_SUPPKEY INTEGER NOT NULL PRIMARY KEY,
S_NAME CHAR(25) NOT NULL,
S_ADDRESS VARCHAR(40) NOT NULL,
S_NATIONKEY INTEGER NOT NULL REFERENCES NATION(N_NATIONKEY),
S_PHONE CHAR(15) NOT NULL,
S_ACCTBAL DECIMAL(15,2) NOT NULL,
S_COMMENT VARCHAR(101) NOT NULL
);
CREATE TABLE PARTSUPP ( PS_PARTKEY INTEGER NOT NULL REFERENCES PART(P_PARTKEY),
PS_SUPPKEY INTEGER NOT NULL REFERENCES SUPPLIER(S_SUPPKEY),
PS_AVAILQTY INTEGER NOT NULL,
PS_SUPPLYCOST DECIMAL(15,2) NOT NULL,
PS_COMMENT VARCHAR(199) NOT NULL,
PRIMARY KEY (PS_PARTKEY, PS_SUPPKEY)
);
CREATE TABLE CUSTOMER ( C_CUSTKEY INTEGER NOT NULL PRIMARY KEY,
C_NAME VARCHAR(25) NOT NULL,
C_ADDRESS VARCHAR(40) NOT NULL,
C_NATIONKEY INTEGER NOT NULL REFERENCES NATION(N_NATIONKEY),
C_PHONE CHAR(15) NOT NULL,
C_ACCTBAL DECIMAL(15,2) NOT NULL,
C_MKTSEGMENT CHAR(10) NOT NULL,
C_COMMENT VARCHAR(117) NOT NULL
);
CREATE TABLE ORDERS ( O_ORDERKEY INTEGER NOT NULL PRIMARY KEY,
O_CUSTKEY INTEGER NOT NULL REFERENCES CUSTOMER(C_CUSTKEY),
O_ORDERSTATUS CHAR(1) NOT NULL,
O_TOTALPRICE DECIMAL(15,2) NOT NULL,
O_ORDERDATE DATE NOT NULL,
O_ORDERPRIORITY CHAR(15) NOT NULL,
O_CLERK CHAR(15) NOT NULL,
O_SHIPPRIORITY INTEGER NOT NULL,
O_COMMENT VARCHAR(79) NOT NULL
);
CREATE TABLE LINEITEM ( L_ORDERKEY INTEGER NOT NULL REFERENCES ORDERS(O_ORDERKEY),
L_PARTKEY INTEGER NOT NULL REFERENCES PART(P_PARTKEY),
L_SUPPKEY INTEGER NOT NULL REFERENCES SUPPLIER(S_SUPPKEY),
L_LINENUMBER INTEGER NOT NULL,
L_QUANTITY DECIMAL(15,2) NOT NULL,
L_EXTENDEDPRICE DECIMAL(15,2) NOT NULL,
L_DISCOUNT DECIMAL(15,2) NOT NULL,
L_TAX DECIMAL(15,2) NOT NULL,
L_RETURNFLAG CHAR(1) NOT NULL,
L_LINESTATUS CHAR(1) NOT NULL,
L_SHIPDATE DATE NOT NULL,
L_COMMITDATE DATE NOT NULL,
L_RECEIPTDATE DATE NOT NULL,
L_SHIPINSTRUCT CHAR(25) NOT NULL,
L_SHIPMODE CHAR(10) NOT NULL,
L_COMMENT VARCHAR(44) NOT NULL,
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER),
FOREIGN KEY (L_PARTKEY,L_SUPPKEY) REFERENCES PARTSUPP(PS_PARTKEY, PS_SUPPKEY)
);
I am able to create first 3 tables in memsql but not able to remaining tables.1st and 3rd queries are very simple and worked as it is. I am able to create 2nd table but again not sure whether this is right way to achieve.
CREATE TABLE NATION ( N_NATIONKEY INTEGER NOT NULL,
N_NAME CHAR(25) NOT NULL,
N_REGIONKEY INTEGER NOT NULL,
N_COMMENT VARCHAR(152),
FOREIGN SHARD KEY (N_REGIONKEY) REFERENCES REGION (R_REGIONKEY),
PRIMARY KEY (N_NATIONKEY, N_REGIONKEY)
);
Is it possible to create only Replicate table and not partition in memsql? and how?
Since MemSQL does not support referential integrity, foreign shard keys are an optimization aid and not necessary. Foreign shard keys though do allow you to know at table creation time that two tables may be joined locally (no network traffic) on that key. However, the optimizer does not require foreign shard keys to take advantage of this data locality.
Starting with the ORDERS and LINEITEM tables:
CREATE TABLE ORDERS ( O_ORDERKEY INTEGER NOT NULL PRIMARY KEY,
O_CUSTKEY INTEGER NOT NULL,
O_ORDERSTATUS CHAR(1) NOT NULL,
O_TOTALPRICE DECIMAL(15,2) NOT NULL,
O_ORDERDATE DATE NOT NULL,
O_ORDERPRIORITY CHAR(15) NOT NULL,
O_CLERK CHAR(15) NOT NULL,
O_SHIPPRIORITY INTEGER NOT NULL,
O_COMMENT VARCHAR(79) NOT NULL,
KEY (O_CUSTKEY)
);
CREATE TABLE LINEITEM ( L_ORDERKEY INTEGER NOT NULL,
L_PARTKEY INTEGER NOT NULL,
L_SUPPKEY INTEGER NOT NULL,
L_LINENUMBER INTEGER NOT NULL,
L_QUANTITY DECIMAL(15,2) NOT NULL,
L_EXTENDEDPRICE DECIMAL(15,2) NOT NULL,
L_DISCOUNT DECIMAL(15,2) NOT NULL,
L_TAX DECIMAL(15,2) NOT NULL,
L_RETURNFLAG CHAR(1) NOT NULL,
L_LINESTATUS CHAR(1) NOT NULL,
L_SHIPDATE DATE NOT NULL,
L_COMMITDATE DATE NOT NULL,
L_RECEIPTDATE DATE NOT NULL,
L_SHIPINSTRUCT CHAR(25) NOT NULL,
L_SHIPMODE CHAR(10) NOT NULL,
L_COMMENT VARCHAR(44) NOT NULL,
PRIMARY KEY (L_ORDERKEY,L_LINENUMBER),
FOREIGN SHARD KEY (L_ORDERKEY) REFERENCES ORDERS (O_ORDERKEY),
KEY (L_PARTKEY),
KEY (L_SUPPKEY)
);
In this case we know we can take advantage of a local join between ORDERS and LINEITEM, because they are both sharded on ORDERKEY. ORDERS and LINEITEM are the two biggest tables in TPCH, so we want to ensure that they can be joined locally. Since ORDERS' primary key is O_ORDERKEY, I do not need to specify a shard key for ORDERS. MemSQL will shard by O_ORDERKEY automatically.
I've also put secondary indexes on the remaining foreign key columns. This is useful since there will be joins on the foreign keys.
Applying these concepts to PART, PARTSUPP, SUPPLIER, and CUSTOMER:
CREATE TABLE CUSTOMER ( C_CUSTKEY INTEGER NOT NULL PRIMARY KEY,
C_NAME VARCHAR(25) NOT NULL,
C_ADDRESS VARCHAR(40) NOT NULL,
C_NATIONKEY INTEGER NOT NULL,
C_PHONE CHAR(15) NOT NULL,
C_ACCTBAL DECIMAL(15,2) NOT NULL,
C_MKTSEGMENT CHAR(10) NOT NULL,
C_COMMENT VARCHAR(117) NOT NULL,
KEY(C_NATIONKEY)
);
CREATE TABLE SUPPLIER ( S_SUPPKEY INTEGER NOT NULL PRIMARY KEY,
S_NAME CHAR(25) NOT NULL,
S_ADDRESS VARCHAR(40) NOT NULL,
S_NATIONKEY INTEGER NOT NULL,
S_PHONE CHAR(15) NOT NULL,
S_ACCTBAL DECIMAL(15,2) NOT NULL,
S_COMMENT VARCHAR(101) NOT NULL,
KEY(S_NATIONKEY)
);
CREATE TABLE PART ( P_PARTKEY INTEGER NOT NULL PRIMARY KEY,
P_NAME VARCHAR(55) NOT NULL,
P_MFGR CHAR(25) NOT NULL,
P_BRAND CHAR(10) NOT NULL,
P_TYPE VARCHAR(25) NOT NULL,
P_SIZE INTEGER NOT NULL,
P_CONTAINER CHAR(10) NOT NULL,
P_RETAILPRICE DECIMAL(15,2) NOT NULL,
P_COMMENT VARCHAR(23) NOT NULL
);
CREATE TABLE PARTSUPP ( PS_PARTKEY INTEGER NOT NULL,
PS_SUPPKEY INTEGER NOT NULL,
PS_AVAILQTY INTEGER NOT NULL,
PS_SUPPLYCOST DECIMAL(15,2) NOT NULL,
PS_COMMENT VARCHAR(199) NOT NULL,
PRIMARY KEY (PS_PARTKEY, PS_SUPPKEY),
SHARD KEY(PS_PARTKEY),
KEY(PS_SUPPKEY)
);
Although PARTSUPP and PART are both sharded on the same key (PARTKEY), I do not need to specify a foreign shard key in order to take advantage of a local join between them on that key; the optimizer will pick that up automatically.
In response to your final question, MemSQL does allow you create a replicated table instead of a partitioned table. This is called a reference table and will be useful for the NATION and REGION tables since they are very small. Reference tables are not necessary to run distributed queries, but are a useful optimization.
CREATE REFERENCE TABLE REGION ( R_REGIONKEY INTEGER NOT NULL PRIMARY KEY,
R_NAME CHAR(25) NOT NULL,
R_COMMENT VARCHAR(152)
);
CREATE REFERENCE TABLE NATION ( N_NATIONKEY INTEGER NOT NULL PRIMARY KEY,
N_NAME CHAR(25) NOT NULL,
N_REGIONKEY INTEGER NOT NULL,
N_COMMENT VARCHAR(152)
);
For more documentation on everything described, check out:
http://docs.memsql.com/latest/concepts/distributed_sql/
I have the following table of 60000 rows, in a MySQL 5 database, which is derived from a CSV file:
CREATE TABLE `smts_import` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`AscCode` varchar(5) NOT NULL,
`AscName` varchar(50) NOT NULL,
`RgnCode` varchar(5) NOT NULL,
`RgnName` varchar(100) NOT NULL,
`SCode` varchar(30) NOT NULL,
`SName` varchar(100) NOT NULL,
`AM` int(11) NOT NULL,
`AF` int(11) NOT NULL,
`LG` decimal(10,4) NOT NULL,
`LT` decimal(10,4) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ars_codes` (`AscCode`,`RgnCode`,`SCode`),
KEY `s_code` (`SCode`)
);
and which contains much repeated data. SCodes are unique, and the relationship between the RgnCode and SCode fields is many-to-one, as is that between the AscCode and RgnCode fields.
I want to split up (normalize) the data into three separate tables in a relational manner, such that no data are repeated:
CREATE TABLE `ascs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a_code` varchar(6) NOT NULL,
`a_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `a_code` (`a_code`)
);
CREATE TABLE `rgns` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`a_id` int(11) NOT NULL,
`r_code` varchar(6) NOT NULL,
`r_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `a_id`+`r_code` (`a_id`, `r_code`),
KEY `r_code` (`r_code`)
);
CREATE TABLE `sms` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`r_id` int(11) NOT NULL,
`s_code` varchar(16) NOT NULL,
`s_name` varchar(100) NOT NULL,
`a_m` int(11) NOT NULL,
`a_f` int(11) NOT NULL,
`lg` decimal(10,4) NOT NULL,
`lt` decimal(10,4) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `r_id+s_code` (`r_id`,`s_code`),
KEY `s_code` (`s_code`)
);
where rgns.a_id is a foreign key on ascs.id, and sms.r_id is a foreign key on rgns.id.
I've created the three tables and successfully populated the first two, ascs and rgns, with unique data from the smts_import table. My problem comes when I try to populate the third table sms with just the SCode, SName, AM, AF, LG and LT fields from the smts_import table, PLUS the appropriate id from the rgns table. And here I just get lost, I've tried many variations on the following:
INSERT INTO sms (r_id, s_code, s_name, a_m, a_f, lg, lt)
SELECT DISTINCT sr.id, SCode, SName, AM, AF, LG, LT
FROM sms_import AS si, rgns AS sr, ascs AS sa
WHERE (sr.r_code = si.RgnCode)
AND (sr.a_id = sa.id)
AND (sa.a_code = si.AscCode)
ORDER BY SCode
but I just end up with too many records. How do I write this insert statement to get the appropriate fields from all records from the sms_import table, plus the correct values in the sms.r_id field from the rgns.id field?
Thanks for your help
"the relationship between the RgnCode and SCode fields is many-to-one"
So you also get many rgns.id per SCode.
If you select distinct rgns.id, SCode, etc. you will get each SCode more than once depending on how many RgnCode you have for it.
I would suggest adding a id column to the sms table and create a separate table sms2rgns which contains the one-to-many relations sms.id -> rgns.id