Error Code: 1064. You have an error in your SQL syntax; check the manual - mysql-error-1064

--
-- Table structure for table artist
CREATE TABLE artist (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
);
--
-- Dumping data for table artist
INSERT INTO artist VALUES (1,'Bing Crosby'),(2,'Bill Haley & His Comets'),(3,'Elvis Presley'),(4,'The Beatles'),(5,'Queen'),(6,'Culture Club'),(7,'ABBA'),(8,'The Jackson 5');
--
-- Table structure for table song
CREATE TABLE song (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(255) NOT NULL,
artist_id int(11) NOT NULL,
duration int(11) NOT NULL,
PRIMARY KEY (id)
);
--
-- Dumping data for table song
INSERT INTO song VALUES (1,'Fernando',7,257),(2,'Hound Dog',3,136),(3,'White Christmas',1,186),(4,'I Want to Hold Your Hand',4,148),(5,'Waterloo',7,169),(6,'Rock Around the Clock',2,134),(7,'Blue Suede Shoes',3,118),(8,'She Loves You',4,142),(9,'Bohemian Rhapsody',5,356),(10,'Are You Lonesome Tonight?',3,189),(11,'Hey Jude',4,431),(12,'Another One Bites the Dust',5,216);
I am getting following error:
Error Code: 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 'id int(11) NOT NULL AUTO_INCREMENT, `name

Related

ORA-00903: invalid table name when Creating Table

Please help. I'm not sure what is happening. I even tried to drop table but still unsuccessful.
CREATE TABLE 'customer' (
'customer_id' int NOT NULL AUTO_INCREMENT,
'type' varchar(45) NOT NULL,
'name' varchar(45) NOT NULL,
'cut_off' int NOT NULL,
PRIMARY KEY ('customer_id')
)
Error starting at line : 1 in command -
CREATE TABLE 'customer' (
'customer_id' int NOT NULL AUTO_INCREMENT,
'type' varchar(45) NOT NULL,
'name' varchar(45) NOT NULL,
'cut_off' tinyint NOT NULL,
PRIMARY KEY ('customer_id')
)
Error report -
ORA-00903: invalid table name
00903. 00000 - "invalid table name"
*Cause:
*Action:
Column and table names should be without quotes.
Refer to create table query https://docs.oracle.com/cd/B28359_01/server.111/b28310/tables003.htm#ADMIN11004
There are multiple issues in your code.
Names of table and columns should not be wrapped in single quotes.
AUTO_INCREMENT keyword is not allowed. It should be implemented differently in oracle.
NOT NULL is not needed on PK column. It is implicit.
Corrected code:
CREATE TABLE customer (
customer_id int GENERATED always as IDENTITY,
type varchar(45) NOT NULL,
name varchar(45) NOT NULL,
cut_off int NOT NULL,
primary key(customer_id)
);

Importing the sql table shows, Foreign key constraint is incorrectly formed, table made by laravel migration

