determine a database relationship - mysql

I need help to decide if the relationship between this 2 table is one to many or many to many. One student can sign up for many class and one class can have multiple student. This is my table below:
Thankyou

This is how it should be:
classes to students is in HABTM Relationship maped by classes_students.
Below is schema:
CREATE TABLE `students` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `classes` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`subject` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `classes_students` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`class_id` INT(11) NOT NULL,
`student_id` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX `FK__classes` (`class_id`),
INDEX `FK__students` (`student_id`),
CONSTRAINT `FK__classes` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`),
CONSTRAINT `FK__students` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`)
)
COMMENT='table to support HABT classes to students'
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;

This is a many-to-many relationship. You need to have Students and Classes into separate tables and connect them via a junction table which will contain a student_id and a class_id.

Related

MySQL Unique Index between three tables

I have the schema of the following tables:
CREATE TABLE IF NOT EXISTS `depts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`deptName` varchar(10) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `deptUniq` (`deptName`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `sub_depts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`dept_id` int(11) NOT NULL,
`subDeptName` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `subDeptNameUniq` (`subDeptName`),
KEY `keyDept` (`dept_id`),
CONSTRAINT `keyDept` FOREIGN KEY (`dept_id`) REFERENCES `depts` (`id`) ON DELETE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `objects` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`sub_depts_id` int(11) NOT NULL,
`objName` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `objNameUniq` (`objName`),
KEY `keySubDept` (`sub_depts_id`),
CONSTRAINT `keySubDept` FOREIGN KEY (`sub_depts_id`) REFERENCES `sub_depts` (`id`) ON DELETE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
relationship between tables sub_depts and depts many-to-one
relationship between tables objects and sub_depts many-to-one
relationship between tables objects and depts must be one-to-one
if the tables objects and depts was one table a simple UNIQUE index (objName, deptName) would work
but need to keep the three tables separate
example data:
depts
1 dept1
2 dept2
sub_depts
1 1 subDept1
2 1 subDept2
3 2 subDept3
objects
1 1 obj1
after execute next line:
INSERT INTO `objects` (`sub_depts_id`, `objName`) VALUES (2, "obj1");
must be the error because not unique data
after execute next line:
INSERT INTO `objects` (`sub_depts_id`, `objName`) VALUES (3, "obj1");
success
Could you help me update the schema of the table, please?
I am using the latest version of MySQL. Thank you for your answers.

Cannot add Foreign Key MySQL :errno 150

