MySQL error while trying to "translate" MsSQL to MySQL - mysql

I am trying to create two tables on a MySQL database, with the same characteristics of its MsSQL version but i get a syntax error. Any suggestions please?
CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT ((1)) NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (`id` ASC)
);
CREATE TABLE locations
(
`lat` DECIMAL (10, 6) DEFAULT ((0)) NOT NULL,
`lon` DECIMAL (10, 6) DEFAULT ((0)) NOT NULL,
`dt` DATETIME NULL,
`id` INT auto_increment NOT NULL,
`owner` INT NULL
);
EDIT: The error i get is
SQL query:
CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT ((1)) NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (`id` ASC)
)
MySQL said: Documentation
#1064 - 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 '((1)) NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (' at line 7

Try this.
CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT 1 NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE locations
(
`lat` DECIMAL (10, 6) DEFAULT 0 NOT NULL,
`lon` DECIMAL (10, 6) DEFAULT 0 NOT NULL,
`dt` DATETIME DEFAULT NULL,
`id` INT AUTO_INCREMENT NOT NULL,
`owner` INT ,
PRIMARY KEY (`id`)
);

CREATE TABLE logins
(
`id` INT auto_increment NOT NULL,
`name` CHAR (10) NOT NULL,
`pw` CHAR (10) NOT NULL,
`email` VARCHAR (50) NULL,
`role` INT DEFAULT 1 NOT NULL,
CONSTRAINT `pk_logins` PRIMARY KEY (`id` ASC)
)

Related

Syntax error creating a table in my database

