MySQL getting valid addresses within radius - mysql

I am looking for a query that is able to take into account the radius limit set by restaurants and only return valid restaurants to the user. This is a little bit more complicated than a typical scenario as the radius distance is usually a static value.
This is the restaurant table that has the radius limits:
CREATE TABLE IF NOT EXISTS grabatakeaway.restaurant (
`restaurant_id` int(8) UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` varchar(128) NOT NULL,
`delivery_radius` int(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
The address table stores the long and lat of the addresses:
CREATE TABLE IF NOT EXISTS grabatakeaway.address (
`address_id` int(8) UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
`address` text NOT NULL,
`city` varchar(128) NOT NULL,
`state_province` varchar(128),
`zip_post` varchar(32) NOT NULL,
`latitude` float(16, 12) NOT NULL,
`longitude` float(16, 12) NOT NULL,
FOREIGN KEY (country_id) REFERENCES grabatakeaway.country(country_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
These tables allow a user or a restaurant to be assigned to an address
CREATE TABLE IF NOT EXISTS grabatakeaway.user_address (
`user_address_id` int(16) UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
`user_id` int(16) UNSIGNED NOT NULL,
`address_id` int(8) UNSIGNED NOT NULL,
`primary_address` tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
FOREIGN KEY (user_id) REFERENCES grabatakeaway.user(user_id),
FOREIGN KEY (address_id) REFERENCES grabatakeaway.address(address_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE IF NOT EXISTS grabatakeaway.restaurant_address (
`restaurant_address_id` int(16) UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
`restaurant_id` int(8) UNSIGNED NOT NULL,
`address_id` int(8) UNSIGNED NOT NULL,
FOREIGN KEY (restaurant_id) REFERENCES grabatakeaway.restaurant(restaurant_id),
FOREIGN KEY (address_id) REFERENCES grabatakeaway.address(address_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Anyone have any ideas on how I would approach this problem?

Related

Foreign key referring to primary key in the same table Mysql

# Sql to create userdetails table:
CREATE TABLE `userdetails` (
`user_details_id` int(3) unsigned NOT NULL AUTO_INCREMENT,
`user_group_id` int(1) unsigned NOT NULL,
`name` varchar(50) NOT NULL,
`email_id` varchar(50),
`password` varchar(50) NOT NULL,
`mobile_no` varchar(10) NOT NULL,
`company_id` int(3) unsigned,
`vehicle_id` varchar(10),
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`date_updated` datetime NOT NULL,
`created_by` int(3) unsigned NOT NULL,
`status` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`user_details_id`),
UNIQUE KEY `email_id` (`email_id`),
FOREIGN KEY (`user_group_id`) REFERENCES `usergroups` (`user_group_id`),
FOREIGN KEY (`company_id`) REFERENCES `companies` (`company_id`),
FOREIGN KEY (`created_by`) REFERENCES `userdetails` (`user_details_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Focusing on the 'user_details_id' and 'created_by' columns, the conflict arises when created_by is not referring to an existing user_details_id.
Meaning, if I am creating my own profile with the required details, my user_details_id has not been generated yet and hence I do not know what value to input into the created_by field (which is ideally supposed to contain my user_details_id).
I would appreciate if anyone can guide me in the right direction on how to approach such a conflict, and if there is a way to determine what the value of the user_details_id field could be before it's even generated.
Thanks.
You can't handle it by temporarily disabling foreign key checks by setting server variable foreign_key_checks
SET FOREIGN_KEY_CHECKS=0;
INSERT INTO cities userdetails(....);
SET FOREIGN_KEY_CHECKS = 1;
# Sql to create userdetails table:
CREATE TABLE `userdetails` (
`user_details_id` int(3) unsigned NOT NULL AUTO_INCREMENT,
`user_group_id` int(1) unsigned NOT NULL,
`name` varchar(50) NOT NULL,
`email_id` varchar(50),
`password` varchar(50) NOT NULL,
`mobile_no` varchar(10) NOT NULL,
`company_id` int(3) unsigned,
`vehicle_id` varchar(10),
`date_created` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`date_updated` datetime NOT NULL,
`created_by` int(3) unsigned,
`status` enum('0','1') NOT NULL DEFAULT '0',
PRIMARY KEY (`user_details_id`),
UNIQUE KEY `email_id` (`email_id`),
FOREIGN KEY (`user_group_id`) REFERENCES `usergroups` (`user_group_id`),
FOREIGN KEY (`company_id`) REFERENCES `companies` (`company_id`),
FOREIGN KEY (`created_by`) REFERENCES `userdetails` (`user_details_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Allowing created_by to accept NULL values seems to be a quick fix to this problem as that can imply user is self-created.

Cannot add foreign key constraint in MySQL with FOREIGN KEY

I just created this table:
CREATE TABLE `t_application` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`application_desc` varchar(255) DEFAULT NULL,
`application_key` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_cotl49evfo7w4plf6213uaruc` (`application_key`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Then I want to create this one:
CREATE TABLE `t_device` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`device_key` varchar(50) DEFAULT NULL,
`device_type` varchar(50) DEFAULT NULL,
`application_id` int(11) unsigned NOT NULL,
`device_desc` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `application_id` (`application_id`),
CONSTRAINT `t_device_app` FOREIGN KEY (`application_id`) REFERENCES `t_application` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
but its not possible because I got this error:
Cannot add foreign key constraint
The FK column has to have the same type as PK in referenced table:
Using FOREIGN KEY Constraints
Corresponding columns in the foreign key and the referenced key must
have similar data types. The size and sign of integer types must be
the same. The length of string types need not be the same. For
nonbinary (character) string columns, the character set and collation
must be the same.
You have: int(11) <> int(11) unsigned
CREATE TABLE `t_application` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`application_desc` varchar(255) DEFAULT NULL,
`application_key` varchar(50) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UK_cotl49evfo7w4plf6213uaruc` (`application_key`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `t_device` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`device_key` varchar(50) DEFAULT NULL,
`device_type` varchar(50) DEFAULT NULL,
`application_id` int(11) unsigned NOT NULL,
`device_desc` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `application_id` (`application_id`),
CONSTRAINT `t_device_app` FOREIGN KEY (`application_id`)
REFERENCES `t_application` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
SqlFiddleDemo
So you can change: t_application.id to int(11) unsigned
or t_device.application_id to int(11)

mysql add foreign key constraint error

I have two tables which I'd like to connect with foreign key constraint
For some reason, when I try to do it, it fails and and says: #1215 - Cannot add foreign key constraint
Here is my first table:
CREATE TABLE `zmaneyhayom` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`zman_id` varchar(255) NOT NULL,
`tempHour` tinyint(1) NOT NULL,
`tempHourType` varchar(255) NOT NULL,
`tempHourNum` double NOT NULL,
`tempMinutes` tinyint(1) NOT NULL,
`tempMinutesType` varchar(255) NOT NULL,
`tempMinutesNum` double NOT NULL,
`regularMinutes` tinyint(1) NOT NULL,
`regularMinutesNum` double NOT NULL,
`equivalentMinutes` tinyint(1) NOT NULL,
`equivalentMinutesNum` double NOT NULL,
`degreesBelowHorizon` tinyint(1) NOT NULL,
`degreesBelowHorizonNum` double NOT NULL,
`beforeAfter` varchar(6) NOT NULL,
`riseSet` varchar(4) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `zman_id_2` (`zman_id`),
KEY `zman_id` (`zman_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
And this table holds ID and name, which eventually the ID in this table is the name for the previous table (zman_id column):
CREATE TABLE `zmaneyhayomlabels` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
This is the code I'm attempting in order to create the constraint:
ALTER TABLE `zmaneyhayom` ADD FOREIGN KEY ( `zman_id` ) REFERENCES `luah_v2`.`zmaneyhayomlabels` (
`id`
) ON DELETE NO ACTION ON UPDATE NO ACTION ;
I have no idea why it's failing :/
What I want is that whenever I go on phpmyadmin and go to the first table, instead of typing some id in zman_id I will have a select box which I can select a name (which is stored in the second table) but the value it will store will be the ID.
they are not same type id is INT and zman_id is varchar.
you can change this
`zman_id` varchar(255) NOT NULL,
to
`zman_id` int(11) NOT NULL,

mysql table creation Error Code: 1005 in this specific case

I have tried everything in solving this issue and yes I know that this type of question is already asked here but I could not solve my issue
It is mysql database
Error Code: 1005
Can't create table '.\project\comments.frm' (errno: 150)
the foreign keys are matching in structure (i.e length and type) then what can be the possible problem in the table creation
Table which is giving error is comments:
CREATE TABLE `comments`(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`post_id` INT(10) UNSIGNED NOT NULL,
FOREIGN KEY (`post_id`) REFERENCES `posts`.`id`,
FOREIGN KEY (`user_id`) REFERENCES `users`.`id`,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;`
Here is posts table which is already created in the databas
CREATE TABLE `posts` (
`id` int(10) unsigned NOT NULL auto_increment,
`title` varchar(30) default NULL,
`description` longtext,
`image` varchar(50) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Here is the users table which is also already created in the database
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL auto_increment,
`user_name` varchar(33) default NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) default NULL,
`type` varchar(255) NOT NULL,
`registrationDate` date NOT NULL,
PRIMARY KEY (`id`,`email`,`type`)
)
Your syntax is incorrect. The REFERENCES keyword should be followed by table (columns):
CREATE TABLE `comments`(
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`description` VARCHAR(100) NOT NULL,
`user_id` INT(10) UNSIGNED NOT NULL,
`post_id` INT(10) UNSIGNED NOT NULL,
FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
FOREIGN KEY (`user_id`) REFERENCES `users` (`id`),
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1;

DB Structure : Ok or not?

I am developing an application management business (sales, suppliers, customers, products, ...) for a new company. To begin, I need to create a database. Could you please tell me if the BD scheme bellow is good and optimized ?
CREATE TABLE IF NOT EXISTS `company` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
`nom` varchar(50) NOT NULL,
`description` varchar(500) NOT NULL,
`enable` ENUM('YES', 'NO') DEFAULT 'YES',
`level` int(1) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY SIRET (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
// contactType can be one of the 3 values : email, phone, fax
CREATE TABLE IF NOT EXISTS `contactType` (
id UNSIGNED INT NOT NULL auto_increment,
`contactType` ENUM('email', 'phonenumber', 'faxnumber')
`mobile` ENUM('YES', 'NO') default 'NO',
PRIMARY KEY (id),
UNIQUE KEY type (contactType)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `contacts` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
`contactType` varchar(50) NOT NULL, // A reference to contactType just above
`contactref` varchar(50) NOT NULL, // Phone number, fax number or email adress
PRIMARY KEY (id),
UNIQUE KEY type (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `customers` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `supplier` (
id UNSIGNED INT NOT NULL auto_increment,
`SIRET` varchar(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (SIRET)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `industry` (
id UNSIGNED INT NOT NULL auto_increment,
`industry` varchar(250) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (activite)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
CREATE TABLE IF NOT EXISTS `entreprisePerIndustry` (
id UNSIGNED INT NOT NULL auto_increment,
`industry_id` varchar(250) NOT NULL, // Chemical, Computer, Consulting, ...
FOREIGN KEY (industry_id) REFERENCES industry(id) ON DELETE CASCADE,
`SIRET` varchar(50) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY type (industry_id)
) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Below are few TIPS which can helps you to create better DB
Change table engine from MyISAM to InnoDB if you want make foreign key constrains to work.
AUTO INCREMENT DataType should be UNSIGNED INT. this will double the range.
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT`
if a column value chosen from a list of permitted values then change dataType to ENUM .In your case enable, level can be turned into ENUM dataType
`enable` ENUM( 'y', 'n' ) NOT NULL DEFAULT 'y' COMMENT 'y:yes; n:no' `
add foreign key relation to
contacts.contactType with contactType.id
entreprisePerIndustry .industry with industry.id
update
I have created basic and optimized table structure ( AFAIK ).
--
-- Table structure for table `tbl_company`
--
CREATE TABLE IF NOT EXISTS `tbl_company` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`siret` varchar(50) NOT NULL,
`nom` int(11) NOT NULL,
`description` varchar(250) NOT NULL,
`enable` enum('y','n') NOT NULL DEFAULT 'y' COMMENT 'y:yes; n:no',
`level` enum('1','2') NOT NULL DEFAULT '1',
`last_updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- Table structure for table `tbl_contact`
--
CREATE TABLE IF NOT EXISTS `tbl_contact` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`siret` varchar(50) NOT NULL,
`contact_type` enum('email','phone','fax') NOT NULL DEFAULT 'email',
`contact_ref` varchar(100) NOT NULL COMMENT 'Phone number, fax number or email adress',
`last_updated_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `siret` (`siret`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
...
...
complate structure is here