In Laravel migration, all are working fine. But when I export this table and import in new db, this table shows Foreign Key constraint is incorrectly formed.
sales.sql
-- phpMyAdmin SQL Dump
-- version 4.9.1
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Jul 05, 2020 at 08:19 PM
-- Server version: 10.4.8-MariaDB
-- PHP Version: 7.3.11
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET #OLD_CHARACTER_SET_CLIENT=##CHARACTER_SET_CLIENT */;
/*!40101 SET #OLD_CHARACTER_SET_RESULTS=##CHARACTER_SET_RESULTS */;
/*!40101 SET #OLD_COLLATION_CONNECTION=##COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `inventory_management`
--
-- --------------------------------------------------------
--
-- Table structure for table `sales`
--
CREATE TABLE `sales` (
`id` bigint(20) UNSIGNED NOT NULL,
`sales_no` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
`reference` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`date` date NOT NULL,
`sub_total` double NOT NULL,
`discount` double NOT NULL,
`discount_type` enum('percent','amount') COLLATE utf8mb4_unicode_ci NOT NULL,
`tax` double NOT NULL,
`tax_type` enum('percent','amount') COLLATE utf8mb4_unicode_ci NOT NULL,
`grand_total` double NOT NULL,
`payment_type` enum('no-payment','partial-payment','full-payment') COLLATE utf8mb4_unicode_ci NOT NULL,
`payment_term_id` bigint(20) UNSIGNED DEFAULT NULL,
`notes` text COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_by` bigint(20) UNSIGNED NOT NULL,
`updated_by` bigint(20) UNSIGNED NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`deleted_at` timestamp NULL DEFAULT NULL,
`status` enum('paid','void','cancelled','overdue','unpaid') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`customer_id` bigint(20) UNSIGNED DEFAULT NULL,
`is_item_wise_discount` enum('0','1') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0',
`is_item_wise_tax` enum('0','1') COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `sales`
--
ALTER TABLE `sales`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `sales_sales_no_unique` (`sales_no`),
ADD KEY `sales_payment_term_id_index` (`payment_term_id`),
ADD KEY `sales_created_by_index` (`created_by`),
ADD KEY `sales_updated_by_index` (`updated_by`),
ADD KEY `sales_customer_id_index` (`customer_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `sales`
--
ALTER TABLE `sales`
MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=35;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `sales`
--
ALTER TABLE `sales`
ADD CONSTRAINT `sales_created_by_foreign` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `sales_customer_id_foreign` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `sales_payment_term_id_foreign` FOREIGN KEY (`payment_term_id`) REFERENCES `payment_terms` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
ADD CONSTRAINT `sales_updated_by_foreign` FOREIGN KEY (`updated_by`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
COMMIT;
But all are working fine, When I alter table and do php artisan migrate. It works. The only problem is when exporting table and importing the table in new db. Thank You in advance
are you migrating just one table or the all database? if its just one table, Make sure the other tables exist in the new database with the parent table having information that the other table depending on it needs. let me say if you are migrating sales.sql and its information depends on users say( it has a users_id), then the new database must have the users table in it. Also mind about the datatypes of the column values you are relating in both tables.

Name already existing error in SQL Developer

I got some help last night on this code but now I am getting a different error. My professor is STILL not answering me so I am coming to you guys. Here is the code:
--Create Volunteer Supervisor
CREATE TABLE Volunteer_Supervisor
(
PH_Person_ID Number(10) NOT NULL,
EM_Person_ID Number(10) NOT NULL,
VO_Person_ID Number(10) NOT NULL,
End_Date Date NOT NULL,
Begin_Date Date NOT NULL,
Hours_Worked Number(4) NULL,
PWork_Unit_ID Number(4) NULL,
PRIMARY KEY (PWork_Unit_ID),
CONSTRAINT CCPHPersonID_FK FOREIGN KEY (PH_Person_ID) References Physician (PH_Person_ID),
CONSTRAINT CCEMPersonID_FK FOREIGN KEY (EM_Person_ID) References Employee (EM_Person_ID),
CONSTRAINT CCVOPersonID_FK FOREIGN KEY (VO_Person_ID) References Volunteer (VO_Person_ID),
CONSTRAINT CCPWorkUnitID_PK FOREIGN KEY (PWork_Unit_ID) References Work_Unit (PWork_Unit_ID)
);
Now I have changed the names but still getting this error:
Error report -
ORA-00955: name is already used by an existing object
00000 - "name is already used by an existing object"
*Cause:
*Action:
What am I missing?
If you are sure that no such table Volunteer_Supervisor exists, You may try below code -
CREATE TABLE Volunteer_Supervisor
(
PH_Person_ID Number(10) NOT NULL,
EM_Person_ID Number(10) NOT NULL,
VO_Person_ID Number(10) NOT NULL,
End_Date Date NOT NULL,
Begin_Date Date NOT NULL,
Hours_Worked Number(4) NULL,
PWork_Unit_ID Number(4) NULL,
PRIMARY KEY (PWork_Unit_ID),
FOREIGN KEY (PH_Person_ID) References Physician (PH_Person_ID),
FOREIGN KEY (EM_Person_ID) References Employee (EM_Person_ID),
FOREIGN KEY (VO_Person_ID) References Volunteer (VO_Person_ID),
FOREIGN KEY (PWork_Unit_ID) References Work_Unit (PWork_Unit_ID)
);

Oracle SQL: Missing right parenthesis

I'm migrating a database from mySQL to Oracle SQL but I'm getting a "ORA-00907: missing right parenthesis" error when creating a table. I've tried everything I can think of but still keep getting the same error.
Create table statement:
CREATE TABLE menu
(id int(11) NOT NULL AUTO_INCREMENT,
restaurant_id varchar(30) DEFAULT NULL,
menu_name varchar(30) DEFAULT NULL,
menu_description varchar(500) DEFAULT NULL,
menu_price varchar(30) DEFAULT NULL,
quantity int(11) DEFAULT '1',
PRIMARY KEY (id))
I think the problem is with the PRIMARY KEY as it's only table with PRIMARY KEYs that I get the error on. Apologies if this is an obvious question, I'm new to Oracle SQL. Thanks in advance!
Oracle != MySQL:
CREATE TABLE menu
( id number(11,0) GENERATED AS IDENTITY, --IDENTITY <=> AUTO_INCREMENT
restaurant_id varchar2(30) DEFAULT NULL, --VARCHAR2 instead of VARCHAR
menu_name varchar2(30) DEFAULT NULL,
menu_description varchar2(500) DEFAULT NULL,
menu_price varchar2(30) DEFAULT NULL,
quantity number(11,0) DEFAULT '1', --NUMBER(11,0) instead of INT(11)
PRIMARY KEY (id)
);

H2 Schema initialization. Syntax error in SQL statement

I have a spring boot application and I trying to initialize some data on application startup.
This is my application properties:
#Database connection
spring.datasource.url=jdbc:h2:mem:test_db
spring.datasource.username=...
spring.datasource.password=...
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.initialize=true
spring.datasource.schema=schema.sql
spring.datasource.data=schema.sql
#Hibernate configuration
#spring.jpa.hibernate.ddl-auto = none
This is schema.sql:
CREATE TABLE IF NOT EXISTS `Person` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`first_name` VARCHAR(50) NOT NULL,
`age` INTEGER NOT NULL,
PRIMARY KEY(`id`)
);
and data.sql
INSERT INTO `Person` (
`id`,
`first_name`,
`age`
) VALUES (
1,
'John',
20
);
But I got 'Syntax error in SQL statement' on application startup:
19:08:45.642 6474 [main] INFO o.h.tool.hbm2ddl.SchemaExport - HHH000476: Executing import script '/import.sql'
19:08:45.643 6475 [main] ERROR o.h.tool.hbm2ddl.SchemaExport - HHH000388: Unsuccessful: CREATE TABLE Person (
19:08:45.643 6475 [main] ERROR o.h.tool.hbm2ddl.SchemaExport - Syntax error in SQL statement "CREATE TABLE PERSON ( [*]"; expected "identifier"
Syntax error in SQL statement "CREATE TABLE PERSON ( [*]"; expected "identifier"; SQL statement:
I can't understand, what's wrong with this SQL.
Try this code. Remove PRIMARY KEY(id) and execute it.
CREATE TABLE IF NOT EXISTS `Person` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`first_name` VARCHAR(50) NOT NULL,
`age` INTEGER NOT NULL
);
This error results from the structure of the CREATE TABLE declaration.
It will be the result when you have an extra comma in the end of your SQL declaration--no column declaration following the comma. For example:
CREATE TABLE IF NOT EXISTS `Person` (
`id` INTEGER PRIMARY KEY AUTO_INCREMENT,
`first_name` VARCHAR(50) NOT NULL,
`age` INTEGER NOT NULL, --note this line has a comma in the end
);
That's because CREATE TABLE expects a list of the columns that will be created along with the table, and the first parameter of the column is the identifier. As you check here, the column declaration follows the structure:
identifier datatype <constraints> <autoincrement> <functions>
Thus, in your case, as #budthapa and #Vishwanath Mataphati have mentioned, you could simply remove the PRIMARY KEY(id) line from the CREATE TABLE declaration. Moreover, you have already stated that id is a primary key on the first line of the column definitions.
In case you do not have a statement as the PRIMARY KEY declaration, be sure to check for the extra comma following your last column declaration.
Try this, as you have used Table_name
CREATE TABLE IF NOT EXISTS Person (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50) NOT NULL,
age INTEGER NOT NULL
);
I was add below in to application.properties and it work for me
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.globally_quoted_identifiers_skip_column_definitions = true
What helped in my case was removing single quotes from the table name in my insert query
I had to change this:
INSERT INTO 'translator' (name, email) VALUES ('John Smith', 'john#mail.com');
to this:
INSERT INTO translator (name, email) VALUES ('John Smith', 'john#mail.com');
You set auto increment id, so you can't insert new record with id.
Try INSERT INTO `Person` (
`first_name`,
`age`
) VALUES (
'John',
20
);
I ran into same issue. I fixed that with these application.properties:
spring.jpa.properties.hibernate.connection.charSet=UTF-8
spring.jpa.properties.hibernate.hbm2ddl.import_files_sql_extractor=org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor
Some issue with multi-line and default encoding.

Resources