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;
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 have a table called participants:
CREATE TABLE IF NOT EXISTS `participants` (
`id` int(8) NOT NULL AUTO_INCREMENT,
`email` varchar(30) NOT NULL,
`name` varchar(40) NOT NULL,
`birthdate` date NOT NULL,
`personal_number` varchar(16) DEFAULT NULL,
`phone` varchar(16) NOT NULL,
`photo` varchar(100) NOT NULL,
`address` text NOT NULL,
`city_id` int(3) DEFAULT NULL,
`tournament_id` int(8) NOT NULL,
`time_created` datetime NOT NULL,
`time_modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `city_id` (`city_id`),
KEY `tournament_id` (`tournament_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The rule for creating participants is each registered email address can only participate in one tournament (foreign key tournament_id); therefore two records with the same email address but different tournament_id should be allowed.
Is is_unique form validation able to do this operation? Or should I create a callback or a helper?
is_unique is just a short name for value_does_not_already_exists_in_a_single_column.
So, no - it won't do the job; you'll need a callback.
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
I have this table structure
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`realname` varchar(50) NOT NULL,
`password` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`bos1` varchar(30) DEFAULT NULL,
`emailbos1` varchar(50) DEFAULT NULL,
`role` varchar(20) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;
I got an issue here :
Two persons (eg: username=steve, and balmer) has bos1 name (eg: bill)
When I login as steve and/or balmer I can see my own detail / data,
but when I login as bill, I want to show steve's and balmer's detail as well.
How do i suppose to do it ?
Thank you.
I have a table environments that has both a createdBy field, as well as an updatedBy field. These fields store a member ID.
There is also a table members which houses the ID and the username. Below are the queries to create the tables
CREATE TABLE `environments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`createdBy` varchar(45) DEFAULT NULL,
`createdDtTm` datetime DEFAULT NULL,
`updatedBy` varchar(45) DEFAULT NULL,
`updatedDtTm` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8$$
CREATE TABLE `members` (
`member_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`firstname` varchar(100) DEFAULT NULL,
`lastname` varchar(100) DEFAULT NULL,
`login` varchar(100) NOT NULL DEFAULT '',
`passwd` varchar(32) NOT NULL DEFAULT '',
PRIMARY KEY (`member_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8$$
I would like to write a query that displays all the environments, but instead of an ID showing, I'd the the user name to show. Since they can be different though, I'm not sure how to write the join statement. Ideas?
You need a double join. SOmething along the lines of
SELECT
environments.*,
creators.firstname AS cfirstname,
creators.lastname AS clastname,
updaters.firstname AS ufirstname,
updaters.lasstname AS ulastname
FROM
environments
INNER JOIN members AS creators ON environments.createdBy=creators.login
LEFT JOIN members AS updaters ON environments.updatedBy=updaters.login
WHERE -- whatever