Multiple Inserts across multiple tables

mysql

    Next

  • 1. Index of row
    Is there any function to get index of row in the table complying with some conditions? Thanks.
  • 2. what is wrong with this view script?
    I have the following view that was dumped from myphpadmin: CREATE ALGORITHM=UNDEFINED DEFINER=dbuser@localhost SQL SECURITY DEFINER VIEW mydb.vw_quickpick_companies AS select distinct mydb.subjectemployment.se_company AS se_company,mydb.subjectemployment.se_address AS se_address,mydb.subjectemployment.se_suite AS se_suite,mydb.subjectemployment.se_city AS se_city,mydb.subjectemployment.se_state AS se_state,mydbb.subjectemployment.se_postal AS se_postal,mydb.subjectemployment.se_phone AS se_phone from mydb.subjectemployment where ((mydb.subjectemployment.se_employmenttype <> _latin1'Unemployment') and (mydb.subjectemployment.se_company <> _latin1'')) order by mydb.subjectemployment.se_company; I am trying to create it in mysql 4.1. It says that I have an error. I have tried to run it as dbuser and also as root. Thoughts? ~Gina_M~

Re: Multiple Inserts across multiple tables

Postby Captain Paralytic » Fri, 04 Jun 2010 23:44:18 GMT



I would insert into 1 table and then use the ID from that one as the
primary key to use on inserting to all the rest.

And yes, if you want Commit/Syncpoint/Rollback functionality you need
to use InnoDB.


Re: Multiple Inserts across multiple tables

Postby Lennart Jonsson » Sat, 05 Jun 2010 00:17:27 GMT



I don't understand your data model. Why do you need information
regarding client_company_name in the client_account table? A typical
model for what you describe (at least how I interpret it) would look like:

create table client_account (
   client_account_id  integer not null primary key,
   ...
) engine=innodb;

create table client_account_name (
   ...,

   client_account_id integer not null,
   ...,
   companyname char(100) not null,
   ...
   foreign key ( client_account_id )
   references client_account ( client_account_id )
) engine=innodb;

to retrieve information regarding client_account and the name of the
company:

select x.client_account_id, y.companyname
from client_account x
join client_account_name y
    on x.client_account_id = y.client_account_id

Why is this model not an option for you? I'm not saying that it will fit
for you, but you will have to explain why it does not.


