Create Table Syntax Error - MS Access, SQL view - sql

I keep getting CREATE TABLE Syntax Error, but I don't see the error! What is causing the error?
My SQL:
CREATE TABLE my_employee
(
employee_id INTEGER PRIMARY KEY NOT NULL,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(30) NOT NULL,
address VARCHAR(10) NOT NULL,
birthdate DATE,
salary NUMERIC(8,2) DEFAULT 15000,
marital_status CHAR(1)
);

Since your DDL statement includes DEFAULT, you must execute it with ADO. I loaded your statement into a string variable and executed it from Access 2007 like this:
CurrentProject.Connection.Execute strSql
The salary field is decimal with precision 8, scale 2, and default 15000.
DEFAULT is one of the Access SQL features added with Jet 4.0. Those features are not available for a statement executed from DAO. If you are using Access' query designer to create and execute the statement, you're using DAO. Same if you were using CurrentDb.Execute. But CurrentProject.Connection is an ADO object, so it can .Execute Jet 4.0 features.
Note NOT NULL is not necessary after PRIMARY KEY since PRIMARY KEY implies NOT NULL. However PRIMARY KEY NOT NULL does not trigger an error. The statement works as you originally wrote it as long as you execute it from ADO.

Well I was having the same problem with Ms Access 2007, but I solved it later on.
It is because actually some features are disabled by default for security reasons over there.
When it shows you syntax error, you can see the message at menu bar somewhere or at the bottom: Some Features Are Disabled For Security Reasons....
Click on the message then proceed to enable further features.

As HansUp said: "default" in DDL doesn't work here. As an alternative you can create the table without the default first and add the default via the TableDef afterwards:
CurrentDb().Execute "create table my_employee ..."
CurrentDb().TableDefs("my_employee").Fields("salary").DefaultValue = 15000

Your problem is in your PRIMARY KEY declaration
CREATE TABLE my_employee
(
employee_id INTEGER NOT NULL,
first_name VARCHAR(25) NOT NULL,
last_name VARCHAR(30) NOT NULL,
address VARCHAR(10) NOT NULL,
birthdate DATE,
salary NUMERIC(8,2) DEFAULT 15000,
marital_status CHAR(1),
PRIMARY KEY (employee_id)
);

Related

H2 refuses to create auto_increment for Postgres emulated database

I created an in memory H2 database with JDBC URL
jdbc:h2:~/test;MODE=PostgreSQL;DATABASE_TO_LOWER=TRUE;DEFAULT_NULL_ORDERING=HIGH
The H2 web console refuses to let me do an auto_increment. I've seen serial for Postgres, but that doesn't work either.
At it's simplest, it hates:
create table test(id bigint auto_increment);
Syntax error in SQL statement "create table test(id bigint [*]auto_increment)"; expected "ARRAY, INVISIBLE, VISIBLE, NOT NULL, NULL, AS, DEFAULT, GENERATED, ON UPDATE, NOT NULL, NULL, DEFAULT ON NULL, NULL_TO_DEFAULT, SEQUENCE, SELECTIVITY, COMMENT, CONSTRAINT, COMMENT, PRIMARY KEY, UNIQUE, NOT NULL, NULL, CHECK, REFERENCES, ,, )"; SQL statement:
create table test(id bigint auto_increment) [42001-214] 42001/42001 (Help)
Why do I care:
My code base was failing with NULL not allowed for column "REV". I'm using JPA/Hibernate + Liquibase. In order to try the suggestions at
Hibernate Envers + Liquibase: NULL not allowed for column "REV"
I'm trying to add an auto_increment to my Liquibase changelog file.
You can use the SQL Standard's generation clause GENERATED ALWAYS AS IDENTITY. For example:
create table test (
id bigint generated always as identity,
name varchar(10)
);
See PostgreSQL Example.
It works the same way in H2. For example:
create table test(id bigint generated always as identity, name varchar(10));
insert into test (name) values ('Chicago') ;
select * from test;
Result:
ID NAME
-- -------
1 Chicago

Sql error while creating tables - Firebird

