Unable to create table from query - mysql

I have the following query
CREATE TABLE grades_gra (
id_gra INT(11) NOT NULL AUTO_INCREMENT,
identifier_gra VARCHAR(2) DEFAULT NULL,
name_gra VARCHAR(250) DEFAULT NULL
PRIMARY KEY (id_gra)
)
ENGINE = INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=UTF8;
When I execute it, it gives me an error invalid default vale for 'naes_gra'

You forgot a comma before primary key:
CREATE TABLE grades_gra (
id_gra INT(11) NOT NULL AUTO_INCREMENT,
identifier_gra VARCHAR(2) DEFAULT NULL,
name_gra VARCHAR(250) DEFAULT NULL,
PRIMARY KEY (id_gra)
)
ENGINE = INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=UTF8;
SQLFiddle demo

Put the comma after DEFAULT NULL, before PRIMARY keyword

Related

Change table structure from mysql to sqllite

i want to change database of my simple application from mysql to sqlite, this is my sql command :
CREATE TABLE `Todo` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Title` varchar(255) DEFAULT NULL,
`Category` varchar(255) DEFAULT NULL,
`State` varchar(255) DEFAULT NULL,
PRIMARY KEY (`Id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
im try to create sqllite but return me this error : Error: near "AUTO_INCREMENT": syntax error how i can fix it ?
Instead of using primary key separately, please mention it with your AUTO INCREMENT key
CREATE TABLE `Todo` (
`Id` integer primary key AUTOINCREMENT,
`Title` varchar(255) DEFAULT NULL,
`Category` varchar(255) DEFAULT NULL,
`State` varchar(255) DEFAULT NULL
)

There can be only one auto column error when giving AUTO_INCREMENT value

I am using phpMyAdmin while trying to create a table.
The definition is as follows
CREATE TABLE `koment` (
`movieID` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`id` int(11) NOT NULL,
`text` varchar(255) DEFAULT NULL,
PRIMARY KEY (`movieID`,`userID`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
I want to have id as an auto increment variable but it seems that I can't make it thus even though it is a primary key(I needed the id attribute because a user can write many comments on the same movie). So I don't understand why I still get this error. Thanks in advance.
I'd rather advise you to have clear PRIMARY KEY which is AUTO_INCREMENT and UNIQUE KEY like in your example:
CREATE TABLE `koment` (
`movieID` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY (`movieID`,`userID`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `koment` (
`movieID` int(11) NOT NULL,
`userID` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` varchar(255) DEFAULT NULL,
PRIMARY KEY (`movieID`,`userID`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

Got the ERROR 1046* CREATE TABLE IF NOT EXISTS `activity` (

Table structure for table activity
CREATE TABLE IF NOT EXISTS `activity` (
`fnum` int(10) unsigned NOT NULL auto_increment,
`fid` int(25) default NULL,
`fdate` datetime default NULL,
`ftask` varchar(50) default NULL,
`username` varchar(255) NOT NULL default '',
PRIMARY KEY (`fnum`),
KEY `username` (`username`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1204 ;
MySQL said:
1046 - No database selected
As the error says "1046 - No database selected". You should start your script with selecting a database. E.g.:
use my_database;

MYSQL table creation error #1072

I ran an SQL query as follows in MYSQL:
CREATE TABLE `table1_companies` (
`company_id` int(11) NOT NULL AUTO_INCREMENT,
`RSSD9001` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`company_id`),
KEY `index1` (`RSSDID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
And got the following error:
#1072 - Key column 'RSSDID' doesn't exist in table `
Any thoughts? I am new to MYSQL. The table does not already exist in my database.
The error tells you exactly what is wrong.
Either add RSSDID to the schema;
CREATE TABLE `table1_companies` (
`RSSDID` INT(5) NOT NULL,
`company_id` int(11) NOT NULL AUTO_INCREMENT,
`RSSD9001` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`company_id`),
KEY `index1` (`RSSDID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Or remove the KEY
CREATE TABLE `table1_companies` (
`company_id` int(11) NOT NULL AUTO_INCREMENT,
`RSSD9001` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
The erroe says that all, the column RSSDID is not there in your table. You need to add that as well:
CREATE TABLE `table1_companies` (
`company_id` int(11) NOT NULL AUTO_INCREMENT,
`RSSDID` INT(11) NOT NULL, --> Here
`RSSD9001` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
PRIMARY KEY (`company_id`),
KEY `index1` (`RSSDID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

MySQL Error Number 150 when creating Table with Foreign Key

I am having an issue creating a new table in my database. I've seen that the error code it is returning is to do with Foreign Key constraints.
I checked to ensure that the data type of the foreign key in the new table matched the data type of the primary key in the other table. They are both int(11).
However I am still getting an error. Am I missing something? This is my SQL script for creating the new table:
CREATE TABLE `regular_features` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(200) DEFAULT NULL,
`day` VARCHAR(200) DEFAULT NULL,
`description` TEXT DEFAULT NULL,
`programme_id` INT(11) NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`programme_id`) REFERENCES directoryprogramme(id)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
This is the original table containing the primary key:
CREATE TABLE `directoryprogramme` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(250) NOT NULL,
`broadcast_time` VARCHAR(100) NOT NULL,
`description` TEXT NOT NULL,
`days` VARCHAR(150) NOT NULL,
`contributors` VARCHAR(250) NOT NULL,
`directorycompany_id` INT(11) NOT NULL,
`directorycontact_id` VARCHAR(250) NOT NULL,
`facebook_link` VARCHAR(250) DEFAULT NULL,
`twitter_link` VARCHAR(250) DEFAULT NULL,
`wikipedia_link` VARCHAR(250) DEFAULT NULL,
`web` VARCHAR(250) DEFAULT NULL,
`imageextension` VARCHAR(10) DEFAULT NULL,
`type` VARCHAR(20) NOT NULL DEFAULT 'other',
PRIMARY KEY (`id`)
) ENGINE=MYISAM AUTO_INCREMENT=1161 DEFAULT CHARSET=utf8;
The Foreign Key will be the id of directoryprogramme
The problem is the last line of your create statement:
ENGINE=INNODB DEFAULT CHARSET=utf8;
You mix MYISAM in ald table with INNODB in your new table.
That doesn't work.
Chnage the engine in your new table to MYISAM and it works.