Not possible with MyISAM, neither is foreign keys. If possible, and the
data is important to you, consider moving to InnoDB. You will gain
referential integrity and acid (at least I think innodb is acid
compliant, haven't checked), which will spare you a lot of logic that
otherwise have to be coded in the application. I this particular case
you will have to implement undo operations for your insert/update/delete
operations. This you will have to do in every application that modifies
your data, not to mention the interesting effects a trigger will have on
your application logic ...


/Lennart

[...]

Re: Multiple Inserts across multiple tables

Postby Jerry Stuckle » Sat, 05 Jun 2010 00:25:50 GMT




<snip unrelated info>


It's quite common to have multiple clients with one company.  Different 
departments, for instance, may have separate accounts.  But billing 
still goes to the same place.

And each account may have one or more users authorized to use the 
account; each user would have to be inserted.

But in general, I do agree with you - from the skimpy description, I 
don't think this sounds like a good data model.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
 XXXX@XXXXX.COM 
==================

Re: Multiple Inserts across multiple tables

Postby David » Sat, 05 Jun 2010 00:56:03 GMT

n 3 June, 16:25, Jerry Stuckle < XXXX@XXXXX.COM > wrote:

Thanks for the input so far.

I kept the company name out of the client_account originally so that
everything was small int() fields, but as the database grew and other
options needed char fields did start creeping in.

The way our database works currently is, we have clients who have
multiple addresses (same company name though), we have resellers who
have multiple addresses (same company name) but who can place orders
for their clients, which must also have an account with us. so we can
get multiple addresses linked to different accounts (ie client and
reseller)

I will paste all tables which link to the client account table, as
Jerry has suggested, my original post only showed a few.

CREATE TABLE `client_account` (
`id` int(15) NOT NULL auto_increment,
`acc_id` char(15) NOT NULL,
`address_id` int(5) NOT NULL,
`cname_id` int(5) NOT NULL,
`acontact_id` int(5) NOT NULL,
`scontact_id` int(5) NOT NULL,
`tcontact_id` int(5) NOT NULL,
`atel_id` int(5) NOT NULL,
`stel_id` int(5) NOT NULL,
`ttel_id` int(5) NOT NULL,
`afax_id` int(5) NOT NULL,
`sfax_id` int(5) NOT NULL,
`tfax_id` int(5) NOT NULL,
`sales_emailaddress_id` int(5) NOT NULL,
`accounts_emailaddress_id` int(5) NOT NULL,
`technical_emailaddress_id` int(5) NOT NULL,
`live` enum('N','Y') NOT NULL default 'Y',
`exempt` enum('Y','N') NOT NULL,
`reseller` enum('Y','N') NOT NULL default 'N',
`payment_terms` int(3) default NULL,
`reseller_link_id` char(12) default NULL,
`invoice_out_note` char(50) NOT NULL,
`date` date default NULL,
`suspend` enum('N','Y') NOT NULL default 'N',
`reputation` enum('N','Y') NOT NULL default 'Y',
`uid` int(5) NOT NULL default '6',
PRIMARY KEY (`id`),
UNIQUE KEY `acc_id` (`acc_id`)
)

CREATE TABLE `client_address` (
`id` int(3) NOT NULL auto_increment,
`address` text,
`add2` char(150) default NULL,
`town` char(100) NOT NULL,
`county` char(100) NOT NULL,
`postcode` char(10) NOT NULL,
`country_id` int(4) NOT NULL default '222',
`type` int(5) default NULL,
`acc_id` int(11) default NULL,
`uid` int(5) NOT NULL default '6',
PRIMARY KEY (`id`),
KEY `town` (`town`),
KEY `county` (`county`)
)

CREATE TABLE `client_company_name` (
`id` int(3) NOT NULL auto_increment,
`acc_id` int(5) NOT NULL,
`account` char(12) default NULL,
`companyname` char(100) default NULL,
`live` enum('N','Y') NOT NULL default 'Y',
`suspend` enum('N','Y') NOT NULL default 'N',
`uid` int(11) NOT NULL default '6',
PRIMARY KEY (`id`),
KEY `account` (`account`),
KEY `acc_id` (`acc_id`)
)

CREATE TABLE `client_security_details` ( // 1 row is inserted by
default for security details, but others are added for each product
ordered.
`id` int(5) NOT NULL auto_increment,
`acc_id` int(5) default NULL,
`details` text,
`type` enum('Company','Reseller','Client') default NULL,
PRIMARY KEY (`id`)
)

CREATE TABLE `client_invoice_settings` ( // 1 row is inserted by
default, but others are added for each product ordered.
`id` int(11) NOT NULL auto_increment,
`aid` int(11) default NULL,
`package_id` int(15) NOT NULL,
`cca` enum('N','Y') default 'N',
`early_invoicing` tinyint(2) NOT NULL,
`standing_order` enum('N','Y') default NULL,
`area` enum('Account','Reseller','Client') NOT NULL default
'Account',
PRIMARY KEY (`id`),
KEY `p

Re: Multiple Inserts across multiple tables

Postby Lennart Jonsson » Sat, 05 Jun 2010 03:28:46 GMT


[...]

All agreed, but I don't think I would end up with a data model like the
OP's, no matter what ;-) However, I think you are making an important
point here. It's essential that the business rules and the conceptual
model are well defined (at least the parts we are trying to implement),
before we start writing ddl.

/Lennart

[...]

Re: Multiple Inserts across multiple tables

Postby Captain Paralytic » Sat, 05 Jun 2010 17:27:08 GMT

On Jun 3, 7:28m, Lennart Jonsson < XXXX@XXXXX.COM >




An author who likes playing with language (e.g. Terry Pratchett or
Douglas Adams) would say something like:

"There is no such thing as a perfect data model and this is an
excellent example."


Re: Multiple Inserts across multiple tables

Postby David » Sat, 05 Jun 2010 18:37:10 GMT




I can see everyone who has comments suggests that this is an imperfect
model. I would welcome any feedback on why its imperfect, and what
everyone else would do instead.

Regards
Dave.

Re: Multiple Inserts across multiple tables

Postby Willem Bogaerts » Sat, 05 Jun 2010 19:59:39 GMT

> I can see everyone who has comments suggests that this is an imperfect

Links between tables only need to go one side. So if you have, for
instance, a general Address table, you can insert an address, get its
autonumber value and store that in the accounts table.

It helps to define a foreign key relation in the table. That way, there
are no records that point to related records that no longer exist.

Best regards,
-- 
Willem Bogaerts

Application smith
Kratz B.V.
 http://www.**--****.com/ 

Re: Multiple Inserts across multiple tables

Postby Captain Paralytic » Sat, 05 Jun 2010 20:50:07 GMT





I did that in my first post in this thread.
If you need a 1:m relationship, you do it using the primary key from
the main (1) table and using it as a foreign key in any (m) tables.
If you have a 1:1 relationship, then you either put all the data in a
single table or, if you have a good reason for spreading the data
amongst many tables, you use the same primary key value in all of
them.

Really all you have to do is to follow the standard normalisation
rules:
 http://www.**--****.com/ 

Re: Multiple Inserts across multiple tables

Postby David » Sat, 05 Jun 2010 21:07:35 GMT







Ok, Now I am really confused, because i thought that is what i am
doing?

insert an address, get the last_insert_id and then update the account
table with that id.
I do the same for the contacts, email addresses and numbers

So if im being a bit thick forgive me, but isnt that what you are
talking about?




Re: Multiple Inserts across multiple tables

Postby Jerry Stuckle » Sat, 05 Jun 2010 21:13:59 GMT









You have a 1:m relationship (client:address).  How do you indicate 
multiple addresses if you are inserting the address id in the client table?

Rather, client_id should be a member of address, and nothing need be 
updated in client.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
 XXXX@XXXXX.COM 
==================

Re: Multiple Inserts across multiple tables

Postby David » Sat, 05 Jun 2010 21:51:44 GMT











Oh, so im doing it the wrong way round.


Re: Multiple Inserts across multiple tables

Postby Jerry Stuckle » Sat, 05 Jun 2010 22:05:00 GMT














Yes.  You way, how would you specify multiple addresses for a single 
company?

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
 XXXX@XXXXX.COM 
==================

Re: Multiple Inserts across multiple tables

Postby David » Sat, 05 Jun 2010 22:07:03 GMT

n 4 June, 14:05, Jerry Stuckle < XXXX@XXXXX.COM > wrote:

The account_id is saved in the address table along with the type of
address (ie billing, registration, local office etc..)


Re: Multiple Inserts across multiple tables

Postby Jerry Stuckle » Sat, 05 Jun 2010 22:10:16 GMT

avid wrote:

Ok, so then why do you need to update in the account table?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
XXXX@XXXXX.COM
==================

Similar Threads:

1.Best way to search across multiple tables

I have a horizontally-partitioned database which contains multiple tables of
the same schema which contain messages. Each table has a FULLTEXT index on
(subject, body). Currently, the search script only allows searching one
table at a time, but I'd like to allow searching of all tables at once, or
at least more than one at once. Is there a good way to allow for this? Would
it involve using a view and/or union?

2.Inserting multiple rows into multiple tables

Hi,

I currently have two tables Table1 and Table2, and two 
databases Current and Backup.  Table2 has a foreign key 
relationship with Table1.  I want to insert some rows from 
Table1 and Table2 from Backup into Current.

I know I can insert multiple rows from Backup's Table1 to 
Current's Table1 using something like this.

USE Current
INSERT INTO Table1
   SELECT *
   FROM Backup..Table1
   WHERE Date = '7/31/2003'

However, I don't know how to insert Table2's rows.  I 
probably have to use @@Identity somehow, but I am not sure 
how.  Any ideas?


3.Search Multiple keywords across multiple fields

Hi,

I'm about halfway through building a search engine using ASP, SQL and
Access.
As part of that search engine I need to search multiple keywords across
multiple fields
So far I've figured out how to split a string into words. From here I could:

Construct an SQL statement of varying length that, using the LIKE and AND
operators. This seems damn hard!

Alternately I could concatenate the multiple fields into a variable or array
then search.

What is best - can anyone provide examples ?

Darren


4.searching across multiple fields

I have a query designed to search multiple fields for any word that
starts with the characters entered in a search. Each field will
typically contain more than one word. The query takes the form:

SELECT
field1, field2
FROM table1 WHERE ((field1 LIKE "a%") OR (field1 LIKE "% a%") OR (
field2 LIKE "a%") OR (field2 LIKE "% a%"))

Is there a more efficient way to search than this?

Michael

5.search across multiple records?

Hello,

I have an HTML file with <form etc in which the names of composers are
listed. Say Beethoven, Mozart,  etc.

In the MySQL database I have records

composer1     Beethoven
composer1work1            Symphony No.4

composer2     Mozart
composer2work1            Piano Sonata No.2

etc etc.

How can I form the SQL statement so that the search will find
composers and their works in different records?

Any pointers please?!

Cheers,

Geoff




6. Doing JOIN with multiple table on multiple fields in each table



Return to mysql

 

Who is online

Users browsing this forum: No registered users and 62 guest