I have simple sql code for create table and then add constraint to it. It looks like this:
CREATE TABLE bills (
id INTEGER NOT NULL,
code VARCHAR2(25) NOT NULL,
dateOfGeneration DATE NOT NULL,
job_id INTEGER NOT NULL
);
ALTER TABLE bills ADD CONSTRAINT bills_pk PRIMARY KEY ( id,job_id );
I am using IBExpert - client for Firebird. When I execute this code I get 2 errors:
First error: - in code VARCHAR2(25) NOT NULL
Invalid token.
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 3, column 29.
(.
Second error: - in code ALTER TABLE ...
Invalid token.
Dynamic SQL Error.
SQL error code = -104.
Token unknown - line 8, column 1.
ALTER.
The first one I think is because i am using varchar2 instead of varchar. What about second error? How to fix this?
There is no VARCHAR2 type in Firebird - https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-datatypes-chartypes.html
If you want to run two commands - you have to run TWO commands. You try to run two commands in one, but that is not a way to do it. You have to split them and run one after another. Or you have to wrap them into one EXECUTE BLOCK command.
Also IBExpert has a separate window of Script Executive for multiple commands running. It is not SQL Editor which is designed to execute ONE command, it is a separate window in another menu - https://www.ibexpert.net/ibe/pmwiki.php?n=Doc.ScriptExecutive
Table creation command is described here: https://firebirdsql.org/file/documentation/reference_manuals/fblangref25-en/html/fblangref25-ddl-tbl.html
Basically what you trying to do looks like this, if to do it in one command:
CREATE TABLE bills (
id INTEGER NOT NULL,
code VARCHAR(25) NOT NULL,
dateOfGeneration DATE NOT NULL,
job_id INTEGER NOT NULL,
PRIMARY KEY ( id,job_id )
)
or if you insist on naming then perhaps
CREATE TABLE bills (
id INTEGER NOT NULL,
code VARCHAR(25) NOT NULL,
dateOfGeneration DATE NOT NULL,
job_id INTEGER NOT NULL,
CONSTRAINT bills_pk PRIMARY KEY ( id,job_id )
)

SQL Create Table Syntax Error on Constraint

While trying to create some tables with constraints I have stumbled on syntax errors. I am using Microsoft Access and it keeps advising me that my second Constraint is wrong. What is going on? My code looks as follows:
CREATE TABLE STORE
(
StoreName Char(25) NOT NULL,
City Char(35) NULL
Country Char(50) NULL,
Phone Char(8) NULL,
Fax Char(15) NULL,
Email Varchar(100) NULL,
Contact Char(35) NULL,
CONSTRAINT StorePK PRIMARY KEY(StoreName),
CONSTRAINT Citizen CHECK (Country IN ('Belize', 'United States', 'Mexico','China', 'Germany', 'France', 'Netherlands'))
);
I believe you have to create the table and the use ALTER TABLE to add the constraint.
ALTER TABLE STORE
CONSTRAINT Citizen CHECK (
Country IN (
'Belize', 'United States', 'Mexico','China',
'Germany', 'France', 'Netherlands'
)
);
I don't know how much the situation has changed since Access 2000 I'm pretty certain that some limitations still apply as indicated in this old documentation:
Note The check constraint statement can only be executed through the Jet OLE DB provider and ADO; it will return an error message if
used though the Access SQL View user interface.
https://msdn.microsoft.com/en-us/library/aa140015%28office.10%29.aspx#acintsql_ddlconst
Your CREATE TABLE statement is valid Access DDL when executed from ADO/OleDb. CHECK constraints are among the DDL features added with Jet 4, and which are not supported under DAO.
That also means CHECK is not supported by default for queries run from the query designer. You might be able to work around that limitation by setting the Access option "SQL Server Compatible Syntax (ANSI 92)". However that option has other side effects. If you use it, make sure to test your existing queries to see whether they all still operate as intended.
I put your statement text in a variable and executed it successfully like this:
CurrentProject.Connection.Execute strDDL
That worked because CurrentProject.Connection is an ADO object.

Can't create new table

I'm new to SQL Server 2000 and face a problem. I want to make a new table but I encounter an error message with the following code:
create table Buku
(
Kode_Buku char(5) constraint PK_Kode_Buku Primary Key,
Judul_Buku varchar(10)not null,
Nama_Pengarang varchar(30) not null,
Penerbit varchar(30),
Kota_Terbit varchar(30) default,
Tahun_Terbit varchar(4) default,
Bahasa varchar(4) check,
Harga_Jual money,
)
and here's the error code:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'Buku'.
You have three problems:
You have multiple cases where you say default but don't specify anything
You have a check but don't specify anything
Your last column definition says money, with a trailing comma
Now, none of these lead to the exact error message you're getting, so maybe there is more you're not telling us (is there more code before the create table bit?), but these little syntax problems are far too localized to be useful in this Q & A format.
EDIT
I just ran this on a SQL Server 2000 instance and it worked just fine:
create table Buku
(
Kode_Buku char(5) constraint PK_Kode_Buku Primary Key,
Judul_Buku varchar(10) not null, -- added space here
Nama_Pengarang varchar(30) not null,
Penerbit varchar(30),
Kota_Terbit varchar(30), -- removed default here
Tahun_Terbit varchar(4), -- removed default here
Bahasa varchar(4), -- removed check here
Harga_Jual money -- removed comma here
)
So I'm not sure what you're doing differently, but I can't get the error message you are seeing with the information you've provided in the question. If you're still getting an error message with this code (and only this code), you'll need to provide more information, such as ##VERSION, what interface you're using to submit the create table statement to SQL Server, etc.

How to set default value for all tables column in SQL Server?

How to set default value for all tables column in SQL Server?
If it is string it will empty and if it is int in already created table.
You can also do this in SQL server management studio, if you have designed you table using the GUI as opposed to creating it with a SQL statement.
To do this (off the top of my head), select the column you are editing, the properties window should be visible (if not i think f4 will open it). About half way down that you can set column defaults.
Add DEFAULT constraint
Here is the way to do it:
CREATE TABLE Persons
(
id int NOT NULL,
LName varchar(25) NOT NULL,
FName varchar(25),
Addr varchar(255),
City varchar(255) DEFAULT 'Hawaii'
)