I have two tables users and lead_contacts which have similar data. When somebody buys a product they become a user. How should I modify the two create staements below so:
- that the leads table receives a new
entry with first_name, last_name, company and email, when a user is created. -
the first_name, last_name, company and email in users table is updated
automatically when the leads table info is changedCREATE TABLE
lead_contacts(
contact_idint(11) NOT NULL auto_increment,
user_idint(11) unsigned NOT NULL default ‘0’,
emailvarchar(100) NOT NULL default ”,
companyvarchar(50) NOT NULL default ”,
first_namevarchar(50) NOT NULL default ”,
last_namevarchar(50) NOT NULL default ”,
addressvarchar(100) NOT NULL default ”,
address_2varchar(100) NOT NULL default ”,
cityvarchar(50) NOT NULL default ”,
statevarchar(50) NOT NULL default ”,
countryvarchar(50) NOT NULL default ”,
postal_codevarchar(30) NOT NULL default ”,
phonevarchar(30) NOT NULL default ”,
faxvarchar(30) NOT NULL default ”,
ship_bill_sameenum(‘Y’, ‘N’) NOT NULL default ‘Y’,
notestext NOT NULL,
admin_notestext NOT NULL,
list_namevarchar(50) NOT NULL default ”,
lead_listint(11) unsigned NOT NULL default ‘0’,
is_master_listenum(‘N’, ‘Y’) NOT NULL default ‘N’,
active_workenum(‘Y’, ‘N’) NOT NULL default ‘Y’,
parentIDint(11) unsigned NOT NULL default ‘0’,
PRIMARY KEY (contact_id),
KEYuser_id(user_id),
KEYlead_list(lead_list),
KEYis_master_list(is_master_list),
KEYactive_work(active_work),
KEYparentID(parentID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 PACK_KEYS=1;CREATE TABLE
users(
userIDint(11) NOT NULL auto_increment,
access_levelint(11) NOT NULL default ‘0’,
usernamevarchar(100) NOT NULL default ”,
passwordvarchar(100) NOT NULL default ”,
first_namevarchar(50) NOT NULL default ”,
last_namevarchar(50) NOT NULL default ”,
companyvarchar(100) NOT NULL default ”,
emailvarchar(100) NOT NULL default ”,
PRIMARY KEY (userID),
UNIQUE KEYusername(username)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
I think you misunderstand how foreign keys work.
A reference from leads to users means that a row must already exist in users before a row in leads can reference it.
There’s no way in SQL to make a dependent table automatically create a row in its parent table on demand.
You could do this with a trigger, I suppose. But not a foreign key constraints. Besides, the values to fill into the parent table must come from somewhere. Either you need to specify them in an INSERT statement in your application or in a trigger, or else use the defaults defined for each column in the users table. Given that you have a unique constraint on
users.username, I don’t think this would be possible from a trigger.Re: your followup question in the comment:
No, a foreign key can’t do what you’re describing. When you modify info in the
leadstable (the table with the foreign key), the only thing a foreign key can do is prevent the modification if you try to change theleads.user_idcolumn to a value that is not found in theuserstable.The foreign key in the child (
leads) table can’t change anything in the parent (users) table.I’m not sure what is the source of your mistaken understanding. Did you read it somewhere or see someone do something like this?