Incorrect syntax in SQL Server query - sql

CREATE TABLE identity (
empid VARCHAR(255) PRIMARY KEY,
entry VARCHAR(255)
);
-- Table: vectors
CREATE TABLE vectors (
f_id INTEGER PRIMARY KEY IDENTITY(1,1),
label STRING NOT NULL,
empid STRING REFERENCES identity (empid)
NOT NULL,
vector BLOB NOT NULL
);
I tried to run the above query but it gives me error
Incorrect syntax near expected '.', ID or QUOTED_ID.
I don't understand why it is giving me this error, is it because IDENTITY is a keyword in SQL Server. Kindly help!

Try this:
CREATE TABLE [identity] (
empid VARCHAR(255) PRIMARY KEY,
entry VARCHAR(255)
);
-- Table: vectors
CREATE TABLE vectors (
f_id INTEGER PRIMARY KEY IDENTITY(1,1),
label VARCHAR(255) NOT NULL,
empid VARCHAR(255) REFERENCES [identity] (empid)
NOT NULL,
vector VARCHAR(255) NOT NULL
);
Also, if you are working with SQL Server Management Studio, you can see that some words are colored in blue. It will be better to avoid them using in your code as names of tables, variables and other objects. For example, identity is such word.

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
);

SQL column maximum input values

hi please how do i give sql column maximum input example if i want max_num column to take only three(3) result sets or inputs
It's mostly the same in other rdbms. You need to specify right after the column type
MYSQL
CREATE TABLE TestTable(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
three_char_demo VARCHAR(3) NOT NULL,
)
PostgreSQL
CREATE TABLE TestTable(
ID INT PRIMARY KEY NOT NULL,
three_char_demo CHAR(3) NOT NULL,
);

SQL auto increment pgadmin 4

I am trying to make a simple database with an number generator but why do I get the error below?
ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 2: IDNumber int NOT NULL AUTO_INCREMENT,
Code:
CREATE TABLE Finance
(
IDNumber int NOT NULL AUTO_INCREMENT,
FinName varchar(50) NOT NULL,
PRIMARY KEY(IDNumber)
);
The subject of the question mentions pgAdmin 4, so here's how to do it there.
First, add a column to your table, then click the little edit icon:
Then go to Constraints and select the Identity type:
This generates SQL similar to this:
CREATE TABLE public.my_table_name
(
id integer NOT NULL GENERATED ALWAYS AS IDENTITY,
PRIMARY KEY (id)
);
For Postgres you have to use SERIAL
CREATE TABLE Finance
(
IDNumber SERIAL PRIMARY KEY,
FinName varchar(50) NOT NULL
);
IN pgadmin-2.
step 01:
create seq:
and set info:
step 02: go to ID in table and set constraints
if it is sql the syntax is the following
CREATE TABLE Persons (
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
You are using a MySQL syntax which won't work in SQL Server.
CREATE TABLE Finance
(
IDNumber int NOT NULL IDENTITY(1,1) PRIMARY KEY,
FinName varchar(50) NOT NULL
);
The following code should work for SQL Server.
IDENTITY(1,1) is SQL Server's way of saying "auto increment".
The starting value for IDENTITY is 1, and it will increment by 1 for each new record.
So : IDENTITY(startvalue, incrementvalue)
there are two options; one is to use the "datatype" serial or create a sequence and use this sequence as a default value for your integer as follows:
CREATE SEQUENCE your_seq;
CREATE TABLE foo(
id int default nextval('your_seq'::regclass),
other_column TEXT);
INSERT INTO foo(other_column) VALUES ('bar') RETURNING *;
It is important to note that specifying a table as follows:
CREATE TABLE tablename (
colname SERIAL);
is equivalent to:
CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq'));
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
More information about serial can be found here.

create table in Oracle BD but gives error

CREATE TABLE employees (
id INT NOT NULL auto_increment PRIMARY KEY (ID),
first_name VARCHAR(20) DEFAULT NULL,
last_name VARCHAR(20) DEFAULT NULL,
salary INT DEFAULT NULL);
I think this is correct query to create table in Oracle database.. but it gives the following error:
ORA-00907: missing right parenthesis
How to correct the statement?
You can validate your SQL using formatting tools such as http://www.dpriver.com/pp/sqlformat.htm
auto_increment seems like a proprietary MySQL extension, so it's not valid for Oracle.
also, "id int not null auto_increment primary key (id)" does not need the last "(id)"
Using Oracle, you shoud try something like this
CREATE SEQUENCE seq;
CREATE TABLE employees
(
id INTEGER NOT NULL PRIMARY KEY,
first_name VARCHAR2(20) DEFAULT NULL,
last_name VARCHAR2(20) DEFAULT NULL,
salary INTEGER DEFAULT NULL
);
INSERT INTO employees
VALUES (seq.NEXTVAL,
'name',
'last name',
1);
Sometimes, SQL is fancy, because even having a standard (ANSI), most DBMS vendors add their proprietary extensions to the SQL creating their own languages, so it's rare the situation where you can port one SQL from one DB into another without any changes.
Also, it's a pretty useless error message. It could at least say which position. (also, there's no missing parenthesis, but an unexpected token)
EDITED : New feature 12c
CREATE TABLE employees(
id NUMBER GENERATED ALWAYS AS IDENTITY,
first_name VARCHAR2(30)
etc.
);
Why would you do default null?
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.
Replace
id INT NOT NULL auto_increment PRIMARY KEY (ID),
with
id INT NOT NULL auto_increment PRIMARY KEY,
this is more efficient
CREATE TABLE EMPLOYEES_T(
ID NUMBER,
FIRST_NAME VARCHAR2(20) DEFAULT NULL,
LAST_NAME VARCHAR2(20) DEFAULT NULL,
SALARY INTEGER DEFAULT NULL,
CONSTRAINT PK_EMPLOYEES_T PRIMARY KEY(ID)
);

SQL Server : create error how to write database name with the table name

CREATE DATABASE agom COLLATE Arabic_CI_AS
CREATE TABLE Branches
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
NAME VARCHAR(255) NOT NULL
)
CREATE TABLE agom.Brands
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
NAME VARCHAR(255) NOT NULL
)
CREATE TABLE agom.Work_Order
(
NUMBER INT NOT NULL,
BRANCHID INT NOT NULL,
BRANDID INT NOT NULL,
WDATE DATE NOT NULL,
REPAIRSTATUS VARCHAR(255),
REPAIRCOST VARCHAR(255),
REMARK VARCHAR(500),
PRIMARY KEY (NUMBER,BRANCHID,BRANDID)
)
CREATE TABLE agom.Profiles
(
ID INT PRIMARY KEY NOT NULL IDENTITY,
USERNAME VARCHAR(25) NOT NULL,
PASS VARCHAR(25) NOT NULL
)
ALTER TABLE agom.Work_Order
ADD CONSTRAINT branchfk
FOREIGN KEY (BRANCHID) REFERENCES Branches(ID)
ALTER TABLE agom.Work_Order
ADD CONSTRAINT brandfk
FOREIGN KEY (BRANDID) REFERENCES Brands(ID)
I get an error that cannot create table
I try to write database name with the table name db.tablename but it's not working
I need to create the database then create the tables and its constraints but I don't know where is the error.I am a sql noob
It's never Database.Table.
It's either Table, or Schema.Table, or Database.Schema.Table, or Server.Database.Schema.Table.
You probably just want to insert USE agom right after create database, and then only refer to tables by name.
Alternatively, you can refer to your tables as agom.dbo.Work_Order, dbo being the default database schema.
See Using Identifiers As Object Names for general reference.