I'm getting error after trying to create a table for my data base
This is my script
CREATE TABLE `users` (
`id` int PRIMARY KEY NOT NULL AUTO_INCREMENT,
`user_role_id` int NOT NULL,
`first_name` varchar(250) NOT NULL,
`last_name` varchar(250) NOT NULL,
`name` varchar(250),
`email` varchar(250) NOT NULL,
`dni_type` varchar(32) NOT NULL,
`dni` int NOT NULL,
`phone` int,
`address` varchar(250),
`city` varchar(125),
`state` varchar(125),
`country` varchar(32),
`zip` varchar(32),
`created_at` datetime,
`created_by` int,
`updated_at` datetime,
`updated_by` int,
`deleted_at` datetime,
`deleted_by` int
);
I'm getting
#1064 - Something is wrong in your syntax near 'CREATE TABLE `users` (
`id` int PRIMARY KEY NOT NULL AUTO_INCREMENT,
`u...' on line 2
Why is the error generated, and how can I avoid it in the future?
CREATE TABLE `users` (
`id` int PRIMARY KEY NOT NULL AUTO_INCREMENT
Don't use `
CREATE TABLE `users` (
id int PRIMARY KEY NOT NULL AUTO_INCREMENT

MySQL issue with CREATE TABLE film database

I am trying to create a table with multiple foreign keys which reference other parent tables. 1 of my foreign keys is being accepted 'fk_film_id' however the second is not due to a syntax error. how can I get this to pass?
the body of my tables is below:
create table `Actor_Role` (
`Actor_Role_id` INT AUTO_INCREMENT,
`Actor_Role` VARCHAR(20) NOT NULL,
`Character_Name` VARCHAR(50) NOT NULL,
`Alias_Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`Actor_Role_id`)
);
create table `Actor` (
`Actor_id` INT AUTO_INCREMENT,
`First_Name` VARCHAR(20) NOT NULL,
`Last_Name` VARCHAR(50) NOT NULL,
`Date_of_Birth` DATE NOT NULL,
`Nationality` VARCHAR(20) NOT NULL,
`Gender` VARCHAR(10) NOT NULL,
`Residence` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Actor_id`)
);
create table `Film` (
`Film_id` INT AUTO_INCREMENT,
`Title` VARCHAR(50) NOT NULL,
`Release_Date` DATE NOT NULL,
`Running_Time` INT NOT NULL,
`Budget` BIGINT NOT NULL,
`Box_Office` BIGINT NOT NULL,
`Rating` INT NOT NULL,
`Language` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Film_id`)
);
create table `Film_Staff` (
`Staff_id` INT AUTO_INCREMENT,
`First_Name` VARCHAR(20) NOT NULL,
`Last_Name` VARCHAR(50) NOT NULL,
`Nationality` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Staff_id`)
);
create table `Staff_Role` (
`Staff_Role_id` INT AUTO_INCREMENT,
`Staff_Role` VARCHAR(20) NOT NULL,
PRIMARY KEY (`Staff_Role_id`)
);
create table `Infinity_Stones` (
`Stone_id` INT AUTO_INCREMENT,
`Colour` VARCHAR(10) NOT NULL,
`Power` VARCHAR(10) NOT NULL,
PRIMARY KEY (`Stone_id`)
);
The table I am trying to make looks like this:
CREATE TABLE Film_Staff_Role(
-> `Film_id` INT NOT NULL,
-> `Role_id` INT NOT NULL,
-> `Staff_id` INT NOT NULL,
-> FOREIGN KEY fk_film_id(Film_id)
-> REFERENCES film(Film_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> FOREIGN KEY fk_Role_id(Role_id)
-> REFERENCES Staff_Role(Staff_Role_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> FOREIGN KEY fk_Staff_id(Staff_id)
-> REFERENCES Film_Staff(Staff_id)
-> ON UPDATE CASCADE
-> ON DELETE RESTRICT
-> )ENGINE=InnoDB;
However I receive the following issue when I attempt to execute:
ERROR 1064 (42000): 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 'FOREIGN KEY fk_Staff_Role_id(Staff_Role_id)
REFERENCES Staff_Role(Staff_Role_id' at line 9
How can this be overcome?

MySql CONSTRAINT issues

Fetching back view definitions in final form.
Nothing to fetch
Executing SQL script in server results in:
ERROR: Error 1064: 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 'CONSTRAINT `fk_employee_department`
FOREIGN KEY (`dpt_id`)
REFERENCES `r' at line 16
SQL Code:
CREATE TABLE IF NOT EXISTS `rrm17b`.`employee` (
`emp_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`dpt_id` INT UNSIGNED NULL,
`emp_first` VARCHAR(20) NOT NULL,
`emp_last` VARCHAR(20) NOT NULL,
`emp_type` ENUM('f', 'p') NOT NULL,
`emp_street` VARCHAR(30) NOT NULL,
`emp_city` VARCHAR(20) NOT NULL,
`emp_state` CHAR(2) NOT NULL,
`emp_zip` INT UNSIGNED NOT NULL,
`emp_phone` BIGINT UNSIGNED NOT NULL,
`emp_email` VARCHAR(45) NOT NULL,
`emp_notes` VARCHAR(100) NULL,
PRIMARY KEY (`emp_id`),
INDEX `fk_employee_department_idx` (`dpt_id` ASC)
CONSTRAINT `fk_employee_department`
FOREIGN KEY (`dpt_id`)
REFERENCES `rrm17b`.`department` (`dpt_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 9 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Please try this:
CREATE TABLE IF NOT EXISTS rrm17b.employee(
emp_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
dpt_id INT UNSIGNED NULL,
emp_first VARCHAR(20) NOT NULL,
emp_last VARCHAR(20) NOT NULL,
emp_type ENUM('f', 'p') NOT NULL,
emp_street VARCHAR(30) NOT NULL,
emp_city VARCHAR(20) NOT NULL,
emp_state CHAR(2) NOT NULL,
emp_zip INT UNSIGNED NOT NULL,
emp_phone BIGINT UNSIGNED NOT NULL,
emp_email VARCHAR(45) NOT NULL,
emp_notes VARCHAR(100) NULL,
PRIMARY KEY (emp_id),
INDEX fk_employee_department_idx(dpt_id ASC),
CONSTRAINT fk_employee_department
FOREIGN KEY (dpt_id)
REFERENCES rrm17b.department(dpt_id) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB
Fixed multiple things:
--Added comma after INDEX
--Removed "`" after dpt_id

Error on creating FOREIGN KEY

CREATE TABLE IF NOT EXISTS `servergraph_server` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL,
`ip` VARCHAR(16) NOT NULL,
`port` SMALLINT UNSIGNED NOT NULL
)
CREATE TABLE IF NOT EXISTS `servergraph_data` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`serverid` INT NOT NULL,
`time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
`fps` SMALLINT UNSIGNED NOT NULL,
`replay` TINYINT UNSIGNED NOT NULL,
`dropped_packets` INT UNSIGNED NOT NULL,
`online` TINYINT UNSIGNED NOT NULL,
`clients0` TINYINT UNSIGNED NOT NULL,
`clients1` TINYINT UNSIGNED NOT NULL,
`clients2` TINYINT UNSIGNED NOT NULL,
`clients3` TINYINT UNSIGNED NOT NULL,
FOREIGN KEY (`serverid`) REFERENCES `servergraph_server` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
This are the tables I'd like to create.
But I am always getting an error on the FOREIGN KEY line.
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 'FOREIG' at line 1
I am using this version of MySQL: 5.5.35-0+wheezy1
What is wrong?
Thanks, floube
SOLUTION:
ALTER TABLE `servergraph_data` ADD CONSTRAINT `fk_server` FOREIGN KEY (`serverid`) REFERENCES `servergraph_server` (`id`)
instead of the FOREIGN KEY line in CREATE TABLE servergraph_data
Place semi colons after the create table statements:
CREATE TABLE IF NOT EXISTS `servergraph_server` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` VARCHAR(64) NOT NULL,
`ip` VARCHAR(16) NOT NULL,
`port` SMALLINT UNSIGNED NOT NULL
);
CREATE TABLE IF NOT EXISTS `servergraph_data` (
`id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
`serverid` INT NOT NULL,
`time` TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
`fps` SMALLINT UNSIGNED NOT NULL,
`replay` TINYINT UNSIGNED NOT NULL,
`dropped_packets` INT UNSIGNED NOT NULL,
`online` TINYINT UNSIGNED NOT NULL,
`clients0` TINYINT UNSIGNED NOT NULL,
`clients1` TINYINT UNSIGNED NOT NULL,
`clients2` TINYINT UNSIGNED NOT NULL,
`clients3` TINYINT UNSIGNED NOT NULL,
FOREIGN KEY (`serverid`) REFERENCES `servergraph_server` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
);

How do I remove a uniqueness constraint from a MySQL table?

I created a table in a MySQL database via the following:
CREATE TABLE `newsubscriptions_orderspecification` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`display_name` varchar(256) NOT NULL,
`sub_def_id` integer UNSIGNED NOT NULL,
`source_code_id` integer UNSIGNED NOT NULL,
`order_code_id` integer UNSIGNED NOT NULL,
`rate` numeric(5, 2) NOT NULL,
`type` varchar(4) NOT NULL,
`region` varchar(4) NOT NULL,
`term` varchar(4) NOT NULL,
`buyer_type` varchar(4) NOT NULL,
`is_active` bool NOT NULL,
UNIQUE (`sub_def_id`, `buyer_type`, `rate`, `is_active`)
)
;
How can I remove the uniqueness constraint?
use this:
ALTER TABLE `newsubscriptions_orderspecification` DROP INDEX `sub_def_id`