I am a self taught CS and I am really novice at mySQL. I created a table called "jobs". I would like to create a new table keywords with 3 columns:
keyword_id as a primary key
job_id as a foreign key from the jobs table
keyword, text
This is the query I wrote:
CREATE TABLE `keywords` (
`keyword_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY(`keyword_id`),
`keyword` text NOT NULL,
FOREIGN KEY (job_id) REFERENCES jobs(job_id)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
I got this error message:
Key column 'job_id' doesn't exist in table
The current jobs table code is the following:
CREATE TABLE `jobs` (
`title` text NOT NULL,
`type` text NOT NULL,
`location` text NOT NULL,
`salary` int(11) NOT NULL,
`description` text NOT NULL,
`date` date NOT NULL,
`job_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
You need to have a column in the keywords table to hold the foreign key.
Like this
CREATE TABLE `jobs` (
`title` text NOT NULL,
`type` text NOT NULL,
`location` text NOT NULL,
`salary` int(11) NOT NULL,
`description` text NOT NULL,
`date` date NOT NULL,
`job_id` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
CREATE TABLE `keywords` (
`keyword_id` int(11) NOT NULL AUTO_INCREMENT,
`keyword` text NOT NULL,
`job_id` int(11) NOT NULL, #<- new column
PRIMARY KEY(`keyword_id`),
FOREIGN KEY (job_id) REFERENCES jobs(job_id)
) ENGINE=InnoDB AUTO_INCREMENT=28 DEFAULT CHARSET=latin1;
Related
I have a mysql table whose description is given below
Create Table
CREATE TABLE `question` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`category` varchar(255) DEFAULT NULL,
`is_deleted` bit(1) NOT NULL,
`question` varchar(255) DEFAULT NULL,
`version` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8
I have another table
Create Table
CREATE TABLE `parent_question` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`is_deleted` bit(1) NOT NULL,
`version` int(11) DEFAULT NULL,
`pid` bigint(20) NOT NULL,
`qid` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK8FEA83DBE860AF9` (`pid`),
KEY `FK8FEA83DBF34C20F6` (`qid`),
CONSTRAINT `FK8FEA83DBE860AF9` FOREIGN KEY (`pid`) REFERENCES `parent` (`id`),
CONSTRAINT `FK8FEA83DBF34C20F6` FOREIGN KEY (`qid`) REFERENCES `question` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8
I have the right to insert one row in the question table.So when I add a new row then id will be generated (because it is auto_increment) and I want to store this id in the qid field parent_question table.
Can anybody please tell me how to do it?
Use the last_insert_id() function:
insert into question values (...);
insert into parent_question values (..., last_insert_id(), ...);
I want to make 3 Tables like this :
wc_groups Table
CREATE TABLE IF NOT EXISTS `wc_groups` (
`id` int(2) unsigned NOT NULL AUTO_INCREMENT,
`idgroup` int(7) NOT NULL,
`title` varchar(10) NOT NULL,
`content` text,
`status` smallint(1) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`),
UNIQUE KEY `idgroup` (`idgroup`),
KEY `status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
wc_matches Table
CREATE TABLE IF NOT EXISTS `wc_matches` (
`id` int(4) unsigned NOT NULL AUTO_INCREMENT,
`time` date NOT NULL,
`group_id` int(2) unsigned NOT NULL,
`place` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`team_id_1` int(10) unsigned NOT NULL,
`team_id_2` int(10) unsigned NOT NULL,
`edituser` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
KEY `group_id_foreign` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
wc_teams Table
CREATE TABLE IF NOT EXISTS `wc_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`group_id` int(2) unsigned NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
ALTER TABLE `wc_teams`
ADD CONSTRAINT `group_id_foreign` FOREIGN KEY (`group_id`) REFERENCES `wc_groups` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Why i got an error when execution Code to create table wc_team ?
What's possible to use What Is Possible Use One Reference Field ( wc_groups (id) ) To Multiple Foreign Key Constraint ?
I don't understand what you mean with multiple foreign keys.
But the problem in your wc_teams create query is that you have a lost , behind
`group_id` int(2) unsigned NOT NULL,
But you also miss your primary key so i guess you need this
CREATE TABLE IF NOT EXISTS `wc_teams` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`group_id` int(2) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
I have the following database schema that has a bunch of tables and foreign keys, when i try to import the sql dump i keep getting the following errors.
Can't create table errno 150
I understand that it is trying to create tables with dependencies of tables that are not created yet but i don't understand how to import the schema without butchering out all of the foreign keys and then re-creating them per the answers given on Stack and google.
There has to be an easier way, what do big companies do that have hundreds of tables?
I have the sql statements below and any suggestions would be appreciated. Thanks
#
# Encoding: Unicode (UTF-8)
#
DROP TABLE IF EXISTS `contact_interest`;
DROP TABLE IF EXISTS `contact_seeking`;
DROP TABLE IF EXISTS `interests`;
DROP TABLE IF EXISTS `job_current`;
DROP TABLE IF EXISTS `job_desired`;
DROP TABLE IF EXISTS `job_listings`;
DROP TABLE IF EXISTS `my_contacts`;
DROP TABLE IF EXISTS `profession`;
DROP TABLE IF EXISTS `seeking`;
DROP TABLE IF EXISTS `status`;
DROP TABLE IF EXISTS `zip_code`;
CREATE TABLE `contact_interest` (
`contact_id` int(10) unsigned NOT NULL,
`interest_id` int(10) unsigned NOT NULL,
KEY `mycontacts_contactinterest_fk` (`contact_id`),
KEY `interests_contactinterest_fk` (`interest_id`),
CONSTRAINT `mycontacts_contactinterest_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`),
CONSTRAINT `interests_contactinterest_fk` FOREIGN KEY (`interest_id`) REFERENCES `interests` (`interest_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `contact_seeking` (
`contact_id` int(10) unsigned NOT NULL,
`seeking_id` int(10) unsigned NOT NULL,
KEY `contactid_contactseeking_fk` (`contact_id`),
KEY `seeking_contactseeking_fk` (`seeking_id`),
CONSTRAINT `contactid_contactseeking_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`),
CONSTRAINT `seeking_contactseeking_fk` FOREIGN KEY (`seeking_id`) REFERENCES `seeking` (`seeking_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `interests` (
`interest_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`interest` varchar(50) DEFAULT NULL,
PRIMARY KEY (`interest_id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=latin1;
CREATE TABLE `job_current` (
`contact_id` int(10) unsigned NOT NULL,
`title` varchar(20) DEFAULT NULL,
`salary` decimal(8,2) DEFAULT NULL,
`start_date` date DEFAULT NULL,
KEY `mycontacts_jobcurrent_fk` (`contact_id`),
CONSTRAINT `mycontacts_jobcurrent_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `job_desired` (
`contact_id` int(10) unsigned NOT NULL,
`title` varchar(20) DEFAULT NULL,
`salary_low` decimal(8,2) DEFAULT NULL,
`salary_high` decimal(8,2) DEFAULT NULL,
`available` date DEFAULT NULL,
`years_exp` int(11) DEFAULT NULL,
KEY `mycontacts_jobdesired_fk` (`contact_id`),
CONSTRAINT `mycontacts_jobdesired_fk` FOREIGN KEY (`contact_id`) REFERENCES `my_contacts` (`contact_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `job_listings` (
`job_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(25) DEFAULT NULL,
`salary` decimal(8,2) DEFAULT NULL,
`zip_code` char(5) DEFAULT NULL,
`description` varchar(50) DEFAULT NULL,
PRIMARY KEY (`job_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
CREATE TABLE `my_contacts` (
`contact_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`last_name` varchar(30) DEFAULT NULL,
`first_name` varchar(20) DEFAULT NULL,
`phone` char(10) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`gender` char(1) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`prof_id` int(11) unsigned NOT NULL,
`status_id` int(10) unsigned NOT NULL,
`zip_code` char(5) DEFAULT NULL,
PRIMARY KEY (`contact_id`),
KEY `profession_mycontacts_fk` (`prof_id`),
KEY `zipcode_mycontacts_fk` (`zip_code`),
KEY `status_my_contacts_fk` (`status_id`),
CONSTRAINT `profession_mycontacts_fk` FOREIGN KEY (`prof_id`) REFERENCES `profession` (`prof_id`),
CONSTRAINT `status_my_contacts_fk` FOREIGN KEY (`status_id`) REFERENCES `status` (`status_id`),
CONSTRAINT `zipcode_mycontacts_fk` FOREIGN KEY (`zip_code`) REFERENCES `zip_code` (`zip_code`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;
CREATE TABLE `profession` (
`prof_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`profession` varchar(30) DEFAULT NULL,
PRIMARY KEY (`prof_id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=latin1;
CREATE TABLE `seeking` (
`seeking_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`seeking` varchar(40) DEFAULT NULL,
PRIMARY KEY (`seeking_id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=latin1;
CREATE TABLE `status` (
`status_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`status` varchar(30) DEFAULT NULL,
PRIMARY KEY (`status_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;
CREATE TABLE `zip_code` (
`zip_code` char(5) NOT NULL DEFAULT '',
`city` varchar(20) DEFAULT NULL,
`state` char(2) DEFAULT NULL,
PRIMARY KEY (`zip_code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I found that I only needed two lines to fix my issue, I added one 0 at the top and 1 at the bottom and I was good. Sorry to waste your time...
SET FOREIGN_KEY_CHECKS = 0;
SET FOREIGN_KEY_CHECKS = 1;
the easiest way would be to do it via commandline like this:
mysql db_name < backup-file.sql
this executes your sql file in one transaction. If you execute your stuff in one transaction then you won't get the foreign key errors.
I am trying to use this query in a Filemaker -> MySQL transition script. The table creation steps are taken directly from a phpMyAdmin export, and I added in the DROP statements.
DROP TABLE IF EXISTS artifacts;
CREATE TABLE IF NOT EXISTS `artifacts`
(
`aid` INT(11) UNSIGNED NOT NULL auto_increment,
`accession number` TEXT NOT NULL,
`name` TEXT NOT NULL,
`period 1` TEXT NOT NULL,
`period 3 date` TEXT NOT NULL,
`visual description` TEXT NOT NULL,
`religion 1` TEXT NOT NULL,
`dimen 1 number` DECIMAL(10, 2) NOT NULL,
`dimen 2 number` DECIMAL(10, 2) NOT NULL,
`dimen 3 number` DECIMAL(10, 2) NOT NULL,
`dimen 1 type` TEXT NOT NULL,
`dimen 2 type` TEXT NOT NULL,
`dimen 3 type` TEXT NOT NULL,
`materials 2` TEXT NOT NULL,
`manufacturing processes 2` TEXT NOT NULL,
`weight` INT(11) NOT NULL,
`measuring remarks` TEXT NOT NULL,
`munsell color information` TEXT NOT NULL,
`reproduction` TEXT NOT NULL,
`reproduction notes` TEXT NOT NULL,
`published description` TEXT NOT NULL,
`scholarly notes` TEXT NOT NULL,
`bibliography` TEXT NOT NULL,
`comparanda` TEXT NOT NULL,
`exhibit label` TEXT NOT NULL,
`artist` TEXT NOT NULL,
`spurlock loc 3` TEXT NOT NULL,
`archaeological data` TEXT NOT NULL,
`credit line` TEXT NOT NULL,
`provenance` TEXT NOT NULL,
`museum dedication` TEXT NOT NULL,
`spurlock status` TEXT NOT NULL,
`public description` TEXT NOT NULL,
`working set 5 wb` TEXT NOT NULL,
`image source` TEXT NOT NULL,
`cm mec ma` TEXT NOT NULL,
`webprivate` TEXT NOT NULL,
`spurlock loc 2` TEXT NOT NULL,
`hiresimagecheck` TEXT NOT NULL,
PRIMARY KEY (`aid`),
FULLTEXT KEY `name` (`name`),
FULLTEXT KEY `accession number` (`accession number`, `name`, `period 1`,
`visual description`, `materials 2`, `published description`, `artist`,
`credit line`),
FULLTEXT KEY `accession number_2` (`accession number`),
FULLTEXT KEY `visual description` (`visual description`),
FULLTEXT KEY `published description` (`published description`)
)
engine=myisam
DEFAULT charset=latin1
auto_increment=1;
DROP TABLE IF EXISTS culture;
CREATE TABLE IF NOT EXISTS `culture`
(
`cid` INT(10) UNSIGNED NOT NULL auto_increment,
`culture` VARCHAR(255) NOT NULL,
PRIMARY KEY (`cid`),
UNIQUE KEY `culture` (`culture`)
)
engine=myisam
DEFAULT charset=latin1
auto_increment=1;
DROP TABLE IF EXISTS geocity;
CREATE TABLE IF NOT EXISTS `geocity`
(
`gid` INT(10) UNSIGNED NOT NULL auto_increment,
`city` VARCHAR(255) NOT NULL,
PRIMARY KEY (`gid`),
UNIQUE KEY `city` (`city`)
)
engine=myisam
DEFAULT charset=latin1
auto_increment=1;
DROP TABLE IF EXISTS geocontinent;
CREATE TABLE IF NOT EXISTS `geocontinent`
(
`gid` INT(10) UNSIGNED NOT NULL auto_increment,
`continent` VARCHAR(255) NOT NULL,
PRIMARY KEY (`gid`),
UNIQUE KEY `continent` (`continent`)
)
engine=myisam
DEFAULT charset=latin1
auto_increment=1;
DROP TABLE IF EXISTS geocountry;
CREATE TABLE IF NOT EXISTS `geocountry`
(
`gid` INT(10) UNSIGNED NOT NULL auto_increment,
`country` VARCHAR(255) NOT NULL,
PRIMARY KEY (`gid`),
UNIQUE KEY `country` (`country`)
)
engine=myisam
DEFAULT charset=latin1
auto_increment=1;
DROP TABLE IF EXISTS geolocality;
CREATE TABLE IF NOT EXISTS `geolocality`
(
`gid` INT(10) UNSIGNED NOT NULL auto_increment,
`locality` VARCHAR(255) NOT NULL,
PRIMARY KEY (`gid`),
UNIQUE KEY `locality` (`locality`)
)
engine=myisam
DEFAULT charset=latin1
auto_increment=1;
DROP TABLE IF EXISTS georegion;
CREATE TABLE IF NOT EXISTS `georegion`
(
`gid` INT(10) UNSIGNED NOT NULL auto_increment,
`region` VARCHAR(255) NOT NULL,
PRIMARY KEY (`gid`),
UNIQUE KEY `region` (`region`)
)
engine=myisam
DEFAULT charset=latin1
auto_increment=1;
DROP TABLE IF EXISTS nomcategory;
CREATE TABLE IF NOT EXISTS `nomcategory`
(
`nid` INT(10) UNSIGNED NOT NULL auto_increment,
`category` VARCHAR(255) NOT NULL,
PRIMARY KEY (`nid`),
UNIQUE KEY `category` (`category`)
)
engine=myisam
DEFAULT charset=latin1
auto_increment=1;
DROP TABLE IF EXISTS nomclassification;
CREATE TABLE IF NOT EXISTS `nomclassification`
(
`nid` INT(10) UNSIGNED NOT NULL auto_increment,
`classification` VARCHAR(255) NOT NULL,
PRIMARY KEY (`nid`),
UNIQUE KEY `classification` (`classification`)
)
engine=myisam
DEFAULT charset=latin1
auto_increment=1;
DROP TABLE IF EXISTS nomsubclassification;
CREATE TABLE IF NOT EXISTS `nomsubclassification`
(
`nid` INT(10) UNSIGNED NOT NULL auto_increment,
`subclassification` VARCHAR(255) NOT NULL,
PRIMARY KEY (`nid`),
UNIQUE KEY `subclassification` (`subclassification`)
)
engine=myisam
DEFAULT charset=latin1
auto_increment=1;
However, when I attempt to execute this query, I get:
MySQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CREATE TABLE IF NOT EXISTS `artifacts`
(
`aid` IN' at line 3
The syntax all looks good to me, can anyone spot the error? The only SQL formatter that I use reports no errors.
Your script works fine. The problem is either the way you are executing it (client will expect query by query not the whole script) or you have changed your delimiter to something else than ;
DELIMITER ;
Does your username have permission to create tables?
did you indicate which database to use? I usually do
USE databasename;
CREATE TABLE tablename (
name_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
(etc....)
);
etc.
I have Three tables 1) product, 2)product_x, 3) product_y. I have set primary key for these three tables. The tables are
1) product : id, name, product_type, created_at
2) product_x : id, product_id,description, created_at
3) product_y : id,product_id,description, created_at
The product_id table in the product_x and product_y tables are foreign reference from the table product. If product_type is=1 entry will go to product_x and if product_type=0 entry will go to product_1. SO here my problem is i have set delete on cascade for the foreign key reference for these two tables. But when i delete an entry from product_x or product_y the corresponding id from the product table is not deleted. That means delete on cascade in not working. I need help from you guys, Please help.
Here is my product table.
--
-- Table structure for table `product`
--
CREATE TABLE IF NOT EXISTS `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`code` varchar(100) NOT NULL,
`description` text NOT NULL,
`product_type` tinyint(4) NOT NULL COMMENT '1=pronova product,2=doctor product',
`ingredients` varchar(200) NOT NULL,
`directions` varchar(200) NOT NULL,
`status` tinyint(1) NOT NULL COMMENT '0=inactive,1=active',
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=67 ;
my product_x table
--
-- Table structure for table product_x
CREATE TABLE IF NOT EXISTS `product_x` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`inventory_id` int(11) NOT NULL,
`doctor_id` int(11) NOT NULL,
`stock` varchar(20) NOT NULL,
`image` varchar(50) NOT NULL,
`small_image` varchar(100) NOT NULL,
`sail_price` float DEFAULT NULL,
`acquire_price` float DEFAULT NULL,
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `product_id` (`product`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
and my product_y table is
CREATE TABLE IF NOT EXISTS `product_y` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`inventory_id` int(11) NOT NULL,
`specialization_type` int(11) NOT NULL,
`stock` varchar(20) NOT NULL,
`image` varchar(100) NOT NULL,
`unit_credit_value` int(11) NOT NULL,
`suggested_price` float NOT NULL,
`list_price` float DEFAULT NULL COMMENT 'the price which this product sold if pronova sold this',
`created_at` datetime NOT NULL,
PRIMARY KEY (`id`),
KEY `inventory_id` (`product`),
KEY `FK50E07CF68B1B2BCE` (`inventory_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
Check to make sure that you are using InnoDB and not MyISAM, if you really don't have control over it, you could write a trigger as explained below:
How to use delete cascade on MySQL MyISAM storage engine?