Execute query in SQL Server Management Studio - sql

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.

Related

ORA-00907 (missing right parenthesis)

I'm trying to run this in my database but for some reason, I keep getting missing the right parenthesis. Thoughts?
CREATE TABLE PET_OWNER
(
OwnerID Int NOT NULL IDENTITY(1, 1),
OwnerLastName Char(25) NOT NULL,
OwnerFirstName Char(25) NOT NULL,
OwnerPhone Char(12) NULL,
OwnerEmail VarChar(100) NULL
CONSTRAINT OWNER_PK PRIMARY KEY(OwnerID)
);
EShirvana's answer directly answers the question you asked. However, I would suggest:
CREATE TABLE PET_OWNER (
OwnerID Int generated always as identity primary key,
OwnerLastName varchar2(25) NOT NULL,
OwnerFirstName varchar2(25) NOT NULL,
OwnerPhone varchar2(12),
OwnerEmail varchar2(100)
);
The differences:
The primary key constraint can be inlined. To be honest, I don't generally see much use for naming the primary key as a separate constraint (no harm).
Declaring a primary key column as NOT NULL is redundant. That is part of being a primary key.
Do you know what the char() data type does? It pads strings with spaces so they match the length. Use variable length strings -- and Oracle recommends varchar2() for this purpose.
By default, columns are NULLable. I actually find that explicitly declaring NULL is harder to read because I have to distinguish between NOT NULL and NULL which requires more work than just seeing NOT NULL.
2 issues:
identity and missing comma before constraint:
CREATE TABLE PET_OWNER(
OwnerID Int GENERATED ALWAYS AS IDENTITY NOT NULL,
OwnerLastName Char(25) NOT NULL,
OwnerFirstName Char(25) NOT NULL,
OwnerPhone Char(12) NULL,
OwnerEmail VarChar(100) NULL,
CONSTRAINT OWNER_PK PRIMARY KEY(OwnerID)
);

Issue with SQL create table error in Microsoft SQL Server

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.

Annoying error when trying to run SQL exported from db browser for sqlite

