Issue with SQL create table error in Microsoft SQL Server - sql

Can someone help me find the reason this SQL query isn't working in Microsoft SQL Server?
CREATE TABLE USER
(
USER_ID int NOT NULL PRIMARY KEY,
USER_L_NAME varchar(30) NOT NULL,
USER_F_NAME varchar(20) NOT NULL,
PASSWORD varchar(20) NOT NULL,
USERNAME varchar(20) NOT NULL,
DOB date NOT NULL,
COUNTRY char(30) NOT NULL,
STATE_REGION char(2),
EMAIL varchar(20) NOT NULL,
STREET varchar(50) NOT NULL,
CITY varchar(30),
ZIP_CODE char(5),
MOBILE_PHONE char(11)
);

USER is a reserved word in SQL Server - as in most other databases.
You can either quote this identifier by surrounding it with square brackets (CREATE TABLE [USER] (...)), or change the table name to something else - such as USERS for example.
I would recommend the latter. Using reserved words for identifiers is error-prone (you need to quote the identifier everywhere you use it later on), and can easily be avoided.

Related

Alternative to Postgresql BIGSERIAL data type in Azure Database?

I am learning Azure and data analytics with Azure. Recently finished learning Postgresql.
My question is if there is an alternative to BIGSERIAL data type for Azure Databases. I ran the query (below the error in the following) and had an error. Note that this datatype exists in Postgresql and hence I am getting confused in Azure. Any alternative to BIGSERIAL?
Failed to execute the query. Error: Column, parameter, or variable #1:
Cannot find data type BIGSERIAL.
create table person (
ID BIGSERIAL NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(50),
gender VARCHAR(50) NOT NULL,
date_of_birth DATE NOT NULL,
Country_of_birth VARCHAR(50) NOT NULL
);
In PostgreSQL, the SERIAL keyword is used to setup an auto increment column, this works similar to auto increment in SQL. BIGSERIAL is an auto-incremented Bigint column of 8 bytes.
Closest, I could find "bigserial"in MS docs is as here
So...you can use BIGINT instead, below works fins for me.
create table person (
ID BIGINT NOT NULL PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
email VARCHAR(50),
gender VARCHAR(50) NOT NULL,
date_of_birth DATE NOT NULL,
Country_of_birth VARCHAR(50) NOT NULL
);

Unable to Run a query on IBM db2

I have just started with IBM db2 and am running an sql query which I suppose is right. But nothing is happening when I hit the run button. Could anyone help?
Create table INSTRUCTOR
CREATE TABLE INSTRUCTOR
(ins_id INTEGER PRIMARY KEY NOT NULL,
lastname VARCHAR(15) NOT NULL,
firstname VARCHAR(15) NOT NULL,
city VARCHAR(15),
country CHAR(2)
);
Under the result area it is displaying "waiting" and nothing actually happens.
Try
CREATE TABLE INSTRUCTOR
( ins_id INTEGER PRIMARY KEY NOT NULL,
lastname VARCHAR(15) NOT NULL,
firstname VARCHAR(15) NOT NULL,
city VARCHAR(15),
country CHAR(2)
);
That should work for you assuming that you have ; set as the statement terminator in your query interface (it is the default in most SQL interfaces for Db2).
Alternatively, it might be that your CREATE TABLE is waiting for a lock on the system catalog. You can look at currently held lock with MON_GET_LOCKS https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.rtn.doc/doc/r0056428.html

what does the error "missing right parenthesis" in oracle sql means

I'm trying to run this code and it seems correct to me but I'm getting an error stating that there's a right parenthesis missing.
The code is the following:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
DATE_OF_BIRTH DATE,
PHONE_NB CHAR(8) NOT NULL,
ADDRESS VARCHAR(50),
TOTAL_SPENDING FLOAT NOT NULL DEFAULT 0.0,
PRIMARY KEY(CUSTOMER_ID)
);
Can anyone help me in solving my problem?
Since you tagged SQL Developer...
...the tool tries to give you a heads-up there will be a problem before you even hit the Execute button
The default value for the column is confusing the parser because it's not expected at that point.
Move it to after the data type and you'll be good
CREATE TABLE customer (
customer_id INT NOT NULL,
name VARCHAR2(30) NOT NULL,
date_of_birth DATE,
phone_nb CHAR(8) NOT NULL,
address VARCHAR(50),
total_spending FLOAT DEFAULT 0.0 NOT NULL,
PRIMARY KEY ( customer_id )
);
PS In oracle, use VARCHAR2, not VARCHAR. While VARCHAR will 'work', it's reserved and could mean something different in a future release.
You are using wrong order of column definition clauses: the constraint (NOT NULL) should follow the default value.
This is the right way:
CREATE TABLE CUSTOMER
(
CUSTOMER_ID INT NOT NULL,
NAME VARCHAR(30) NOT NULL,
DATE_OF_BIRTH DATE,
PHONE_NB CHAR(8) NOT NULL,
ADDRESS VARCHAR(50),
TOTAL_SPENDING FLOAT DEFAULT 0.0 NOT NULL ,
PRIMARY KEY(CUSTOMER_ID)
);

