Auto_increment with Perl SQLite - sql

I am having trouble with auto increment in SQLite using Perl.
Database design:
$dbh->do( "CREATE TABLE IF NOT EXISTS `Users` (
`UserID` VARCHAR(45) NOT NULL PRIMARY KEY UNIQUE ,
`First_Name` VARCHAR(45) NOT NULL ,
`Last_Name` VARCHAR(45) NOT NULL ,
`Email` VARCHAR(45) NOT NULL ,
`Password` VARCHAR(45) NOT NULL )" ) ;
$dbh->do( "CREATE TABLE IF NOT EXISTS `Files` (
`FileID` INTEGER AUTO_INCREMENT ,
`UserID` VARCHAR(45) NOT NULL ,
`File_Name` VARCHAR(45) NOT NULL ,
`File` BLOB NULL ,
PRIMARY KEY (`FileID`, `UserID`) ,
CONSTRAINT `fk_Files_Users1`
FOREIGN KEY (`UserID` )
REFERENCES `Users` (`UserID` )
ON DELETE CASCADE
ON UPDATE CASCADE)" ) ;
The query:
$dbh->do( "INSERT INTO Files ( UserID, File_Name )
VALUES ( '$_[0]', '$_[1]' )" ) ;
Even though I have identified the FileID as being both an AUTO_INCREMENT and a PRIMARY KEY, when I add a new field using the above query the FileID is left blank and isn't incremented automatically. Any thoughts? Have I designed the tables wrong?
Cheers,
Nate

I recommend you to create a primary key on Files table with the FileID field exclusively (you have two fields). And I think you have a typo: it's AUTOINCREMENT, (you type AUTO_INCREMENT). See this: http://www.sqlite.org/autoinc.html

Related

How to select all records with foreign key?

I have tables that have been created like so:
CREATE TABLE `d_account` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`account_name` varchar(128) CHARACTER SET utf8 NOT NULL,
`user_id` smallint(5) NOT NULL,
`type_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK_account_type` (`type_id`),
CONSTRAINT `FK_type` FOREIGN KEY (`type_id`) REFERENCES `d_types` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
);
CREATE TABLE `d_types` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) CHARACTER SET latin1 NOT NULL,
PRIMARY KEY (`id`)
);
How would I select all records that exist in both tables below using the foreign key where the d_type name equals ‘large’?
That's basically just a JOIN like this:
SELECT * FROM d_types t
JOIN d_account a ON t.id = a.type_id
WHERE t.name = 'large';
The * after select should be replaced by the columns you want to select.

Derby and SQL from Workbench

