groups and sub-group up to 5 level database structure - mysql

i was wondering what is the best structure for a table in mysql,
The structure needs to have:
Parent (level 0)
-region (inside regions are)(level 1)
-countrys (inside countrys are)(level 2)
-Districts (level 3)
So I need to store all that info but in the same table, any clues??
If you need to know i building this app on cakephp.
Thanks in advance.

I've impulsively made an EER diagram for your situation.
Please tell me if it works (and if you agree), I'll be awaiting your feedback.
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
DROP SCHEMA IF EXISTS `hierarchy` ;
CREATE SCHEMA IF NOT EXISTS `hierarchy` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `hierarchy` ;
-- -----------------------------------------------------
-- Table `hierarchy`.`level`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `hierarchy`.`level` (
`id` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(20) NOT NULL ,
PRIMARY KEY (`id`) ,
UNIQUE INDEX `name_UNIQUE` (`name` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `hierarchy`.`location`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `hierarchy`.`location` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`parent` INT UNSIGNED NULL ,
`name` VARCHAR(50) NOT NULL ,
`level` TINYINT UNSIGNED NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_location_location_idx` (`parent` ASC) ,
INDEX `fk_location_level1_idx` (`level` ASC) ,
INDEX `index_name` (`name` ASC) ,
CONSTRAINT `fk_location_location`
FOREIGN KEY (`parent` )
REFERENCES `hierarchy`.`location` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_location_level1`
FOREIGN KEY (`level` )
REFERENCES `hierarchy`.`level` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
USE `hierarchy` ;
-- -----------------------------------------------------
-- Placeholder table for view `hierarchy`.`details_location`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `hierarchy`.`details_location` (`location_id` INT, `location_name` INT, `parent_id` INT, `level_id` INT, `level_name` INT, `children` INT);
-- -----------------------------------------------------
-- View `hierarchy`.`details_location`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `hierarchy`.`details_location`;
USE `hierarchy`;
CREATE OR REPLACE VIEW `hierarchy`.`details_location` AS
SELECT
x.`id` AS `location_id`,
x.`name` AS `location_name`,
IFNULL(x.`parent`, 0) AS `parent_id`,
y.`id` AS `level_id`,
y.`name` AS `level_name`,
(SELECT COUNT(*) FROM location AS l WHERE l.parent = x.id) AS `children`
FROM location AS `x`
INNER JOIN `level` AS `y`
ON (x.`level` = y.id);
USE `hierarchy`;
DELIMITER $$
USE `hierarchy`$$
CREATE TRIGGER `insert_location` BEFORE INSERT ON location
FOR EACH ROW BEGIN
DECLARE x INT UNSIGNED DEFAULT 1;
IF NEW.parent IS NOT NULL THEN
SELECT `level`+1 INTO x FROM location WHERE id = NEW.parent;
END IF;
SET NEW.`level` = x;
END$$
DELIMITER ;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
-- -----------------------------------------------------
-- Data for table `hierarchy`.`level`
-- -----------------------------------------------------
START TRANSACTION;
USE `hierarchy`;
INSERT INTO `hierarchy`.`level` (`id`, `name`) VALUES (1, 'parent');
INSERT INTO `hierarchy`.`level` (`id`, `name`) VALUES (2, 'region');
INSERT INTO `hierarchy`.`level` (`id`, `name`) VALUES (3, 'country');
INSERT INTO `hierarchy`.`level` (`id`, `name`) VALUES (4, 'district');
COMMIT;
INSERT INTO location (parent,name,level) VALUES
(NULL,'a1',NULL), (NULL,'a2',NULL), (NULL,'a3',NULL);
INSERT INTO location (parent,name,level) VALUES
(1,'b1',NULL), (1,'b2',NULL), (2,'b3',NULL),
(2,'b4',NULL), (3,'b5',NULL), (3,'b6',NULL);
INSERT INTO location (parent,name,level) VALUES
(4,'c1',NULL), (4,'c2',NULL), (5,'c3',NULL),
(5,'c4',NULL), (6,'c5',NULL), (6,'c6',NULL),
(7,'c7',NULL), (7,'c8',NULL), (8,'c9',NULL),
(8,'c10',NULL), (9,'c11',NULL), (9,'c12',NULL);
SELECT * FROM details_location;
[(Mostly) auto-generated code from MySQL Workbench 5.2]
Note that the trigger insert_location determines item's level at registration time, increasing so the parent's level by one unit.

Related

Can I run a SELECT query that depends on two rows from a table without using subqueries

Suppose the schemas are
users(id, name)
users_attributes(user_id, attribute_name, attribute_value)
Sample data:
--
-- Database: `sample_db`
--
CREATE DATABASE IF NOT EXISTS `sample_db` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `sample_db`;
-- --------------------------------------------------------
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`name` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `users`
--
INSERT INTO `users` (`id`, `name`) VALUES
(1, 'Tim'),
(2, 'Joe'),
(3, 'Bob');
-- --------------------------------------------------------
--
-- Table structure for table `users_attributes`
--
CREATE TABLE `users_attributes` (
`user_id` int(11) NOT NULL,
`attribute_name` varchar(50) NOT NULL,
`attribute_value` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `users_attributes`
--
INSERT INTO `users_attributes` (`user_id`, `attribute_name`, `attribute_value`) VALUES
(1, 'height', '10'),
(1, 'over_18', 'yes'),
(2, 'height', '5'),
(3, 'height', '7');
--
-- Indexes for dumped tables
--
--
-- Indexes for table `users`
--
ALTER TABLE `users`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `users_attributes`
--
ALTER TABLE `users_attributes`
ADD UNIQUE KEY `user_id` (`user_id`,`attribute_name`,`attribute_value`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=4;
COMMIT;
Right now I'm calling INNER JOIN on a specific attribute_name so my selection also returns that along with the user id and name. Now I want to exclude from whatever I selected, if they contain a certain other attribute. I know I can accomplish that by using a NOT IN (SELECT ...) conditional but can I do that without having to select again?
EDIT:
Attributes may or may not exist for all users. For example, I would like to fetch id, name, attribute_value where attribute_name = height but only if attribute_name over_18 does not exist or is 'no'

INSERT ON DUPLICATE KEY with INNER JOIN

Given t.id, a.id, t1.name and t2.name, how do I add or update t1_has_t2.data?
I can update it if there is currently a record.
UPDATE t1_has_t2
INNER JOIN t1 ON t1.id=t1_has_t2.t1_id
INNER JOIN t2 ON t2.id=t1_has_t2.t2_id
SET t1_has_t2.data=123
WHERE t1.name="foo" AND t1.t_id=333 AND t2.name="bar" AND t2.t_id=333;
How can I insert it if a record currently doesn't exist?
EDIT. Would it be something like the following? Seems a waste to include t in the JOIN.
INSERT INTO t1_has_t2(t1_id,t2_id,data)
SELECT t1.id, t2.id, 123
FROM t
INNER JOIN t1 ON t1.t_id=t.id
INNER JOIN t2 ON t2.t_id=t.id
WHERE t1.name="foo" AND t1.t_id=333 AND t2.name="bar" AND t2.t_id=333
ON DUPLICATE KEY SET t1_has_t2.data=123;
EDIT2. Ah, maybe I get it now. I just JOIN t1 and t2 to each other through their shared t.id?
INSERT INTO t1_has_t2(t1_id,t2_id,data)
SELECT t1.id, t2.id, 123
FROM t1
INNER JOIN t2 ON t2.t_id=t1.t_id
WHERE t1.name="foo" AND t1.t_id=333 AND t2.name="bar" AND t2.t_id=333
ON DUPLICATE KEY UPDATE t1_has_t2.data=123;
-- MySQL Script generated by MySQL Workbench
-- 08/08/16 07:40:04
-- Model: New Model Version: 1.0
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema mydb
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`accounts`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`accounts` (
`id` INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`t`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t` (
`id` INT NOT NULL AUTO_INCREMENT,
`accounts_id` INT NOT NULL,
PRIMARY KEY (`id`, `accounts_id`),
INDEX `fk_t_accounts_idx` (`accounts_id` ASC),
CONSTRAINT `fk_t_accounts`
FOREIGN KEY (`accounts_id`)
REFERENCES `mydb`.`accounts` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`t1`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t1` (
`id` INT NOT NULL AUTO_INCREMENT,
`t_id` INT NOT NULL,
`t_accounts_id` INT NOT NULL,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
INDEX `fk_t1_t1_idx` (`t_id` ASC, `t_accounts_id` ASC),
UNIQUE INDEX `un1` (`t_id` ASC, `name` ASC),
CONSTRAINT `fk_t1_t1`
FOREIGN KEY (`t_id` , `t_accounts_id`)
REFERENCES `mydb`.`t` (`id` , `accounts_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`t2`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t2` (
`id` INT NOT NULL AUTO_INCREMENT,
`t_id` INT NOT NULL,
`t_accounts_id` INT NOT NULL,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
INDEX `fk_t2_t1_idx` (`t_id` ASC, `t_accounts_id` ASC),
UNIQUE INDEX `un2` (`t_id` ASC, `name` ASC),
CONSTRAINT `fk_t2_t1`
FOREIGN KEY (`t_id` , `t_accounts_id`)
REFERENCES `mydb`.`t` (`id` , `accounts_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`t1_has_t2`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`t1_has_t2` (
`t1_id` INT NOT NULL,
`t2_id` INT NOT NULL,
`data` VARCHAR(45) NULL,
PRIMARY KEY (`t1_id`, `t2_id`),
INDEX `fk_t1_has_t2_t21_idx` (`t2_id` ASC),
INDEX `fk_t1_has_t2_t11_idx` (`t1_id` ASC),
CONSTRAINT `fk_t1_has_t2_t11`
FOREIGN KEY (`t1_id`)
REFERENCES `mydb`.`t1` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_t1_has_t2_t21`
FOREIGN KEY (`t2_id`)
REFERENCES `mydb`.`t2` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
Works fine if I understand what you want with Insert on Duplicate Key Update (IODKU).
Data Load:
insert accounts(id) values (NULL); -- id = 1
insert t(accounts_id) values (1); -- id = 1
insert t1(t_id,t_accounts_id,name) values (1,1,'n1'); -- id=1
insert t1(t_id,t_accounts_id,name) values (1,1,'n2'); -- id=2
insert t2(t_id,t_accounts_id,name) values (1,1,'n1'); -- id=1
insert t2(t_id,t_accounts_id,name) values (1,1,'n2'); -- id=2
insert t1_has_t2(t1_id,t2_id,data) values(1,1,'one_one'); -- success
insert t1_has_t2(t1_id,t2_id,data) values(1,77,'x'); -- Error 1452 as expected
insert t1_has_t2(t1_id,t2_id,data) values(77,1,'x'); -- Error 1452 as expected
insert t1_has_t2(t1_id,t2_id,data) values(1,2,'one_two'); -- success
insert t1_has_t2(t1_id,t2_id,data) values(2,1,'two_one'); -- success
insert t1_has_t2(t1_id,t2_id,data) values(2,2,'two_two'); -- success
Your Query:
UPDATE t1_has_t2
INNER JOIN t1 ON t1.id=t1_has_t2.t1_id
INNER JOIN t2 ON t2.id=t1_has_t2.t2_id
SET t1_has_t2.data='I am a string'
WHERE t1.name="n1" AND t1.t_id=1 AND t2.name="n1" AND t2.t_id=1;
IODKU:
insert t1_has_t2(t1_id,t2_id,data) values(2,2,'two_two_version002')
on duplicate key update data='anchovies';
See results:
select * from t1_has_t2;
+-------+-------+---------------+
| t1_id | t2_id | data |
+-------+-------+---------------+
| 1 | 1 | I am a string |
| 1 | 2 | one_two |
| 2 | 1 | two_one |
| 2 | 2 | anchovies |
+-------+-------+---------------+
You can also look into
insert ignore t1_has_t2(t1_id,t2_id,data) [something];
Which succeeds or fails silently by design.

insert trigger: issue with constraint

I'm trying to create trigger on customer-order database where each customer has several orders and each order has several items.
I'm planning to create a trigger to ensure that
the total number of all orders place by the same customer cannot
exceed 10000
How can create the insert trigger for above constraint.
Here is my SQL file with sample data provided.
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `mydb` ;
CREATE TABLE customers
(`id` int not null auto_increment primary key, `first_name` varchar(64), `last_name`varchar(64) );
INSERT INTO customers(`first_name`, `last_name`)VALUES('Jhon', 'Doe');
CREATE TABLE items
(`id` int not null auto_increment primary key,`item` varchar(64),`price` decimal(19,2));
INSERT INTO items(`item`, `price`)VALUES('Item1', 10.5),('Item2', 25);
CREATE TABLE orders
(`id` int not null auto_increment primary key, `date` date, `customer_id` int,`status` int not null default 1, -- 1 new constraint fk_customer_id foreign key (customer_id) references customers (id));
INSERT INTO orders(`date`, `customer_id`, `status`)VALUES(CURDATE(), 1, 1);
CREATE TABLE order_items(`id` int not null auto_increment primary key,
`order_id` int not null, `item_id` int not null, `quantity` decimal(19,3) not null, `price` decimal(19,3) not null,
constraint fk_order_id foreign key (order_id) references orders (id),
constraint fk_item_id foreign key (item_id) references items (id));
INSERT INTO order_items(`order_id`, `item_id`, `quantity`, `price`)VALUES
(1, 1, 2, 10.5),(1, 2, 4, 25);
;
Although Jahul's answer would technically work, here is alternative logic:
DELIMITER $$
CREATE TRIGGER `customer_orders_check`
BEFORE INSERT ON `orders` FOR EACH ROW
BEGIN
IF ((select count(*)
from `orders`
where a.customer_id = NEW.customer_id
) >= 10000 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Too many orders already';
END IF;
END;
$$
DELIMITER ;
That said, I would suggest an alternative approach. Counting up to 10,000 rows for each insert seems like a lot of work. Instead, keep the counter in the customers table, using an after insert trigger (and perhaps after update/delete as well). Then when inserting a new row, you can just check the count in customers.
This trigger will stop insert --
CREATE TRIGGER `customer_orders_check`
BEFORE INSERT ON `orders` FOR EACH ROW
BEGIN
IF exists(select count(*)
from `orders` a
where a.customer_id= NEW.customer_id
having count(*)>=10000 ) THEN
SET NEW.id = 1 ;
END IF;
END;

Can't create table 'e-learningsystem.chapter' (errno: 121) [duplicate]

I have troubles with forward engineering my MySQL database into WAMP server..
I was going to post an image of the schema but as this is my first post I can't.
Below is the executed script..
use aquaticstar;
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Table `Students`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Students` ;
CREATE TABLE IF NOT EXISTS `Students` (
`id` VARCHAR(10) NOT NULL ,
`studentName` VARCHAR(45) NOT NULL ,
`gender` CHAR NOT NULL ,
`birthDate` DATETIME NOT NULL ,
`mNo` VARCHAR(10) NOT NULL ,
`contactName` VARCHAR(45) NOT NULL ,
`contactEmail` VARCHAR(45) NOT NULL ,
`contactPhone` INT(10) NOT NULL ,
`startDate` DATETIME NOT NULL ,
`remarks` VARCHAR(200) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `Waiting List`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Waiting List` ;
CREATE TABLE IF NOT EXISTS `Waiting List` (
`wait_id` VARCHAR(5) NOT NULL ,
`name` VARCHAR(45) NULL ,
`contactName` VARCHAR(45) NULL ,
`contactPhone` INT(10) NULL ,
`contactEmail` VARCHAR(45) NULL ,
`status` CHAR NULL ,
`remarks` VARCHAR(200) NULL ,
PRIMARY KEY (`wait_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `Schedule`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Schedule` ;
CREATE TABLE IF NOT EXISTS `Schedule` (
`lesson_id` VARCHAR(10) NOT NULL ,
`day` VARCHAR(3) NOT NULL ,
`branch` VARCHAR(30) NOT NULL ,
`level` VARCHAR(30) NOT NULL ,
`time` TIME NOT NULL ,
`ae` VARCHAR(45) NOT NULL ,
PRIMARY KEY (`lesson_id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `Link`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Link` ;
CREATE TABLE IF NOT EXISTS `Link` (
`link_id` VARCHAR(10) NOT NULL ,
`id` VARCHAR(10) NOT NULL ,
`lesson_id` VARCHAR(10) NOT NULL ,
PRIMARY KEY (`link_id`) ,
INDEX `id_idx` (`id` ASC) ,
INDEX `lesson_id_idx` (`lesson_id` ASC) ,
CONSTRAINT `id`
FOREIGN KEY (`id` )
REFERENCES `Students` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `lesson_id`
FOREIGN KEY (`lesson_id` )
REFERENCES `Schedule` (`lesson_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `Attendance`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `Attendance` ;
CREATE TABLE IF NOT EXISTS `Attendance` (
`date` DATETIME NOT NULL ,
`attendance` VARCHAR(5) NOT NULL ,
`link_id` VARCHAR(10) NOT NULL ,
INDEX `link_id_idx` (`link_id` ASC) ,
CONSTRAINT `link_id`
FOREIGN KEY (`link_id` )
REFERENCES `Link` (`link_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
-- -----------------------------------------------------
-- Data for table `Students`
-- -----------------------------------------------------
START TRANSACTION;
INSERT INTO `Students` (`id`, `studentName`, `gender`, `birthDate`, `mNo`, `contactName`, `contactEmail`, `contactPhone`, `startDate`, `remarks`) VALUES ('s001', 'Sam Khew', 'm', '12/12/1991', 'nm', 'May Khew', 'may#gmail.com', 0198829387, '12/07/2011', NULL);
INSERT INTO `Students` (`id`, `studentName`, `gender`, `birthDate`, `mNo`, `contactName`, `contactEmail`, `contactPhone`, `startDate`, `remarks`) VALUES ('s002', 'Joe Biden', 'm', '13/03/2003', 'nm', 'Layla Biden', 'layla#gmail.com', 0199283763, '14/05/2011', NULL);
INSERT INTO `Students` (`id`, `studentName`, `gender`, `birthDate`, `mNo`, `contactName`, `contactEmail`, `contactPhone`, `startDate`, `remarks`) VALUES ('s003', 'Bob Builder', 'm', '14/02/2002', 'LK920K', 'Mama Builder', 'mama#yahoo.com', 0167728376, '29/02/2012', NULL);
INSERT INTO `Students` (`id`, `studentName`, `gender`, `birthDate`, `mNo`, `contactName`, `contactEmail`, `contactPhone`, `startDate`, `remarks`) VALUES ('s004', 'Kenny Koh', 'm', '18/02/1999', 'MM992', 'Lisa Koh', 'lk#hotmail.com', 0123160231, '19/01/2012', NULL);
INSERT INTO `Students` (`id`, `studentName`, `gender`, `birthDate`, `mNo`, `contactName`, `contactEmail`, `contactPhone`, `startDate`, `remarks`) VALUES ('s005', 'Jane Doe', 'f', '29/09/1999', 'nm', 'Jackie Doe', 'jackied#gmail.com', 0127736254, '02/03/2012', NULL);
INSERT INTO `Students` (`id`, `studentName`, `gender`, `birthDate`, `mNo`, `contactName`, `contactEmail`, `contactPhone`, `startDate`, `remarks`) VALUES ('s006', 'Lola Lai', 'f', '02/05/2004', 'nm', 'Mark Lai', 'mark#gmail.com', 0198827365, '11/09/2011', NULL);
COMMIT;
-- -----------------------------------------------------
-- Data for table `Schedule`
-- -----------------------------------------------------
START TRANSACTION;
INSERT INTO `Schedule` (`lesson_id`, `day`, `branch`, `level`, `time`, `ae`) VALUES ('sat1_s4', 'Sat', 'Sunway', 'basic', '4pm', 'Aini');
INSERT INTO `Schedule` (`lesson_id`, `day`, `branch`, `level`, `time`, `ae`) VALUES ('sat1_s5', 'Sat', 'Sunway', 'basic', '5pm', 'Aini');
INSERT INTO `Schedule` (`lesson_id`, `day`, `branch`, `level`, `time`, `ae`) VALUES ('sat1_s6', 'Sat', 'Sunway', 'basic', '6pm', 'Aini');
INSERT INTO `Schedule` (`lesson_id`, `day`, `branch`, `level`, `time`, `ae`) VALUES ('sat2_s4', 'Sat', 'Sunway', 'advance', '4pm', 'Nina');
INSERT INTO `Schedule` (`lesson_id`, `day`, `branch`, `level`, `time`, `ae`) VALUES ('sat2_s5', 'Sat', 'Sunway', 'advance', '5pm', 'Nina');
INSERT INTO `Schedule` (`lesson_id`, `day`, `branch`, `level`, `time`, `ae`) VALUES ('sat3_s6', 'Sat', 'Sunway', 'pre-comp', '6pm', 'Marcus');
COMMIT;
-- -----------------------------------------------------
-- Data for table `Link`
-- -----------------------------------------------------
START TRANSACTION;
INSERT INTO `Link` (`link_id`, `id`, `lesson_id`) VALUES ('L001', 's001', 'sat1_s4');
INSERT INTO `Link` (`link_id`, `id`, `lesson_id`) VALUES ('L002', 's002', 'sat1_s5');
INSERT INTO `Link` (`link_id`, `id`, `lesson_id`) VALUES ('L003', 's003', 'sat1_s6');
INSERT INTO `Link` (`link_id`, `id`, `lesson_id`) VALUES ('L004', 's004', 'sat2_s4');
INSERT INTO `Link` (`link_id`, `id`, `lesson_id`) VALUES ('L005', 's005', 'sat1_s5');
COMMIT;
-- -----------------------------------------------------
-- Data for table `Attendance`
-- -----------------------------------------------------
START TRANSACTION;
INSERT INTO `Attendance` (`date`, `attendance`, `link_id`) VALUES ('26/9/2012', '1', NULL);
COMMIT;
But then I get this error:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'aquaticstar.link' (errno: 121)
I can't figure out why. Can anyone help me?
I searched quickly for you, and it brought me here. I quote:
You will get this message if you're trying to add a constraint with a
name that's already used somewhere else
To check constraints use the following SQL query:
SELECT
constraint_name,
table_name
FROM
information_schema.table_constraints
WHERE
constraint_type = 'FOREIGN KEY'
AND table_schema = DATABASE()
ORDER BY
constraint_name;
Look for more information there, or try to see where the error occurs. Looks like a problem with a foreign key to me.
Foreign Key Constraint Names Have to be Unique Within a Database
Both #Dorvalla’s answer and this blog post mentioned above pointed me into the right direction to fix the problem for myself; quoting from the latter:
If the table you're trying to create includes a foreign key constraint, and you've provided your own name for that constraint, remember that it must be unique within the database.
I wasn’t aware of that. I have changed my foreign key constraint names according to the following schema which appears to be used by Ruby on Rails applications, too:
<TABLE_NAME>_<FOREIGN_KEY_COLUMN_NAME>_fk
For the OP’s table this would be Link_lession_id_fk, for example.
You can login to mysql and type
mysql> SHOW INNODB STATUS\G
You will have all the output and you should have a better idea of what the error is.
I faced this error (errno 121) but it was caused by mysql-created intermediate tables that had been orphaned, preventing me from altering a table even though no such constraint name existed across any of my tables. At some point, my MySQL had crashed or failed to cleanup an intermediate table (table name starting with a #sql-) which ended up presenting me with an error such as: Can't create table '#sql-' (errno 121) when trying to run an ALTER TABLE with certain constraint names.
According to the docs at http://dev.mysql.com/doc/refman/5.7/en/innodb-troubleshooting-datadict.html , you can search for these orphan tables with:
SELECT * FROM INFORMATION_SCHEMA.INNODB_SYS_TABLES WHERE NAME LIKE '%#sql%';
The version I was working with was 5.1, but the above command only works on versions >= 5.6 (manual is incorrect about it working for 5.5 or earlier, because INNODB_SYS_TABLES does not exist in such versions). I was able to find the orphaned temporary table (which did not match the one named in the message) by searching my mysql data directory in command line:
find . -iname '#*'
After discovering the filename, such as #sql-9ad_15.frm, I was able to drop that orphaned table in MySQL:
USE myschema;
DROP TABLE `#mysql50##sql-9ad_15`;
After doing so, I was then able to successfully run my ALTER TABLE.
For completeness, as per the MySQL documentation linked, "the #mysql50# prefix tells MySQL to ignore file name safe encoding introduced in MySQL 5.1."
If you have a foreign key definition in some table and the name of the foreign key is used elsewhere as another foreign key you will have this error.
If you want to fix quickly, Forward Engineer again and check "Generate DROP SCHEMA" option and proceed.
I assume the database doesn't contain data, so dropping it won't affect.
Something I noticed was that I had "other_database" and "Other_Database" in my databases.
That caused this problem as I actually had same reference in other database which caused this mysterious error!
mysql> SHOW ENGINE INNODB STATUS;
But in my case only this way could help:
1. Make backup of current DB
2. Drop DB (not all tables, but DB)
3. Create DB (check that you still have previleges)
4. Restore DB from backup

mySQL that gets entries from another table using Foreign Keys?

Here are my tables:
class
-------
classId
course //example: CMSC101
semester //example: Spring 2013
profId
professor
---------
profId
lname
fname
I want to find all the courses where all the professors with the last name Smith teaches. I essentially want to do this:
SELECT * FROM `professor` WHERE lname=`Smith`
Then, take those results and insert them where the ??? is:
SELECT * FROM `class` WHERE profId=`???`
Is there anyway I can do this with just one query?
I strongly recommend you to use SQL JOINS. You learn once, and use throughout your entire life.
SELECT c.*
FROM class AS c
INNER JOIN professor AS p
ON (c.profId = p.profId)
WHERE p.lname LIKE 'Smith';
Some readings for you:
SQL Joins
Database Normalization
[update]
Small gift for you:
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
DROP SCHEMA IF EXISTS `school` ;
CREATE SCHEMA IF NOT EXISTS `school` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `school` ;
-- -----------------------------------------------------
-- Table `school`.`professor`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `school`.`professor` (
`professorId` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`lname` VARCHAR(40) NOT NULL ,
`fname` VARCHAR(40) NOT NULL ,
PRIMARY KEY (`professorId`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `school`.`semester`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `school`.`semester` (
`semesterId` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(8) NOT NULL ,
PRIMARY KEY (`semesterId`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `school`.`course`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `school`.`course` (
`courseId` TINYINT UNSIGNED NOT NULL AUTO_INCREMENT ,
`code` VARCHAR(10) NOT NULL ,
`name` VARCHAR(40) NOT NULL ,
PRIMARY KEY (`courseId`) ,
UNIQUE INDEX `code_UNIQUE` (`code` ASC) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `school`.`class`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `school`.`class` (
`classId` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`courseId` TINYINT UNSIGNED NOT NULL ,
`semesterId` TINYINT UNSIGNED NOT NULL ,
`profId` INT UNSIGNED NOT NULL ,
PRIMARY KEY (`classId`) ,
INDEX `fk_class_professor_idx` (`profId` ASC) ,
INDEX `fk_class_semester1_idx` (`semesterId` ASC) ,
INDEX `fk_class_course1_idx` (`courseId` ASC) ,
CONSTRAINT `fk_class_professor`
FOREIGN KEY (`profId` )
REFERENCES `school`.`professor` (`professorId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_class_semester1`
FOREIGN KEY (`semesterId` )
REFERENCES `school`.`semester` (`semesterId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_class_course1`
FOREIGN KEY (`courseId` )
REFERENCES `school`.`course` (`courseId` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
USE `school` ;
-- -----------------------------------------------------
-- Placeholder table for view `school`.`class_details`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `school`.`class_details` (`class_id` INT, `course_id` INT, `course_code` INT, `course_name` INT, `semester_id` INT, `semester_name` INT, `prof_id` INT, `prof_fname` INT, `prof_lname` INT);
-- -----------------------------------------------------
-- View `school`.`class_details`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `school`.`class_details`;
USE `school`;
CREATE OR REPLACE VIEW `school`.`class_details` AS
SELECT
w.classId AS `class_id`,
w.courseId AS `course_id`,
x.`code` AS `course_code`,
x.`name` AS `course_name`,
w.semesterId AS `semester_id`,
y.`name` AS `semester_name`,
w.profId AS `prof_id`,
z.fname AS `prof_fname`,
z.lname AS `prof_lname`
FROM class AS w
INNER JOIN course AS x
ON (w.courseId = x.courseId)
INNER JOIN semester AS y
ON (w.semesterId = y.semesterId)
INNER JOIN professor AS z
ON (w.profId = z.professorId);
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
-- -----------------------------------------------------
-- Data for table `school`.`professor`
-- -----------------------------------------------------
START TRANSACTION;
USE `school`;
INSERT INTO `school`.`professor` (`professorId`, `lname`, `fname`) VALUES (1, 'Smith', 'Willard');
INSERT INTO `school`.`professor` (`professorId`, `lname`, `fname`) VALUES (2, 'Burton', 'Tim');
COMMIT;
-- -----------------------------------------------------
-- Data for table `school`.`semester`
-- -----------------------------------------------------
START TRANSACTION;
USE `school`;
INSERT INTO `school`.`semester` (`semesterId`, `name`) VALUES (1, '2011.1');
INSERT INTO `school`.`semester` (`semesterId`, `name`) VALUES (2, '2011.2');
INSERT INTO `school`.`semester` (`semesterId`, `name`) VALUES (3, '2012.1');
INSERT INTO `school`.`semester` (`semesterId`, `name`) VALUES (4, '2012.2');
INSERT INTO `school`.`semester` (`semesterId`, `name`) VALUES (5, '2013.1');
INSERT INTO `school`.`semester` (`semesterId`, `name`) VALUES (6, '2013.2');
INSERT INTO `school`.`semester` (`semesterId`, `name`) VALUES (7, '2014.1');
INSERT INTO `school`.`semester` (`semesterId`, `name`) VALUES (8, '2014.2');
COMMIT;
-- -----------------------------------------------------
-- Data for table `school`.`course`
-- -----------------------------------------------------
START TRANSACTION;
USE `school`;
INSERT INTO `school`.`course` (`courseId`, `code`, `name`) VALUES (1, 'CALC1', 'Calculus 1');
INSERT INTO `school`.`course` (`courseId`, `code`, `name`) VALUES (2, 'CALC2', 'Calculus 2');
INSERT INTO `school`.`course` (`courseId`, `code`, `name`) VALUES (3, 'PHYS1', 'Physics 1');
INSERT INTO `school`.`course` (`courseId`, `code`, `name`) VALUES (4, 'PHYS2', 'Physics 2');
COMMIT;
-- -----------------------------------------------------
-- Data for table `school`.`class`
-- -----------------------------------------------------
START TRANSACTION;
USE `school`;
INSERT INTO `school`.`class` (`classId`, `courseId`, `semesterId`, `profId`) VALUES (1, 1, 3, 1);
INSERT INTO `school`.`class` (`classId`, `courseId`, `semesterId`, `profId`) VALUES (2, 2, 3, 1);
INSERT INTO `school`.`class` (`classId`, `courseId`, `semesterId`, `profId`) VALUES (3, 3, 3, 2);
INSERT INTO `school`.`class` (`classId`, `courseId`, `semesterId`, `profId`) VALUES (4, 4, 3, 2);
INSERT INTO `school`.`class` (`classId`, `courseId`, `semesterId`, `profId`) VALUES (5, 1, 4, 1);
INSERT INTO `school`.`class` (`classId`, `courseId`, `semesterId`, `profId`) VALUES (6, 2, 4, 1);
INSERT INTO `school`.`class` (`classId`, `courseId`, `semesterId`, `profId`) VALUES (7, 3, 4, 2);
INSERT INTO `school`.`class` (`classId`, `courseId`, `semesterId`, `profId`) VALUES (8, 4, 4, 2);
COMMIT;
SELECT * FROM class_details;
SELECT * FROM class_details WHERE prof_lname LIKE 'Smith';
This should work:
SELECT a.* FROM class a, professor b WHERE a.profId = b.profId AND b.lname = 'Smith';
Cheers.