Sql syntax error - expecting ( or as

I am trying to write this SQL code:
create table Users
{
UserID int primary key identity(200,1),
FirstName varchar(20) not null,
LastName varchar(20) not null,
BirthDate dateTime not null,
HomeTown varchar(30),
WorkPlace varchar(40),
Email not null
}
The problem is that next the { symbol, I get the error:
Incorrect syntax near '{'.
When my mouse over the sign it adds:
Expecting '(' or AS
In addition, I also get an error on the values that are in the bracket
Incorrect syntax near '20'. Expecting '(' or Select".
The thing is that I have another SQL document (that I didn't write) and the same syntax work there! Why is that and how can I solve it?
You need brackets not braces - http://www.w3schools.com/sql/sql_create_table.asp Also a data type for email
I.e.
create table Users
(
UserID int primary key identity(200,1),
FirstName varchar(20) not null,
LastName varchar(20) not null,
BirthDate dateTime not null,
HomeTown varchar(30),
WorkPlace varchar(40),
Email varchar(40) not null
)
You have not specified the datatype for Email columnn.
Use ()instead of {}.
Your sql statement should look like the following one, first change the {} to () then added the datatype to your email column
create table Users (
UserID int primary key identity(200,1),
FirstName varchar(20) not null,
LastName varchar(20) not null,
BirthDate dateTime not null,
HomeTown varchar(30),
WorkPlace varchar(40),
[Email] varchar(255) not null
)

Execute query in SQL Server Management Studio

This very simple query I am trying to execute in SQL Server Management Studio. But it gives error:
CREATE TABLE `contact` (
`contact_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`email` varchar(45) NOT NULL,
`address` varchar(45) NOT NULL,
`telephone` varchar(45) NOT NULL,
PRIMARY KEY (`contact_id`)
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8
Error is:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '`'.
I also tried by replacing with'` but still no help
The backtick (`) character is only used for MySql. It's not standard. Sql Server is a completely different animal. You need to use square-brackets ([ and ] -- also not standard) to enclose table and column names with Sql Server. Sql Server also supports double quotes ("), which are part of the SQL Standard, but for some reason less-common.
While I'm at it, the ENGINE and AUTO_INCREMENT, and CHARSET clauses are also MySql-specific.
Try this:
CREATE TABLE contact (
contact_id int IDENTITY(25,1) NOT NULL PRIMARY KEY,
[name] varchar(45) NOT NULL,
email varchar(45) NOT NULL,
"address" varchar(45) NOT NULL,
telephone varchar(45) NOT NULL
)
in tsql
CREATE TABLE contact (
contact_id int identity(0,25) NOT NULL,
name nvarchar(45) NOT NULL,
email nvarchar(45) NOT NULL,
address nvarchar(45) NOT NULL,
telephone nvarchar(45) NOT NULL
CONSTRAINT contact_PK PRIMARY KEY (contact_id)
)
You can't specify engine.
identity(0,25) means initial value = 0, increment = 25
You don't specify character set for the table, you can declare individual columns as varchar or nvcarchar -- the n stands for unicode. You can also specify a collation sequence for each column.
If you want a column name that is irregular (keyword, embedded space, etc.) you quote the column name as [column name] -- don't use irregular column names if you have a choice, it just makes things a pain to use.
Try like this:
CREATE TABLE contact (
contact_id[int] IDENTITY(1,1) NOT NULL,
name varchar(45) NOT NULL,
email varchar(45) NOT NULL,
address varchar(45) NOT NULL,
telephone varchar(45) NOT NULL,
PRIMARY KEY (contact_id)
)
Hope this may help you.