I've a project at school and i must use a Derby DB.
I'm using eclipse and the SQL Scrapbook to execute my SQL script.
Here is the problem:
My SQL script was generated by Mysql Workbench, we did a schemas and then exported it to a .sql
When i try to execute it in the SQL Scrapbook, I have MANY errors, the sql seems not to be adapted to derby (for example, AUTOINCREMENT is now (START WITH 1, INCREMENT BY 1).
Here is my .sql :
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';
DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;
-- -----------------------------------------------------
-- Table `mydb`.`User`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`User` ;
CREATE TABLE IF NOT EXISTS `mydb`.`User` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(45) NOT NULL ,
`lastname` VARCHAR(45) NOT NULL ,
`email` VARCHAR(45) NOT NULL ,
`adress` VARCHAR(45) NOT NULL ,
`city` VARCHAR(45) NOT NULL ,
`zip` VARCHAR(45) NOT NULL ,
`login` VARCHAR(45) NOT NULL ,
`password` VARCHAR(45) NOT NULL ,
`admin` TINYINT(1) NOT NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_general_ci;
-- -----------------------------------------------------
-- Table `mydb`.`Movie`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Movie` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Movie` (
`id` INT NOT NULL AUTO_INCREMENT ,
`title` VARCHAR(45) NOT NULL ,
`resume` VARCHAR(500) NULL ,
`genre` VARCHAR(60) NULL ,
`grade` INT(11) NULL ,
`review_pub` VARCHAR(200) NULL ,
`review_gen` VARCHAR(200) NULL ,
`poster` VARCHAR(100) NULL ,
`duration` INT(11) NULL ,
`release_date` VARCHAR(45) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`Projection`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`Projection` ;
CREATE TABLE IF NOT EXISTS `mydb`.`Projection` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT ' ' ,
`date` DATE NULL ,
`length` INT(11) NULL ,
`Movie_id` INT NOT NULL ,
`price` DECIMAL(10,0) NULL ,
`location` VARCHAR(45) NOT NULL ,
`place_nbr` INT(11) NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_Projection_Movie` (`Movie_id` ASC) ,
CONSTRAINT `fk_Projection_Movie`
FOREIGN KEY (`Movie_id` )
REFERENCES `mydb`.`Movie` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `mydb`.`command`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`command` ;
CREATE TABLE IF NOT EXISTS `mydb`.`command` (
`id` INT NOT NULL AUTO_INCREMENT ,
`Projection_id` INT NOT NULL ,
`User_id` INT NOT NULL ,
`paid` TINYINT(1) NOT NULL ,
PRIMARY KEY (`id`) ,
INDEX `fk_command_Projection1` (`Projection_id` ASC) ,
INDEX `fk_command_User1` (`User_id` ASC) ,
CONSTRAINT `fk_command_Projection1`
FOREIGN KEY (`Projection_id` )
REFERENCES `mydb`.`Projection` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_command_User1`
FOREIGN KEY (`User_id` )
REFERENCES `mydb`.`User` (`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;
Thank you
Try using DdlUtils (http://db.apache.org/ddlutils/) to export your schema from your MySQL database, then you can import that same schema to Derby.

How can I insert a new column after x on a table?

I have a table called users:
CREATE TABLE `users` (
`UID` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`username` VARCHAR(45) NOT NULL ,
`password` VARCHAR(200) NOT NULL ,
`name` VARCHAR(45) NOT NULL ,
`email` VARCHAR(255) NOT NULL ,
`phone` VARCHAR(30) NOT NULL ,
`logo` VARCHAR(255) NULL ,
`address` VARCHAR(100) NULL ,
`information` TEXT NULL ,
`announcement` TEXT NULL ,
`role` INT UNSIGNED NULL ,
`yahoo` VARCHAR(100) NULL ,
`twitter` VARCHAR(20) NULL ,
`timelogin` DATETIME NULL,
PRIMARY KEY (`UID`) ,
UNIQUE INDEX `UID_UNIQUE` (`UID` ASC) ,
UNIQUE INDEX `name_UNIQUE` (`name` ASC) ,
UNIQUE INDEX `logo_UNIQUE` (`logo` ASC) ,
UNIQUE INDEX `username_UNIQUE` (`username` ASC))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = utf8_unicode_ci;
the problem is that I want to add or insert a new row like
`image_seal` VARCHAR(255) NULL ,
after logo, can we do something like that in mysql ? append-thing?
Thanks
Adam Ramadhan
ALTER TABLE users ADD COLUMN image_seal VARCHAR(255) NULL AFTER logo;
Check here for Syntax of ALTER TABLE
It seems you want to add a new column to your table, not a row.
From the MySQL reference it seems you can use the keyword AFTER.
ALTER TABLE users ADD COLUMN image_seal VARCHAR(255) AFTER logo;
should do the trick.
What are you using to administer your database? SQLBuddy, PHPmyAdmin and others can do this for you, without you needing to run queries.

How to use subqueries in MySQL

I have two tables.
Table user:
CREATE TABLE IF NOT EXISTS `user` (
`user_id` int(20) NOT NULL AUTO_INCREMENT,
`ud_id` varchar(50) NOT NULL,
`name` text NOT NULL,
`password` text NOT NULL,
`email` varchar(200) NOT NULL,
`image` varchar(150) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB
and mycatch:
CREATE TABLE IF NOT EXISTS `mycatch` (
`catch_id` int(11) NOT NULL AUTO_INCREMENT,
`catch_name` text NOT NULL,
`catch_details` text NOT NULL,
`longitude` float(10,6) NOT NULL,
`latitude` float(10,6) NOT NULL,
`time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`image` varchar(150) NOT NULL,
`user_id` int(20) NOT NULL,
PRIMARY KEY (`catch_id`),
KEY `user_id` (`user_id`)
) ENGINE=InnoDB;
ALTER TABLE `mycatch`
ADD CONSTRAINT `mycatch_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE ON UPDATE CASCADE;
My goal is: I want to retrieve longitude and latitude from mycatch against given ud_id (from user) and catch_id (from mycatch) where ud_id = given ud_id and catch_id > given catch_id.
I used the query but fail to retrieve
SELECT ud_id=$ud_id FROM user
WHERE user_id =
(
SELECT user_id,longitude,latitude from mycatch
WHERE catch_id>'$catch_id'
)
The error is:
#1241 - Operand should contain 1 column(s)
First, try not to use subqueries at all, they're very slow in MySQL.
Second, a subquery wouldn't even help here. This is a regular join (no, Mr Singh, not an inner join):
SELECT ud_id FROM user, mycatch
WHERE catch_id>'$catch_id'
AND user.user_id = mycatch.user_id
Select m.longitude,m.latitude from user u left join mycatch m
on u.user_id =m.user_id
where u.ud_id=$ud_id and
m.catch_id >$catch_id

Can not create table, errno: 150

What is bad in these queries for creating tables?
CREATE TABLE IF NOT EXISTS `CVM`.`CANDIDATE` (
`ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
`FULL_NAME` VARCHAR(64) NOT NULL ,
`HR_INTERVIEW_DATE` DATE NULL ,
`JOB_SEARCH_STATUS` TINYINT(1) NULL ,
`REFERENCES_OPTIONAL` TEXT NULL ,
`AVAILABILITY` TEXT NULL ,
`LOCATION` VARCHAR(200) NULL ,
`TRIP` TEXT NULL ,
`INTERPERSONAL_RISKS` TEXT NULL ,
`TECHNICAL_INTERVIEW_DATE` DATE NULL ,
`EXPERIENCE_IT` TINYINT UNSIGNED NULL ,
`EXPERIENCE_JAVA` TINYINT UNSIGNED NULL ,
`ENGLISH_CHECK_DATE` DATE NULL ,
`ENGLISH_READINESS_DIALOGUE` VARCHAR(100) NULL ,
`ENGLISH_TIME_IMPROVMENT` TINYINT UNSIGNED NULL ,
`ENGLISH_LEVEL_ID` INT NULL ,
`LEVEL_ID` INT NULL ,
`DESIRED_POSITIONS` VARCHAR(100) NULL ,
`HR_COMMENT` TEXT NULL ,
`EXPERT_COMMENT` TEXT NULL ,
`FOREIGN_PASSPORT_ID` INT(11) UNSIGNED NULL ,
`SALARY_ID` INT(11) UNSIGNED NULL ,
`SSE_INFO_ID` INT(11) UNSIGNED NULL ,
`EXPERT_NAME` VARCHAR(100) NULL ,
`HR_NAME` VARCHAR(100) NULL ,
`TEACHER_NAME` VARCHAR(100) NULL ,
PRIMARY KEY (`ID`) ,
INDEX `FK_CANDIDATE_ENGLISH_LEVEL1` (`ENGLISH_LEVEL_ID` ASC) ,
INDEX `fk_CANDIDATE_LEVEL1` (`LEVEL_ID` ASC) ,
INDEX `fk_CANDIDATE_FOREIGN_PASSPORT1` (`FOREIGN_PASSPORT_ID` ASC) ,
INDEX `fk_CANDIDATE_SALARY1` (`SALARY_ID` ASC) ,
INDEX `fk_CANDIDATE_SSE_INFO1` (`SSE_INFO_ID` ASC) ,
CONSTRAINT `FK_CANDIDATE_ENGLISH_LEVEL1`
FOREIGN KEY (`ENGLISH_LEVEL_ID` )
REFERENCES `CVM`.`ENGLISH_LEVEL` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_CANDIDATE_LEVEL1`
FOREIGN KEY (`LEVEL_ID` )
REFERENCES `CVM`.`LEVEL` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_CANDIDATE_FOREIGN_PASSPORT1`
FOREIGN KEY (`FOREIGN_PASSPORT_ID` )
REFERENCES `CVM`.`FOREIGN_PASSPORT` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_CANDIDATE_SALARY1`
FOREIGN KEY (`SALARY_ID` )
REFERENCES `CVM`.`SALARY` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_CANDIDATE_SSE_INFO1`
FOREIGN KEY (`SSE_INFO_ID` )
REFERENCES `CVM`.`SSE_INFO` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CVM`.`CANDIDATE_HISTORY`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CVM`.`CANDIDATE_HISTORY` (
`ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
`CANDIDATE_ID` INT(11) UNSIGNED NOT NULL ,
PRIMARY KEY (`ID`) ,
INDEX `fk_CANDIDATE_HISTORY_CANDIDATE1` (`CANDIDATE_ID` ASC) ,
CONSTRAINT `fk_CANDIDATE_HISTORY_CANDIDATE1`
FOREIGN KEY (`CANDIDATE_ID` )
REFERENCES `CVM`.`CANDIDATE` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CVM`.`CANDIDATE_CHANGES`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CVM`.`CANDIDATE_CHANGES` (
`ID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT ,
`FIELD` VARCHAR(100) NULL ,
`CHANGED_VALUE` TEXT NULL ,
PRIMARY KEY (`ID`) )
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `CVM`.`CANDIDATE_HISTORY_has_CANDIDATE_CHANGES`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `CVM`.`CANDIDATE_HISTORY_has_CANDIDATE_CHANGES` (
`CANDIDATE_HISTORY_ID` INT(11) UNSIGNED NOT NULL ,
`CANDIDATE_CHANGES_ID` INT(11) UNSIGNED NOT NULL ,
PRIMARY KEY (`CANDIDATE_HISTORY_ID`, `CANDIDATE_CHANGES_ID`) ,
INDEX `fk_CANDIDATE_HISTORY_has_CANDIDATE_CHANGES_CANDIDATE_CHANGES1` (`CANDIDATE_CHANGES_ID` ASC) ,
CONSTRAINT `fk_CANDIDATE_HISTORY_has_CANDIDATE_CHANGES_CANDIDATE_HISTORY1`
FOREIGN KEY (`CANDIDATE_HISTORY_ID` )
REFERENCES `CVM`.`CANDIDATE_HISTORY` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_CANDIDATE_HISTORY_has_CANDIDATE_CHANGES_CANDIDATE_CHANGES1`
FOREIGN KEY (`CANDIDATE_CHANGES_ID` )
REFERENCES `CVM`.`CANDIDATE_CHANGES` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
In SQL workbench its all ok, but using maven-sql-plugin it failed to create table candidate_history_has_candidate_changes. All names are less than 64 symbols. Please, help!
Try to narrow down the possible causes, starting with a simple create table. Try creating this simple table with and without backtick quotes in case the plugin isn't handling them properly.
Check you are logging in as the same user in both cases.
See if there are any leftovers from the tables. CREATE IF NOT EXIST does nothing if the tables already exist. (see asker's comments for more info).
Lots of possible answers here:
http://verysimple.com/2006/10/22/mysql-error-number-1005-cant-create-table-mydbsql-328_45frm-errno-150/