Two tables: users and messages
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(10) NOT NULL,
`password` varchar(10) NOT NULL,
`email` varchar(100) NOT NULL,
`verifystring` varchar(20) NOT NULL,
`active` tinyint(4) NOT NULL,
`usertype` tinyint(4) NOT NULL,
`img` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=8 ;
CREATE TABLE IF NOT EXISTS `messages` (
`id` tinyint(4) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
`user_id` int(11) NOT NULL,
`topic_id` int(11) NOT NULL,
`subject` varchar(100) NOT NULL,
`body` text NOT NULL,
`img` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `topic_id` (`topic_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=51 ;
trigger:
CREATE TRIGGER `updateimg` AFTER UPDATE ON `users`
FOR EACH ROW begin
update messages set img = new.img where messages.user_id = users.id;
end
img field is keeping the name of the image user has uploaded.
How can i copy the value img from table users to table messages when it is updated?
Thats should do it:
CREATE TRIGGER update AFTER UPDATE ON table1
for each ROW
begin
update table2 set y = new.y where table2.user_id = new.id;
end
Related
after form submission, the data is stored in database. This is the query i already have:
CREATE TABLE IF NOT EXISTS `registered_users` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`gender` varchar(20) NOT NULL,
`date_birth` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`postal_code` varchar(255) NOT NULL,
`place` varchar(255) NOT NULL,
`school_name` varchar(255) NOT NULL,
`email` varchar(55) NOT NULL,
`tel` varchar(55) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
This is how i insert the data into the DB:
Now i want to insert a new row in the DB which can have only 2 values: "active" or "inactive" for each user. Default on "inactive". That should be inserted automatically for each user after submit. How can i do that?
When you create a table in a mysql db you can specify a default value for attributes. So if you want a boolean "active" / "inactive" flag you might want to try adding an active attribute to your table. See the added last column here:
CREATE TABLE IF NOT EXISTS `registered_users` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`gender` varchar(20) NOT NULL,
`date_birth` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`postal_code` varchar(255) NOT NULL,
`place` varchar(255) NOT NULL,
`school_name` varchar(255) NOT NULL,
`email` varchar(55) NOT NULL,
`tel` varchar(55) NOT NULL,
`active` BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
Ref.: https://dev.mysql.com/doc/refman/5.7/en/create-table.html
I am a novice to MySQL
I have a database where I want to populate sets of records from 'sales' table where condition is sales is not delivered by comparing with 'deliveries' table of 'deliveries.reference_no'.
Both Tables has reference_no in common which is the invoice ref number.
I tried few SQL and got all common filed as it is and tried this one below but its displaying #1052 - Column 'date' in field list is ambiguous.
SELECT sales.id AS sid, date, reference_no, biller_name, customer_name, total_tax, total_tax2, total, internal_note FROM sales LEFT JOIN deliveries ON (sales.reference_no = deliveries.reference_no)
For more information below is my two table schema
Sales
CREATE TABLE IF NOT EXISTS `sales` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reference_no` varchar(55) NOT NULL,
`warehouse_id` int(11) DEFAULT NULL,
`biller_id` int(11) NOT NULL,
`biller_name` varchar(55) NOT NULL,
`customer_id` int(11) NOT NULL,
`customer_name` varchar(55) NOT NULL,
`date` date NOT NULL,
`note` varchar(1000) DEFAULT NULL,
`internal_note` varchar(1000) DEFAULT NULL,
`inv_total` decimal(25,2) NOT NULL,
`total_tax` decimal(25,2) DEFAULT NULL,
`total` decimal(25,2) NOT NULL,
`invoice_type` int(11) DEFAULT NULL,
`in_type` varchar(55) DEFAULT NULL,
`total_tax2` decimal(25,2) DEFAULT NULL,
`tax_rate2_id` int(11) DEFAULT NULL,
`inv_discount` decimal(25,2) DEFAULT NULL,
`discount_id` int(11) DEFAULT NULL,
`user` varchar(255) DEFAULT NULL,
`updated_by` varchar(255) DEFAULT NULL,
`paid_by` varchar(55) DEFAULT 'cash',
`count` int(11) DEFAULT NULL,
`shipping` decimal(25,2) DEFAULT '0.00',
`pos` tinyint(4) NOT NULL DEFAULT '0',
`paid` decimal(25,2) DEFAULT NULL,
`cc_no` varchar(20) DEFAULT NULL,
`cc_holder` varchar(100) DEFAULT NULL,
`cheque_no` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=6 ;
Deliveries
CREATE TABLE IF NOT EXISTS `deliveries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
`time` varchar(10) NOT NULL,
`reference_no` varchar(55) NOT NULL,
`customer` varchar(55) NOT NULL,
`address` varchar(1000) NOT NULL,
`note` varchar(1000) DEFAULT NULL,
`user` varchar(255) DEFAULT NULL,
`updated_by` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
Currently I have a CI program and I am doing like this no Luck what I want to populate
CI PHP
function getdatatableajax()
{
if($this->input->get('search_term')) { $search_term = $this->input->get('search_term'); } else { $search_term = false;}
$this->load->library('datatables');
$this->datatables
->select("sales.id as sid, date, reference_no, biller_name, customer_name, total_tax, total_tax2, total, internal_note")
->from('sales');
$this->datatables->add_column("Actions",
"<center><a href='#' title='$2' class='tip' data-html='true'><i class='icon-folder-close'></i></a> <a href='#' onClick=\"MyWindow=window.open('index.php?module=sales&view=view_invoice&id=$1', 'MyWindow','toolbar=0,location=0,directories=0,status=0,menubar=yes,scrollbars=yes,resizable=yes,width=1000,height=600'); return false;\" title='".$this->lang->line("view_invoice")."' class='tip'><i class='icon-fullscreen'></i></a>
<a href='index.php?module=sales&view=add_delivery&id=$1' title='".$this->lang->line("add_delivery_order")."' class='tip'><i class='icon-road'></i></a>
<a href='index.php?module=sales&view=pdf&id=$1' title='".$this->lang->line("download_pdf")."' class='tip'><i class='icon-file'></i></a>
<a href='index.php?module=sales&view=email_invoice&id=$1' title='".$this->lang->line("email_invoice")."' class='tip'><i class='icon-envelope'></i></a>
</center>", "sid, internal_note")
->unset_column('sid')
->unset_column('internal_note');
echo $this->datatables->generate();
}
Wow, no one answered this yet? Strange.
Your error is because the date field is in both tables. You have to specify which table you want to select date from. So you must write either sales.date or deliveries.date. This goes with the rest of them, once you fix sales.date, your next field reference_no will generate the same error. If you want data from BOTH tables you'll need to assign aliases to them as you did with the first column.
sales.id AS sid, sales.date AS sales_date, deliveries.date AS deliveries_date etc...
I'm running the following MySQL query in PHP.
"SELECT *
FROM `challenges`,`verifications`
WHERE (`challenges`.`user_id`='".$this->record['id']."' OR `challenges`.`opponent_id`='".$this->record['id']."')
AND `challenges`.`is_verified`='0'
AND (`challenges`.`status`='in-progress' OR `challenges`.`status`='pending')
AND
(
(`verifications`.`user_id`='".$this->record['id']."' OR `verifications`.`opponent_id`='".$this->record['id']."')
AND (`verifications`.`user_verified`!=NULL AND `verifications`.`opponent_verified`=NULL)
)
LIMIT 100";
This query is returning duplicate records for some reason. If anyone has any insights, I would greatly appreciate it.
Here is the structure for the two tables (challenges and verifications):
Challenges Table:
CREATE TABLE `challenges` (
`id` int(11) NOT NULL auto_increment,
`wager` int(11) NOT NULL,
`type` varchar(255) NOT NULL,
`user_id` int(11) NOT NULL,
`opponent_id` int(11) NOT NULL,
`start_date` date NOT NULL,
`date_created` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
`start_time` time NOT NULL,
`is_verified` tinyint(1) NOT NULL default '0',
`status` varchar(255) NOT NULL default 'pending',
`winner_id` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;
Verifications Table:
CREATE TABLE `verify` (
`id` int(11) NOT NULL auto_increment,
`user_id` int(11) NOT NULL,
`opponent_id` int(11) NOT NULL,
`challenge_id` int(11) NOT NULL,
`user_verified` int(11) default NULL,
`opponent_verified` int(11) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `challenge_id` (`challenge_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=0;
Thanks for your help, and if you need any more info please let me know.
You have to add condition:
challenges.id = verify.challenge_id
to where clause as below
"SELECT *
FROM `challenges`,`verifications`
WHERE `challenges`.`id` = `verify`.`challenge_id`
AND (`challenges`.`user_id`='".$this->record['id']."'
OR `challenges`.`opponent_id`='".$this->record['id']."')
AND `challenges`.`is_verified`='0'
AND (`challenges`.`status`='in-progress' OR `challenges`.`status`='pending')
AND ( (`verifications`.`user_id`='".$this->record['id']."'
OR `verifications`.`opponent_id`='".$this->record['id']."')
AND (`verifications`.`user_verified`!=NULL
AND `verifications`.`opponent_verified`=NULL)
)
LIMIT 100";
or using ANSI-92
"SELECT *
FROM `challenges` as `challenges`
JOIN `verifications` as `verifications` on `challenges`.`id` = `verify`.`challenge_id`
WHERE (`challenges`.`user_id`='".$this->record['id']."' OR `challenges`.`opponent_id`='".$this->record['id']."')
AND `challenges`.`is_verified`='0'
AND (`challenges`.`status`='in-progress' OR `challenges`.`status`='pending')
AND
(
(`verifications`.`user_id`='".$this->record['id']."' OR `verifications`.`opponent_id`='".$this->record['id']."')
AND (`verifications`.`user_verified`!=NULL AND `verifications`.`opponent_verified`=NULL)
)
LIMIT 100";
I have been making a database and learning along the way. I recently got into using InnoDB and using foreign keys to connect tables together.
But in all honestly I'm probably making my foreign keys blindly. What is the correct set and check list that I need to use when making a foreign key.
My understanding with foreign keys is that I have a Master Table, and any changes in my Master Table are reflected to any tables that hold a foreign key to a specific column in it.
So my current log-in system has a set up like this
users
=====
id PK
username
password
and my other tables look like this
contacts
========
id PK
user_id references `users`.`id`
group
name
address
groups
======
id PK
user_id
group_name
group_contacts
==============
id PK
group_id references `group`.`id`
contact_id references `contacts`.`id`
To my understanding these tables can be deleted when the Master Table is deleted using the ON DELETE CASCADE option correct?
My problem now is that I can't seem to make group_id and contact_id a Foreign key to groups.id and contacts.id with this setup. I get an error when running the SQL statements.
I'm trying to make my address book so that when a user places a contact into a group becomes all automated and I don't have to change much information. The group_contact table is what I THINK I will querying when I want to see where each contact belongs to. If I change the name of a group it will reflect across all tables right? This is where foreign keys come in and I'm confusing myself with how these keys should behave for me.
But like I said I can't seem to make a foreign key without getting an error.
I know I can Google my Foreign Key question, which I have but I can't seem to learn this way without getting feedback and input to my exact scenario ;(
Not to ask too much but because of my confusion I'm also having a hard time trying to see how I can make a PHP script to handle the group name change and query the database to pull down contacts that belong to a specific group.
This would really help me a lot guys, and I hope to learn something!
My query is this:
ALTER TABLE `list_`.`groups_contacts`
ADD CONSTRAINT `group_id` FOREIGN KEY (`group_id`) REFERENCES `list_`.`groups` (`id`)
ADD CONSTRAINT `contact_id` FOREIGN KEY (`contact_id`) REFERENCES `list_`.`contacts` (`id`);
My database looks like this:
CREATE TABLE `list_`.`buyer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`isClosed` tinyint(1) NOT NULL,
`display_limit` int(1),
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`prop_address` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`state` varchar(11) NOT NULL,
`zip` varchar(5) NOT NULL,
`cell_phone` varchar(16) NOT NULL,
`home_phone` varchar(16) NOT NULL,
`other1` varchar(16) NOT NULL,
`other2` varchar(16) NOT NULL,
`comments` text NOT NULL,
`comment_exist` tinyint(1) NOT NULL,
`comment_date` text NOT NULL,
`date_added` date NOT NULL,
`date_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8 AUTO_INCREMENT=23 ;
CREATE TABLE `list_`.`company` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL DEFAULT '0',
`company_name` varchar(128) NOT NULL,
PRIMARY KEY (`id`, `user_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;
CREATE TABLE `list_`.`contacts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`group` varchar(128) NOT NULL,
`first_name` varchar(128) NOT NULL,
`last_name` varchar(128) NOT NULL,
`address` varchar(128) NOT NULL,
`city` varchar(128) NOT NULL,
`state` varchar(2) NOT NULL,
`zip` int(5) NOT NULL,
`phone_number` varchar(16) NOT NULL,
`cell_number` varchar(16) NOT NULL,
`work_number` varchar(16) NOT NULL,
`fax_number` varchar(16) NOT NULL,
`email` varchar(128) NOT NULL,
`company` varchar(55) NOT NULL,
`title` varchar(56) NOT NULL,
`notes` text NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`),
KEY `group` (`group`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
CREATE TABLE `list_`.`groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`position` int(8) unsigned NOT NULL DEFAULT '0',
`name` varchar(128) NOT NULL,
`date_added` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`),
KEY `name` (`name`)
) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8 AUTO_INCREMENT=32 ;
CREATE TABLE `list_`.`prospect` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`isClosed` tinyint(1) DEFAULT '0',
`display_limit` int(1) DEFAULT '0',
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`prop_address` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`state` varchar(11) NOT NULL,
`zip` varchar(5) NOT NULL,
`cell_phone` varchar(16) NOT NULL,
`home_phone` varchar(16) NOT NULL,
`other1` varchar(16) NOT NULL,
`other2` varchar(16) NOT NULL,
`comments` text NOT NULL,
`date_added` date NOT NULL,
`date_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 AUTO_INCREMENT=7 ;
CREATE TABLE `list_`.`seller` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`file` int(11) DEFAULT NULL,
`isClosed` tinyint(1) NOT NULL,
`display_limit` int(1) NOT NULL DEFAULT '0',
`first_name` varchar(255) NOT NULL,
`last_name` varchar(255) NOT NULL,
`prop_address` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`state` varchar(22) NOT NULL,
`zip` varchar(5) NOT NULL,
`cell_phone` varchar(16) NOT NULL,
`home_phone` varchar(16) NOT NULL,
`other1` varchar(16) NOT NULL,
`other2` varchar(16) NOT NULL,
`comments` text NOT NULL,
`comment_exist` tinyint(1) NOT NULL,
`comment_date` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_added` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`date_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8 AUTO_INCREMENT=21 ;
CREATE TABLE `list_`.`settings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` tinyint(11) NOT NULL,
`seller_display_limit` int(4) DEFAULT '0',
`buyer_display_limit` int(4) DEFAULT '0',
`prospect_display_limit` int(4) DEFAULT '0',
`property_display_limit` int(4) DEFAULT '0',
`date_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`user_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
CREATE TABLE `list_`.`users` (
`id` tinyint(11) NOT NULL AUTO_INCREMENT,
`md5_id` varchar(200) NOT NULL,
`username` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`user_level` tinyint(1) DEFAULT '1',
`first_name` varchar(200) NOT NULL,
`last_name` varchar(200) NOT NULL,
`email` varchar(200) DEFAULT NULL,
`approved` int(1) NOT NULL,
`banned` int(1) NOT NULL,
`date_joined` date NOT NULL,
`ip` varchar(15) DEFAULT NULL,
`activation_code` int(9) DEFAULT NULL,
`ckey` varchar(220) NOT NULL,
`ctime` varchar(220) NOT NULL,
`last_logged_in` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`account_number` varchar(128) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
ALTER TABLE `list_`.`buyer`
ADD CONSTRAINT `buyer_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE;
--
-- Constraints for table `company`
--
ALTER TABLE `list_`.`company`
ADD CONSTRAINT `company_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE;
--
-- Constraints for table `contacts`
--
ALTER TABLE `list_`.`contacts`
ADD CONSTRAINT `contacts_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
ADD CONSTRAINT `group_ibfk_2` FOREIGN KEY (`group`) REFERENCES `groups` (`name`) ON UPDATE CASCADE;
--
-- Constraints for table `groups`
--
ALTER TABLE `list_`.`groups`
ADD CONSTRAINT `group_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
--
-- Constraints for table `prospect`
--
ALTER TABLE `list_`.`prospect`
ADD CONSTRAINT `prospect_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
--
-- Constraints for table `seller`
--
ALTER TABLE `list_`.`seller`
ADD CONSTRAINT `seller_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
--
-- Constraints for table `settings`
--
ALTER TABLE `list_`.`settings`
ADD CONSTRAINT `settings_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`);
I think you slightly misunderstood the concept of Foreign Keys. Changing the name of a group is not supposed to reflect on any other table, you just change your group-table.
Assuming you have this simple scenario, where one Contact can belong only to one Group:
Groups
id
group_name
Contacts
id
group_id -> Groups.id
first_name
...
Your Contacts do not have the information about the group_name. You just store the reference to your Groups.id.
If you want to query your contacts and the name of their group, you join those two tables:
Select c.first_name, g.group_name
From contacts c
Join groups g On ( g.id = c.group_id )
If you want to change the name of a group, you do a simple update:
Update groups
Set group_name = 'Your new group name'
Where id = 99 --# The id of the group to rename
This only changes your Groups table, without changing your Contacts.
Your Foreign Key on Contacts.group_id is there to ensure the referential integrity. This means, that you are not allowed to have a contact with group_id=88 if there is no record in Groups with id=88.
Using ON DELETE CASCADE would delete all Contacts that are members of a certain group, once you delete that group.
Here is my USER table
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`expiry` varchar(6) NOT NULL,
`contact_id` int(11) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(100) NOT NULL,
`level` int(3) NOT NULL,
`active` tinyint(4) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`,`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
And here is my contact_info table
CREATE TABLE IF NOT EXISTS `contact_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`email_address` varchar(255) NOT NULL,
`company_name` varchar(255) NOT NULL,
`license_number` varchar(255) NOT NULL,
`phone` varchar(30) NOT NULL,
`fax` varchar(30) NOT NULL,
`mobile` varchar(30) NOT NULL,
`category` varchar(100) NOT NULL,
`country` varchar(20) NOT NULL,
`state` varchar(20) NOT NULL,
`city` varchar(100) NOT NULL,
`postcode` varchar(50) NOT NULL,
PRIMARY KEY (`id`,`email_address`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The system uses username to login users. I want to modify it in such a way that it uses email for login. But there is no email_address in users table.
I have added foreign key - email in user table(which is email_address in contact_info).
How should I query database?
No, no, no, no no. Seriously, no. Don't make me come over there :-)
You're breaking third normal form by storing the email address twice.
The relationship need only be a short one, that of id. Assuming you're not guaranteeing the IDs will be identical in the two tables (i.e., my users.id isn't necessarily equal to my contact_info.id), just add a ci_id to the users table to act as a foreign key to the contact_info table.
Then the query to get a user's username and email will be something like:
select u.username, ci.email
from users u, contact_info ci
where u.username = 'paxdiablo'
and u.ci_id = ci.id;