error: You have an error in your SQL syntax when creating table in mysql [duplicate] - sql

This question already has an answer here:
Create a table with a space in the name
(1 answer)
Closed 9 months ago.
I know what a syntax error is but cannot find any error when creating this table:
my code
CREATE TABLE branch supplier (
branch_id INT,
supplier_name VARCHAR(40),
supply_type VARCHAR(40),
PRIMARY KEY (branch_id, supplier_name),
FOREIGN KEY (branch_id) REFERENCES branch(branch_id) ON DELETE CASCADE
);
and the error I'm getting
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 'supplier ( branch_id INT, supplier_name VARCHAR(40), supply_type ' at line 1

It is not allowed to use table names with space(s) unless you use `. This means you need to change this to...
CREATE TABLE `branch supplier`...
or to something without space, as example
CREATE TABLE branch_supplier...

Related

Can't drop a column with a constraint in command line

I have an exercises table with this schema:
CREATE TABLE exercises
(
id INTEGER NOT NULL,
name VARCHAR(255) NOT NULL,
workoutID INTEGER NOT NULL,
FOREIGN KEY (workoutID) REFERENCES workouts(id)
);
I want to delete the column workoutID but it won't let me delete it:
sqlite> ALTER TABLE exercises DROP workoutID;
Error: near "DROP": syntax error
Upon realizing I need to remove the constraints first I try:
sqlite> ALTER TABLE exercises DROP CONSTRAINT workoutID;
Error: near "DROP": syntax error
I followed this question to the letter: How to drop column with constraint?
But it doesn't work for me.
I can start over by dropping the whole table if I have to but I want to learn why this isn't working for me.
Any help and explanation is greatly appreciated.

New to SQL and already having an issue

I'm doing a group project to learn SQL, I'm using jdoodle as an online IDE for now and w3schools. I feel so weird for asking this because this is literally my first attempt but I get an error.
CREATE DATABASE turing;
CREATE TABLE Suppliers (
SupplierNumber int,
SupplierName varchar(255),
SupplierAddress varchar(255),
);
Error: near line 1: in prepare, near "DATABASE": syntax error (1)
Error: near line 2: in prepare, near ")": syntax error (1)
I'm just like copying exactly what w3schools taught me?
I don't see any errors in here, But these are some best practises,
RUN the SQL queries one by one in case if that's causing the error. First create the Database and then Create the Table. Do both seperately.
CREATE TABLE Suppliers (
supplierNumber int PRIMARY KEY NOT NULL,
supplierName varchar(255),
supplierAddress varchar(255)
);
Best practise is to have the column names in lowerCamelCase.
Normally we don't use comma for the last line. It is Unnecessary
Having a Primary key for every table is good. Make it PRIMARY KEY and NOT NULL at the same time to prevent some error in the future.

i cant create a table because name is invalid [duplicate]

This question already has answers here:
ORA-00904: : invalid identifier Oracle sql [closed]
(2 answers)
Closed 1 year ago.
Im trying to create a table with the name " Order " and these datatypes (Note: i dont have a table named Order and I tried to create other tables and it worked ) and the names are:
create table Order(
Onum Number(8) PRIMARY KEY,
Odate Date,
Otime Time(7),
Delivered_on_time char(1));
and the error was:
invalid table name
so I tried to name it Ordeer and try again and it gave me this error:
ORA-00907: missing right parenthesis
Otime Time(7),
Delivered_on_time char(1)
);
what to do? help please
First, you can't call the table order because order is a reserved word. (There's a tricky way to get around this, but it's a bad idea to do so.)
Second, the DATE type also includes the time, so you don't need to have separate date and time columns.
CREATE TABLE orders (
onum NUMBER(8) PRIMARY KEY,
odate DATE,
delivered_on_time CHAR(1)
);

SQL Informix - adding foreign key

I am struggling while creating new database tables with foreign keys in Informix DB.
I need to migrate from one database to another. I am using DBeaver as DB browsing and editing tool. I am trying to run script like this (I just changed names). The commands come from DBeaver DDL export itself, so this is how it should be.
DROP TABLE IF EXISTS eapp:test_table1;
CREATE TABLE eapp:test_table1 (
column1 smallint NOT NULL,
PRIMARY KEY (column1) CONSTRAINT table1_pk
);
DROP TABLE IF EXISTS eapp:test_table2;
CREATE TABLE eapp:test_table2 (
column1 smallint NOT NULL,
column2 varchar(100) NOT NULL,
column3 smallint NOT NULL,
column4 smallint,
PRIMARY KEY (column1) CONSTRAINT table2_pk,
FOREIGN KEY (column4) REFERENCES eapp:test_table1(column1) ON DELETE RESTRICT ON UPDATE RESTRICT CONSTRAINT table2_fk
);
However, execution keeps crashing with a weird trimmed error message:
The language is Czech and it says something like "Syntax error. (Important notice: Database servers, that". But the rest is trimmed even in detail, so I have no idea what is going on.
Full stack:
org.jkiss.dbeaver.model.sql.DBSQLException: SQL Error [42000]: Chyba syntaxe. (Důležitá poznámka: Databázové servery, které
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:134)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeStatement(SQLQueryJob.java:473)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.lambda$0(SQLQueryJob.java:412)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:148)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.executeSingleQuery(SQLQueryJob.java:405)
at org.jkiss.dbeaver.ui.editors.sql.execute.SQLQueryJob.extractData(SQLQueryJob.java:865)
at org.jkiss.dbeaver.ui.editors.sql.SQLEditor$QueryResultsContainer.readData(SQLEditor.java:2833)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.lambda$0(ResultSetJobDataRead.java:98)
at org.jkiss.dbeaver.model.exec.DBExecUtils.tryExecuteRecover(DBExecUtils.java:148)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetJobDataRead.run(ResultSetJobDataRead.java:96)
at org.jkiss.dbeaver.ui.controls.resultset.ResultSetViewer$17.run(ResultSetViewer.java:3356)
at org.jkiss.dbeaver.model.runtime.AbstractJob.run(AbstractJob.java:103)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: java.sql.SQLException: Chyba syntaxe. (Důležitá poznámka: Databázové servery, které
at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:413)
at com.informix.jdbc.IfxSqli.a(IfxSqli.java:3537)
at com.informix.jdbc.IfxSqli.E(IfxSqli.java:3853)
at com.informix.jdbc.IfxSqli.dispatchMsg(IfxSqli.java:2653)
at com.informix.jdbc.IfxSqli.receiveMessage(IfxSqli.java:2569)
at com.informix.jdbc.IfxSqli.executeExecute(IfxSqli.java:2446)
at com.informix.jdbc.IfxSqli.executeExecute(IfxSqli.java:2421)
at com.informix.jdbc.IfxResultSet.b(IfxResultSet.java:393)
at com.informix.jdbc.IfxStatement.a(IfxStatement.java:1326)
at com.informix.jdbc.IfxStatement.executeImpl(IfxStatement.java:1296)
at com.informix.jdbc.IfxStatement.c(IfxStatement.java:1007)
at com.informix.jdbc.IfxStatement.execute(IfxStatement.java:893)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.execute(JDBCStatementImpl.java:338)
at org.jkiss.dbeaver.model.impl.jdbc.exec.JDBCStatementImpl.executeStatement(JDBCStatementImpl.java:131)
... 12 more
Caused by: java.sql.SQLException
at com.informix.util.IfxErrMsg.getSQLException(IfxErrMsg.java:413)
at com.informix.jdbc.IfxSqli.E(IfxSqli.java:3858)
... 23 more
I was unable to Google message like that, so I am trying to ask here, if anyone has some experience with Informix DB and could possibly give me some hints.
I already tried to remove FK declaration from table creation and add it afterwards via ALTER TABLE, but it yields the same...
DBeaver is not generating valid Informix DDL. Informix does not understand ON DELETE RESTRICT ON UPDATE RESTRICT syntax.
From the online manual:
REFERENCES Clause
|--REFERENCES--table--+------------------+---------------------->
| .-,------. |
| V | |
'-(----column-+--)-'
>--+-------------------------+----------------------------------|
| (1) |
'-------ON DELETE CASCADE-'

creating a table with Foreign key in it gives error ORA-00904: : invalid identifier in oracle 10g [duplicate]

This question already has answers here:
Creating table via SQL Command Line, invalid identifier
(3 answers)
Closed 3 years ago.
Image of the code with table, query and error
I have oracle 10g installed on my computer. I have created a table in it named STUDENT and this STUDENT table has a primary key called RNO and now I want to create another table named FEE and make this RNO key into a foreign key in FEE table with the following query:
CREATE TABLE FEE ( RNO NUMBER(2), Amount number(20) Not Null, Date varchar2(10) Not Null, Receipt Number(10) Not Null, CONSTRAINT FEEFK FOREIGN KEY (RNO) REFERENCES STUDENT (RNO));
Now I have done all I could to correct it but just can't seem to find any problem or error with this query above. The Query gives the following error in Oracle 10g:
ORA-00904: : invalid identifier
Column name can't be DATE, it is reserved for datatype. Rename it to, say, CDATE.
SQL> CREATE TABLE student (rno NUMBER (2) PRIMARY KEY);
Table created.
SQL> CREATE TABLE FEE
2 (
3 RNO NUMBER (2),
4 Amount NUMBER (20) NOT NULL,
5 cDate VARCHAR2 (10) NOT NULL,
6 Receipt NUMBER (10) NOT NULL,
7 CONSTRAINT FEEFK FOREIGN KEY (RNO) REFERENCES STUDENT (RNO)
8 );
Table created.
SQL>
Use double qoutes "Date" or rename your column Date as some other name like DateColumn as Date is a reserved name fpr date types in oracle