I try add foreign key:
CREATE TABLE `invoices` (
`id` int(11) NOT NULL AUTO_INCREMENT,
` name` varchar(255) NOT NULL,
`code_text` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `leads` (
`lead_id` int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`lead_id`)
);
CREATE TABLE `leads_invoices` (
leads_invoice_id int(11) AUTO_INCREMENT NOT NULL,
invoice_id int(11) NOT NULL,
lead_id_i int(11) NOT NULL,
PRIMARY KEY(leads_invoice_id),
FOREIGN KEY (invoice_id) REFERENCES invoices(id),
FOREIGN KEY (lead_id_i) REFERENCES leads(lead_id)
ON UPDATE CASCADE
ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8
But I got error with my lead_id_i key:
Can't create table crm_test.leads_invoices (errno: 150 "Foreign
key constraint is incorrectly formed")
I checked all types and tables, seems they are correct..
How can I solve this problem? Hope you will help me. Thanks!
You have to add ENGINE=InnoDB DEFAULT CHARSET=utf8 to leads table
CREATE TABLE `leads` (
`lead_id` int(11) NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (`lead_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
Because if you want to add a foreign key, both tables must have the same engine

Foreign key cannot be created id MySQL: missing index on column(s)

I have a few foreign keys set up, however phpMyAdmin would not let me to create one more. Here are table in question:
Groups Table
id
name
address
Tasks Table
id
group_id
name
I need a foreign key on group_id in Tasks Table, but when I try to create in it provides the following error: Missing index on column(s). If I add a unique constrain on group_id I am then able to create the foreign key, but the ralation then becomes One to One, which is not the expected result.
Following are table create statements:
CREATE TABLE IF NOT EXISTS `groups` (
`id` int(11) NOT NULL,
`name` varchar(256) NOT NULL,
`address` varchar(256) NOT NULL,
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
ALTER TABLE `groups`
ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `id` (`id`), ADD UNIQUE KEY `id_2` (`id`);
CREATE TABLE IF NOT EXISTS `tasks` (
`id` int(11) NOT NULL,
`group_id` int(11) NOT NULL,
`name` varchar(256) NOT NULL,
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
ALTER TABLE `fixed_tasks`
ADD PRIMARY KEY (`id`), ADD UNIQUE KEY `id` (`id`);
Any help or guidance is much appreciated.
The following code can create two tables with relative foreign keys:
CREATE TABLE `Groups` (
`id` INT NOT NULL,
`name` VARCHAR(45) NULL,
`address` VARCHAR(45) NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `Tasks` (
`id` INT NOT NULL,
`group_id` INT NULL,
`name` VARCHAR(45) NULL,
PRIMARY KEY (`id`),
CONSTRAINT `group_id`
FOREIGN KEY (`group_id`)
REFERENCES `Groups` (`id`)
);

Designing E-commerce database and mysql error

I am trying to make an e-commerce site from scratch. Currently I am trying to make the database.
These are the core tables that the database will have:
Customer: which will have email, username, password....
Customers_Session: Stores information about customer session in hash
Group: basically tells what permissions a customer will have
Category: the category type of a product
Product: information about a product such as name, and description...
Product_Price: Price info on products. This will store the different prices put for each product at various times.
Product_Variation: information about product images, and various colors or styles of a product.
Customer_Orders: What products a customer has ordered.
Customer_Reviews: Reviews made by customers on a product.
Reviewed Products: This table is created based on many to many relationship between the Product table and the Review Table.
Ordered products: This table is created based on the many to many relationship between the Product and the Orders Table.
Based on the above, I have come up with the sql code below:
--
-- Table structure for table `users`
--
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`username` varchar(30) NOT NULL,
`password` varchar(64) NOT NULL,
`first_name` varchar(50) NOT NULL,
`last_name` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
`address` varchar(320) NOT NULL,
`zip` mediumint(5) NOT NULL,
`salt` varchar(40) NOT NULL,
`joined` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`group` tinyint(1) NOT NULL,
`dob` date NOT NULL,
`pic_url` varchar(225) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
--
-- Table structure for table `groups`
--
CREATE TABLE IF NOT EXISTS `groups` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`permissions` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
-- This is for category table
--
CREATE TABLE IF NOT EXISTS `category`(
`category_id` tinyint(3) NOT NULL AUTO_INCREMENT,
`category_type` varchar(100) NOT NULL,
`category_description` varchar(160) NOT NULL,
PRIMARY KEY (`category_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
--
-- This is for product table
--
CREATE TABLE IF NOT EXISTS `products`(
`product_id` int(11) NOT NULL AUTO_INCREMENT,
`product_name` varchar(100) NOT NULL UNIQUE,
`product_description` varchar(160) NOT NULL,
`quantity` int(5) NOT NULL,
`product_code` varchar(4) NOT NULL,
`keywords` varchar(70) NOT NULL,
`category_id` tinyint(3),
PRIMARY KEY (`product_id`),
INDEX (`category_id`),
CONSTRAINT FOREIGN KEY (`category_id`) REFERENCES category(`category_id`) ON DELETE SET NULL
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
--
--
-- Table structure for table `bookings`
--
CREATE TABLE IF NOT EXISTS `bookings` (
`bookings_id` int(11) NOT NULL AUTO_INCREMENT,
`party_type` varchar(50) NOT NULL,
`location` varchar(100) NOT NULL,
`day` varchar(10) NOT NULL,
`time` smallint(6) NOT NULL,
`people_count` smallint(6) NOT NULL,
`booking_name` varchar(50) NOT NULL,
`booking_email` varchar(50) NOT NULL,
PRIMARY KEY (`bookings_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='This is for the bookings' AUTO_INCREMENT=1 ;
--
-- Dumping data for table `groups`
--
INSERT INTO `groups` (`id`, `name`, `permissions`) VALUES
(1, 'Administrator', '{"admin":1}'),
(2, 'Users', '{"users":2}');
--
-- Table Structure for Reviews
--
CREATE TABLE IF NOT EXISTS `customer_reviews`(
`reviews_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11),
`rating` int(5) NOT NULL,
`comment` varchar(160) NOT NULL,
PRIMARY KEY(`reviews_id`),
INDEX (`user_id`),
CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`user_id`) ON DELETE SET NULL
)ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
--
--
-- Table structure for reviewed products This is a many relationshi btw reviews table and product
--
CREATE TABLE IF NOT EXISTS `reviewed_products`(
`product_id` int(11),
`reviews_id`int(11),
PRIMARY KEY(`product_id`,`reviews_id`),
CONSTRAINT FOREIGN KEY(`product_id`) REFERENCES products(`product_id`) ON DELETE SET NULL,
CONSTRAINT FOREIGN KEY(`reviews_id`) REFERENCES customer_reviews(`reviews_id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='This is for many to many cardinality btw reviews and products' AUTO_INCREMENT=1 ;
--
--
-- Table structure for table `orders`
--
CREATE TABLE IF NOT EXISTS `customer_orders` (
`order_id` int(11) NOT NULL AUTO_INCREMENT,
`order_time` int(11) NOT NULL,
`amount` int(5),
`confirmation_number` int(,
`user_id` int(11),
`product_id` int(11),
PRIMARY KEY (`order_id`),
INDEX (`user_id`),
CONSTRAINT FOREIGN KEY (`user_id`) REFERENCES users(`user_id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
/* We need a new table since the customer_order and product is in a many to many relationship */
--
--
-- Table structure for table `curtomer_order_product`
--
CREATE TABLE IF NOT EXISTS `ordered_product`(
`product_id` int(11),
`order_id` int(11),
`quantity` smallint(5),
PRIMARY KEY(`product_id`,`order_id`),
INDEX (`product_id`,`order_id`),
CONSTRAINT FOREIGN KEY (`order_id`) REFERENCES customer_orders(`order_id`) ON DELETE RESTRICT,
CONSTRAINT FOREIGN KEY (`product_id`) REFERENCES products(`product_id`) ON DELETE RESTRICT
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
-- This is for price table
--
CREATE TABLE IF NOT EXISTS `product_price`(
`price_id` int(11) NOT NULL AUTO_INCREMENT,
`price` decimal(6,2) NOT NULL,
`product_id` int(11) NOT NULL,
PRIMARY KEY (`price_id`),
INDEX (`product_id`),
CONSTRAINT FOREIGN KEY (`product_id`) REFERENCES products(`product_id`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
--
-- Table structure for table `product_variations`
--
CREATE TABLE IF NOT EXISTS `product_variations`(
`variations_id` int(11) NOT NULL AUTO_INCREMENT,
`color_name` varchar(10) NOT NULL,
`color_value` char(6) NOT NULL,
`product_id` int(11),
`picture1` varchar(100) NOT NULL,
`picture2` varchar(100) NOT NULL,
`picture3` varchar(100) NOT NULL,
PRIMARY KEY (`variations_id`),
INDEX (`product_id`),
CONSTRAINT FOREIGN KEY (`product_id`) REFERENCES products(`product_id`) ON DELETE RESTRICT
)ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
--
-- Table structure for table `users_session`
--
CREATE TABLE IF NOT EXISTS `users_session` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`hash` varchar(64) NOT NULL,
PRIMARY KEY (`id`),
INDEX (`user_id`),
CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`user_id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
The problem is that when I try to run the above code in PHPmyadmin, I get an error stating "Cannot add foreign key constraint". This starts to happen when creating the customer_Reviews table and any other subsequent tables that require foreign keys.
My questions are:
1. Would you recommend designing the database this way.
2. Why am I getting the "Cannot add foreign key constraint" error?
Thanks.
For a full quality review of your database design, you might try the Code Review sister site. At a glance, I don't see any glaring issues with your database design or your SQL.
The foreign key constraint is failing because you have misnamed the column in question.
CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`user_id`)
should be:
CONSTRAINT FOREIGN KEY(`user_id`) REFERENCES users(`id`)

Lookup table in MySQL

I'm doing my first work in PHP/MySQL & I need help. I have one master table:
CREATE TABLE `m4l_movies` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(250) NOT NULL,
`Rating` int(11) NOT NULL,
`Genre` varchar(250) NOT NULL,
`Actors` varchar(250) NOT NULL,
`UserID` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=115 DEFAULT CHARSET=utf8;
that receives input for a form which has values from a these lookup tables:
CREATE TABLE `m4l_actors` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Actor` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=232 DEFAULT CHARSET=utf8;
CREATE TABLE `m4l_genre` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Genre` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=202 DEFAULT CHARSET=utf8;
CREATE TABLE `m4l_movierating` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Movie_Rating` varchar(250) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=12 DEFAULT CHARSET=utf8;
& I've created a View:
SELECT m4l_movies.ID AS ID,
m4l_movies.Title AS Title,
m4l_movierating.Movie_Rating AS Rating,
m4l_movies.Actors AS Actors,
m4l_movies.Genre AS Genre
FROM m4l_movies
JOIN m4l_movierating ON m4l_movierating.ID = m4l_movies.Rating
INNER JOIN m4l_genre ON m4l_movies.Genre = m4l_genre.ID
INNER JOIN m4l_actors ON m4l_movies.Actors = m4l_actors.ID
ORDER BY m4l_movies.Title
Here is the out put I get:
----------------------------------------------------------------|
ID Title Rating Actor Genre |
10 Summer G (10,15,25) (45,115,123) |
1 About You G-1 (63,163,405) (3,16,51) |
5 Dog Years P (45,65,95) (98,163,357) |
----------------------------------------------------------------|
Firstly this view should return more than 200 records. Secondly, I need to know how to either create a lookup or some other method to convert the NAME & GENRE back to their corresponding text values. Some how the RATING value is doing it right but I can't get NAME or GENRE to do it correctly. I'm sure it has to do with either the way I've joined the tables but I can't figure out where I'm going wrong. Will someone PLEASE help me.
Ok trying to follow along with suggesting made by Phil I've removed actors, genre from movies tale & created movies_genre & movies_actors
DROP TABLE IF EXISTS `m4l_movies`;
CREATE TABLE `m4l_movies` (
`ID` int(11) NOT NULL auto_increment,
`Title` varchar(250) NOT NULL,
`Year` float NOT NULL,
`Review` varchar(250) NOT NULL,
`Rating` int(11) NOT NULL,
`Image` varchar(250) NOT NULL,
`Storyline` longtext NOT NULL,
`Director` varchar(250) NOT NULL,
`UserID` int(11) NOT NULL default '1',
PRIMARY KEY (`ID`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `m4l_movie_actor` (
movie_id INT(11),
actor_id INT(11),
PRIMARY KEY (movie_id, actor_id),
FOREIGN KEY (movie_id) REFERENCES m4l_movies (ID),
FOREIGN KEY (actor_id) REFERENCES m4l_actors (ID)
);
CREATE TABLE `m4l_movie_genre` (
movie_id INT(11),
genre_id INT(11),
PRIMARY KEY (movie_id, genre_id),
FOREIGN KEY (movie_id) REFERENCES m4l_movies (ID),
FOREIGN KEY (genre_id) REFERENCES m4l_genre (ID)
);
DROP TABLE IF EXISTS m4l_genre;
CREATE TABLE m4l_genre (
ID int(11) NOT NULL auto_increment,
Genre varchar(250) NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS m4l_actors;
CREATE TABLE m4l_actors (
ID int(11) NOT NULL auto_increment,
Actor varchar(255) NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
but when I try to create the movie_actor OR movie_genre I get
09:29:45
CREATE TABLE `m4l_movie_actor` (movie_id INT(11), actor_id INT(11),
PRIMARY KEY (movie_id, actor_id),
FOREIGN KEY (movie_id) REFERENCES m4l_movies (ID),
FOREIGN KEY (actor_id) REFERENCES m4l_actors (ID) )
Error Code: 1215. Cannot add foreign key constraint
0.000 sec
09:40:56 CREATE TABLE m4l_movie_genre (movie_id INT(11),genre_id INT(11),
PRIMARY KEY (movie_id, genre_id),
FOREIGN KEY (movie_id) REFERENCES m4l_movies (ID),
FOREIGN KEY (genre_id) REFERENCES m4l_genre (ID) )
Error Code: 1215. Cannot add foreign key constraint
0.016 sec
From what I can gather this only occurs when you have data types mismatch, but I THINK I have all INT data types so why am I getting this error?
You appear to have made the classic blunder of storing relational data in an un-relatable way.
You should be using junction tables instead of comma separated values. For example...
CREATE TABLE `m4l_movies` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(250) NOT NULL,
`Rating` int(11) NOT NULL,
-- removed Actors and Genre
`UserID` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`ID`)
);
CREATE TABLE `m4l_movie_actor` (
movie_id INT(11) NOT NULL,
actor_id INT(11) NOT NULL,
PRIMARY KEY (movie_id, actor_id),
FOREIGN KEY (movie_id) REFERENCES m4l_movies (ID),
FOREIGN KEY (actor_id) REFERENCES m4l_actors (ID)
);
-- repeat for genres
You can then join on the junction table and on to the Actors / Genres tables, eg
SELECT ... m4l_actors.Actor ...
FROM m4l_movies
INNER JOIN m4l_movie_actor ON m4l_movies.ID = m4l_movie_actor.movie_id
INNER JOIN m4l_actors ON m4l_movie_actor.actor_id = m4l_actors.ID
If you want the result in a comma separated list, look into GROUP_CONCAT()