I am simply trying to run a basic SQL script to recreate a database.
The database was initially created in SQLite, and I exported it using DB Browser for SQLite.
The start of the file looks like this:
BEGIN TRANSACTION;
CREATE TABLE "AspNetUsers"
(
`Id` varchar(128) NOT NULL,
`Email` varchar(256) DEFAULT NULL,
`EmailConfirmed` tinyint(1) NOT NULL,
`PasswordHash` longtext,
`SecurityStamp` longtext,
`PhoneNumber` longtext,
`PhoneNumberConfirmed` tinyint(1) NOT NULL,
`TwoFactorEnabled` tinyint(1) NOT NULL,
`LockoutEndDateUtc` datetime DEFAULT NULL,
`LockoutEnabled` tinyint(1) NOT NULL,
`AccessFailedCount` int(11) NOT NULL,
`UserName` varchar(256) NOT NULL,
`IsActivated` tinyint(1) NOT NULL DEFAULT (0),
`Organisation` TEXT NOT NULL,
PRIMARY KEY(`Id`)
);
I created a new db and when running the query in SSMS I get this annoying error
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '`'.
I tried deleting all the whitespace between the first ( and 'Id' but then I just get
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '`'.
I also tried replacing the `s with 's but with the same result....
I'm pretty sure the server I'm trying to execute this on is running SQL Server Express - not sure if that makes a difference
Why must life be so difficult?
The code is rather specific to SQLite in several respects:
The use of backticks is non-standard.
Having a length for integer columns in non-standard.
text and longtext are non-standard.
The equivalent create table statement in SQL Server would be:
CREATE TABLE AspNetUsers (
Id varchar(128) NOT NULL,
Email varchar(256) DEFAULT NULL,
EmailConfirmed tinyint NOT NULL,
PasswordHash varchar(max),
SecurityStamp varchar(max),
PhoneNumber varchar(max),
PhoneNumberConfirmed tinyint NOT NULL,
TwoFactorEnabled tinyint NOT NULL,
LockoutEndDateUtc datetime DEFAULT NULL,
LockoutEnabled tinyint NOT NULL,
AccessFailedCount int NOT NULL,
UserName varchar(256) NOT NULL,
IsActivated tinyint NOT NULL DEFAULT (0),
Organisation varchar(max) NOT NULL,
PRIMARY KEY (Id)
);
Except for the varchar(max), this would be pretty standard for any database.
Some notes:
You probably don't need varchar(max) for any of these fields. Although you can use it, it looks awkward to have a phone number that could occupy megabytes of data.
You could probably replace the tinyints with bits.
DEFAULT NULL is redundant.

Create table does not work

I have a problem with creating a table in Maria DB Server.
I could successfully make the table on my other device, but for MYSQL (with probably different code).
Query:
CREATE TABLE admin (
'id' double(9999) NOT NULL auto_increment,
'username' VARCHAR(50) NOT NULL,
'passcode' VARCHAR(50) NOT NULL,
'email' VARCHAR(50) NOT NULL,
'state' VARCHAR(50) NOT NULL,
'points' double(9999) not null,
PRIMARY KEY(id)
);
Query Error:
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near "id' double(9999) NOT NULL auto_increment 'username' VARCHAR(50) NOT NULL, 'pa' at line 2
If any other information is required ask in comments.
Thanks
You were putting single quotes around the table names, which MariaDB was interpreting as string literals. Instead, just use the unquoted names directly:
CREATE TABLE admin (
id double(9999) NOT NULL auto_increment,
username VARCHAR(50) NOT NULL,
passcode VARCHAR(50) NOT NULL,
email VARCHAR(50) NOT NULL,
state VARCHAR(50) NOT NULL,
points double(9999) not null,
PRIMARY KEY(id)
);
If you do need to escape a column name in MariaDB/MySQL, e.g. because the name has whitespace or is a keyword, you can do so using backticks:
CREATE TABLE admin (
`id` double(9999) NOT NULL auto_increment,
`username` VARCHAR(50) NOT NULL,
...
)
But note that putting whitespace into column or table names, or using reserved keywords, is considered bad practice.
You could also put the PRIMARY KEY right after AUTO_INCREMENT
CREATE TABLE admin (
'id' double(9999) NOT NULL AUTO_INCREMENT PRIMARY KEY,
'username' VARCHAR(50) NOT NULL,
'passcode' VARCHAR(50) NOT NULL,
'email' VARCHAR(50) NOT NULL,
'state' VARCHAR(50) NOT NULL,
'points' double(9999) not null
);

Missing Right parenthesis error?

I'm trying to create a table in sqldeveloper however I keep getting a missing right parenthesis error when there are no missing right parenthises. Any fixes for this or am i just trying to create a table the wrong way?
CREATE TABLE Patient_T1(
PATIENT_ID INT(100) NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
DOB CHAR(10) NOT NULL,
P_STREET_ADRESS VARCHAR(50) NOT NULL,
PATIENT_CITY VARCHAR(30) NOT NULL,
PATIENT_STATE CHAR(2) NOT NULL,
PATIENT_ZIP CHAR(5) NOT NULL,
PATIENT_PHONE CHAR(12) NOT NULL,
PATIENT_ROOM INT(1000) NOT NULL,
CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID));
Not sure why Oracle gives that error message instead of something more helpful, but the cause is the precision applied to INT, switch from INT(100) and INT(1000) to just INT:
CREATE TABLE Patient_T1(
PATIENT_ID INT NOT NULL,
FIRST_NAME VARCHAR(20) NOT NULL,
LAST_NAME VARCHAR(30) NOT NULL,
DOB CHAR(10) NOT NULL,
P_STREET_ADRESS VARCHAR(50) NOT NULL,
PATIENT_CITY VARCHAR(30) NOT NULL,
PATIENT_STATE CHAR(2) NOT NULL,
PATIENT_ZIP CHAR(5) NOT NULL,
PATIENT_PHONE CHAR(12) NOT NULL,
PATIENT_ROOM INT NOT NULL,
CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID));
There are multiple issues with your table DDL:
INT(100) - In Oracle, an INTEGER is an ANSI SQL data type which refers to numeric values which have only an integer portion and no floating point or decimal part. That is, an INTEGER will only store whole numbers ONLY.
VARCHAR(20) - Oracle strongly recommends to use VARCHAR2.
From documentation,
VARCHAR Datatype
The VARCHAR datatype is synonymous with the VARCHAR2 datatype. To
avoid possible changes in behavior, always use the VARCHAR2 datatype
to store variable-length character strings.
CHAR(10) - better use VARCHAR2 as CHAR is blank-padded to the fixed length. That's a wastage of storage.
From documentation,
CHAR Datatype
The CHAR datatype stores fixed-length character strings. If you give a
shorter value, then the value is blank-padded to the fixed length.
Only the issue# 1 would throw an error, anyway fixing all the above issues would let you create the table.
For example,
SQL> CREATE TABLE Patient_T1
2 (
3 PATIENT_ID NUMBER NOT NULL,
4 FIRST_NAME VARCHAR2(20) NOT NULL,
5 LAST_NAME VARCHAR2(30) NOT NULL,
6 DOB DATE NOT NULL,
7 P_STREET_ADRESS VARCHAR2(50) NOT NULL,
8 PATIENT_CITY VARCHAR2(30) NOT NULL,
9 PATIENT_STATE VARCHAR2(2) NOT NULL,
10 PATIENT_ZIP VARCHAR2(5) NOT NULL,
11 PATIENT_PHONE VARCHAR2(12) NOT NULL,
12 PATIENT_ROOM NUMBER NOT NULL,
13 CONSTRAINT PATIENT_PK PRIMARY KEY(PATIENT_ID)
14 );
Table created.