How do i enable EER Model in MYSQL 6.3 workbench ? or How can i create foreign key for my "offers" table? - mysql

These are my tables users and offers
users
name username email password authority enabled
offers
id text
But i want to create a foreign key in my offers table from the column name "username" in my users table. How do i do it with my sql workbench 6.3 ? or how do i enable EER model for my tables ?

These are create statement for the two tables.
offer table
CREATE TABLE `offer` (
`id` int(11) NOT NULL,
`text` varchar(45) DEFAULT NULL,
`username_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `username_id_idx` (`username_id`),
CONSTRAINT `username_id` FOREIGN KEY (`username_id`) REFERENCES `user` (`iduser`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
user table
CREATE TABLE `user` (
`iduser` int(11) NOT NULL,
`username` varchar(45) DEFAULT NULL,
`email` varchar(45) DEFAULT NULL,
`password` varchar(45) DEFAULT NULL,
`authority` varchar(45) DEFAULT NULL,
`enabled` varchar(45) DEFAULT NULL,
PRIMARY KEY (`iduser`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
If you are using Workbench 6.3 you can check the EER diagram by :
Database - > Reverse Engineer - > Select a connection - > Select the scheme you want to see.
Hope this helps!

Related

Should I use Inner Join in this case?

I am working on a student attendance mini-project, and I don't know how to proceed for my database. I'm new to SQL and databases in general so this might seem dumb to you.
So, I want to do a database containing the table student, which contains : student_id (primary key) , name (string) and attendance(boolean) (that's the bare minimum, i'll add more afterwards) and I want to register the daily attendance of the students. So I want to have all the students tied to every date of the week.
I created a date table in phpMyadmin but I don't know how to proceed to link them, i've tried an Inner Join and it was successful.
The problem is : If i want to add another line to the student table my table won't update, so is there a way to "automatically" tie all the students to the date table ?
Sorry if this seems confused I've tried my best to summarize it !
Lets have some idea about tables should be there to implement a proper Student Attendance system in place. I have copied create script for some of my tables that used for maintaining Students record per course. I hope following sample Table scripts with relation will help you understanding regarding table structure and also to solve your issue.
Please be noted, That this table structures for your your reference only. You can add/remove tables/columns as per your requirement once you get an overall idea from this post.
CREATE TABLE `staff` (
`id` int(11) NOT NULL,
`type` varchar(200) DEFAULT NULL,
`first_name` varchar(200) DEFAULT NULL,
`last_name` varchar(200) DEFAULT NULL,
`emal` varchar(200) DEFAULT NULL,
`contact` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `batch` (
`id` int(11) NOT NULL,
`department` varchar(200) DEFAULT NULL,
`details` varchar(200) DEFAULT NULL,
`staff_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `batch_staff_idx` (`staff_id`),
CONSTRAINT `batch_staff` FOREIGN KEY (`staff_id`) REFERENCES `staff` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`batch_id` int(11) DEFAULT NULL,
`first_name` varchar(200) DEFAULT NULL,
`last_name` varchar(200) DEFAULT NULL,
`email` varchar(200) DEFAULT NULL,
`contact` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `batch_student_idx` (`batch_id`),
CONSTRAINT `batch_student` FOREIGN KEY (`batch_id`) REFERENCES `batch` (`batch_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `course` (
`id` int(11) NOT NULL,
`name` varchar(200) DEFAULT NULL,
`details` varchar(200) DEFAULT NULL,
`staff_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `course_staff_idx` (`staff_id`),
CONSTRAINT `course_staff` FOREIGN KEY (`staff_id`) REFERENCES `staff` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `attendence` (
`course_id` int(11) NOT NULL,
`student_id` int(11) DEFAULT NULL,
`class_date` date DEFAULT NULL,
KEY `att_course_idx` (`course_id`),
KEY `att_student_idx` (`student_id`),
CONSTRAINT `att_course` FOREIGN KEY (`course_id`) REFERENCES `course` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `att_student` FOREIGN KEY (`student_id`) REFERENCES `student` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Your required data will finally store into table Attendance. From this table data, you will be able to find list of students absent/present per date and per course. Remember, the attendance table should enrich daily from a automated OR a manual process.

using foreign keys with mysql

I have a forgiven key issue using MySQL. I have a studies table and a samples table. Users can add studies and also add samples. every sample belongs to a study, but studies can have many samples. So theres a one to many relationship with studies and samples. I need to set up a foreign key in the samples table so that whenever a new sample is created it can be referenced by its parent study. I set up a foreign key in the sample table with this script below, but whenever I add a new sample it leaves the study_id column empty. I’m new using foreign keys so any examples would be great. Thanks.
CREATE TABLE `studies` (
`study_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`study_name` varchar(45) DEFAULT NULL,
`description` tinytext,
`reads` varchar(45) DEFAULT NULL,
`created_at` varchar(45) DEFAULT NULL,
PRIMARY KEY (`study_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
CREATE TABLE `samples` (
`sample_id` int(11) NOT NULL AUTO_INCREMENT,
`study_id` int(11) DEFAULT NULL,
`sample_name` varchar(45) DEFAULT NULL,
`created_at` varchar(45) DEFAULT NULL,
PRIMARY KEY (`sample_id`),
FOREIGN KEY (`study_id`)
REFERENCES `studies` (`study_id`)
ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

Unable to create foreign key constraint in MySQL. Error number 150

I am attempting to create a data model. It's fairly complicated, but I'm trying to link the Splits table to the Trials table. A screenshot of the model is below:
I'm attempting to make Splits.protocol + Splits.resultID + Splits.trialNumber a foreign key to Trials. Those three relations are the primary key of Trials. I'm doing this with MySQL Workbench and it throws an Error #150. Does anyone know what the problem is?
Here is the SQL statement and the error that it throws when attempting to execute it:
ERROR 1005: Can't create table '403898_BAMNormalized.#sql-7285_6c29081' (errno: 150)
SQL Statement:
ALTER TABLE `403898_BAMNormalized`.`Splits`
ADD CONSTRAINT `FK_FromTrial`
FOREIGN KEY (`protocol` , `resultID` , `trialNumber`)
REFERENCES `403898_BAMNormalized`.`Trials` (`protocol` , `resultID` , `trialNumber`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
ERROR: Error when running failback script. Details follow.
ERROR 1050: Table 'Splits' already exists
SQL Statement:
CREATE TABLE `Splits` (
`protocol` varchar(255) NOT NULL,
`resultID` int(11) NOT NULL,
`trialNumber` int(11) NOT NULL,
`splitNumber` int(11) NOT NULL,
`splitScore` decimal(10,0) NOT NULL,
PRIMARY KEY (`protocol`,`resultID`,`trialNumber`,`splitNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
Here are the create table statements:
CREATE TABLE `Trials` (
`resultID` int(11) NOT NULL,
`protocol` varchar(255) NOT NULL,
`trialNumber` int(11) NOT NULL,
`trialScore` decimal(10,0) NOT NULL,
`best` char(1) DEFAULT NULL,
`DQFlag` varchar(45) DEFAULT NULL,
PRIMARY KEY (`resultID`,`protocol`,`trialNumber`),
CONSTRAINT `FK_trialID` FOREIGN KEY (`resultID`, `protocol`) REFERENCES `ResultsDetails` (`resultID`, `protocol`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `Splits` (
`protocol` varchar(255) NOT NULL,
`resultID` int(11) NOT NULL,
`trialNumber` int(11) NOT NULL,
`splitNumber` int(11) NOT NULL,
`splitScore` decimal(10,0) NOT NULL,
PRIMARY KEY (`protocol`,`resultID`,`trialNumber`,`splitNumber`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Indexes of Trials table:
Reordering the index to represent the same order as the foreign key was the solution. See comments appended to original post for detailed information.

Creating composite primarykey using a foreignkey and a non key attribute

This is my auto generated code after creating the batch table. while inserting data to this table
BatchID=1,Course_CourseID=1
BatchID=1,Course_CourseID=2
it is creating an error saying "Duplicate entry '1' for key 'BatchID_UNIQUE'".
I'm using C# 2010 express windows application as well as MySQl 5.1
My table schema is here
CREATE TABLE `batch` (
`BatchID` int(11) NOT NULL,
`Course_CourseID` int(11) NOT NULL,
`NoOfStudents` int(11) DEFAULT NULL,
`ClassRoom` varchar(45) DEFAULT NULL,
`StartDate` varchar(45) DEFAULT NULL,
`Day` varchar(45) DEFAULT NULL,
`Time` varchar(45) DEFAULT NULL,
PRIMARY KEY (`BatchID`,`Course_CourseID`),
UNIQUE KEY `BatchID_UNIQUE` (`BatchID`),
KEY `fk_Batch_Course1` (`Course_CourseID`),
CONSTRAINT `fk_Batch_Course1` FOREIGN KEY (`Course_CourseID`)
REFERENCES `course` (`CourseID`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Well, the error message quite clearly refers to this string:
UNIQUE KEY `BatchID_UNIQUE` (`BatchID`)
So what you have to do is either drop this index (with...
ALTER TABLE `batch` DROP INDEX `BatchID_UNIQUE`
... command, or just exclude this line from the table's definition (in CREATE TABLE).
All that said assuming that you really don't need your batch ids to be unique (in other words, there's no logical error in your INSERT statement. That seems to be the case, though: pair BatchID-Course_CourseID is already defined as unique (via PRIMARY KEY).
Try it this way. Drop your batch table and then run this sql. Rightly answered that you can not have two same vaues for a unique key. So I removed the unique key line as well.
CREATE TABLE IF NOT EXISTS `batch` (
`BatchID` int(11) NOT NULL,
`Course_CourseID` int(11) NOT NULL,
`NoOfStudents` int(11) DEFAULT NULL,
`ClassRoom` varchar(45) DEFAULT NULL,
`StartDate` varchar(45) DEFAULT NULL,
`Day` varchar(45) DEFAULT NULL,
`Time` varchar(45) DEFAULT NULL,
PRIMARY KEY (`BatchID`,`Course_CourseID`),
KEY `Course_CourseID` (`Course_CourseID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
ALTER TABLE `batch` ADD CONSTRAINT `batch_ibfk_2` FOREIGN KEY (`Course_CourseID`)
REFERENCES `course` (`CourseId`) ON DELETE NO ACTION ON UPDATE NO ACTION;

Mysql Table creation from Mysql workbench to phpmyadmin

Hi i had the following table structure on mysql workbench for a table example_1
But because of some reasons i want to recreate all the tables in phpmyadmin with the create table sysntax like below
Create table code in MYSQL workbench:
CREATE TABLE `example_1` (
`job_id` int(11) NOT NULL AUTO_INCREMENT,
`source_id` int(11) DEFAULT NULL,
`publication_id` int(11) DEFAULT NULL,
`facility_id` int(11) DEFAULT NULL,
`job_type_id` int(11) NOT NULL,
`template_id` int(11) DEFAULT NULL,
`account_num` varchar(9) DEFAULT NULL,
`start_dt` datetime DEFAULT NULL,
`end_dt` datetime DEFAULT NULL,
`pdf_path` varchar(20) DEFAULT NULL,
`admin_user_id` int(11) NOT NULL,
`skills` varchar(255) DEFAULT NULL,
`tracking_pixel` varchar(255) DEFAULT NULL,
`auto_renew` smallint(6) DEFAULT NULL,
PRIMARY KEY (`job_id`),
KEY `source_id_idxfk` (`source_id`),
KEY `facility_id_idxfk` (`facility_id`),
KEY `job_type_id_idxfk` (`job_type_id`),
KEY `template_id_idxfk` (`template_id`),
CONSTRAINT `jp_job_ibfk_1` FOREIGN KEY (`source_id`) REFERENCES `jp_source` (`source_id`),
CONSTRAINT `jp_job_ibfk_2` FOREIGN KEY (`facility_id`) REFERENCES `adm_facility` (`facility_id`),
CONSTRAINT `jp_job_ibfk_3` FOREIGN KEY (`job_type_id`) REFERENCES `jp_job_type` (`job_type_id`),
CONSTRAINT `jp_job_ibfk_4` FOREIGN KEY (`template_id`) REFERENCES `jp_template` (`template_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
I had copied and pasted the same in phpmyadmin to create the same table, then i got the following error
Error:
InnoDB
Supports transactions, row-level locking, and foreign keys
[ Variables | Buffer Pool | InnoDB Status ]
Can anyone please let me know how to edit the code in order to avoid errors and create a table in phpmyadmin sql editor
It seems that INNODB Engine is disabled on your server. Just remove any disable options for innodb in /etc/my.cnf and restart mysql.