Auto increment number - sql

Can you any one tell me how to generate auto number in SQL? I have tried but it shows the following error
"Incorrect sytnax near AUTO_INCREMENT".
What am I doing wrong?
create table auto1
(
Sno int NOT NULL AUTO_INCREMENT,
fname varchar(50)
)

I assume you are using SQL-Server where the syntax is different compared to other DB engines
create table auto1
(
Sno int NOT NULL IDENTITY(1,1),
fname varchar(50)
)
See here for details on table creation.

please try below one
CREATE TABLE test (
sno INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
fname VARCHAR( 20 ) NOT NULL
)

Related

SQL error while creating a new table, what am I missing

create table paydetails (
emp_id integer (20),
dept_id integer (20),
basic integer(20),
deductions integer(20),
additions integer (20),
joining_date date(20,20,20)
);
This should work in almost any database:
create table paydetails (
emp_id int,
dept_id int,
basic int,
deductions int,
additions int,
joining_date date
);
Most databases do not have a parameter for int (although some interpret that as a length).
As far as I know, none have three parameters for date.
This will work for you. Do yourself the favor of adding in the auto increment id as your unique/primary key.
CREATE TABLE `db_name`.`paydetails` (
`id` INT(20) NOT NULL AUTO_INCREMENT ,
`emp_id` INT(20) NOT NULL ,
`dept_id` INT(20) NOT NULL ,
`basic` INT(20) NOT NULL ,
`deductions` INT(20) NOT NULL ,
`additions` INT(20) NOT NULL ,
`joining_date` DATE NOT NULL ,
PRIMARY KEY (`id`)) ENGINE = MyISAM;

Insert into a new Table in SQL gives error

Why am I getting an error?
--Q1
CREATE table Aorders (
Id INT IDENTITY NOT NULL,
CreatedDate Date NOT NULL,
BillingCountry varchar Not Null,
MerchId int Not null,
OrderStatus varchar Not null,
);
--Q2
select * from Aorders
--Q3 edited as suggested here but still get an error
INSERT INTO Aorders (CreatedDate, BillingCountry, MerchId, OrderStatus)
VALUES ('2001-01-01', 'Israel', 5 ,'Approved');
You must define the length of the varchar columns like:
CREATE table Aorders (
Id INT IDENTITY NOT NULL,
CreatedDate Date NOT NULL,
BillingCountry varchar(100) Not Null,
MerchId int Not null,
OrderStatus varchar(100) Not null,
);
From char and varchar (Transact-SQL):
When n isn't specified in a data definition or variable declaration
statement, the default length is 1
When doing an insert, the best practice is to list all the columns explicitly and to use standard formats for dates:
INSERT INTO Aorders (CreatedDate, BillingCountry, MerchId, OrderStatus)
VALUES ('2001-01-01', 'Israel', 5 ,'Approved');
Your specific error is because you have four values in the VALUES() list but there are five columns in the table.
You have not done your query properly. I think you want the ID to auto increment ,but you haven't declare that. Make changes as below:
CREATE table Aorders ( Id INT IDENTITY NOT NULL
,CreatedDate Date NOT NULL
,BillingCountry varchar Not Null
,MerchId int Not null
,OrderStatus varchar Not null);
Now enter the query as you have inserted. And also make sure proper date formats from documentation.

I got error on column with identity(1,1)?

Sql developer gives an error of:
ORA-00907: missing right parenthesis, for next table :
create table customer (
CUSTOMER_ID number identity (1,1) not null,
CUSTOMER_PHONE char(13) not null
);
Also the 'identity is red underlined, have no idea why
Proper syntax for Oracle 12c and newer:
create table customer (
CUSTOMER_ID NUMBER GENERATED ALWAYS AS IDENTITY,
CUSTOMER_PHONE char(13) not null
);
-- explicit start and increment
create table customer (
CUSTOMER_ID NUMBER GENERATED ALWAYS AS IDENTITY(START WITH 1 INCREMENT BY 1),
CUSTOMER_PHONE char(13) not null
);
db<>fiddle demo

Create SQL Function view with User ID and Date range

As I title says, I need to create a Function View of two tables.
Below are the SQL Tables
CREATE TABLE User_Specialist(
ID_User_Specialist INT NOT NULL,
Name_User_Specialist VARCHAR(50) NOT NULL,
CONSTRAINT PK_ID_User_Specialist PRIMARY KEY(ID_User_Specialist),
GO
CREATE TABLE Incident(
ID_Incident INT IDENTITY(1,1) NOT NULL,
Incident_Creation_Date DATETIME NULL,
Assigned_Specialist INT NULL,
CONSTRAINT FK_Assigned_Specialist FOREIGN KEY (Especialista_Asignado) REFERENCES Usuario_Especialista(ID_Usuario_Especialista),
GO
Based on the previous information, I need the function to display Assign Specialist and the dates that the Incident was created.
Right know this is what I got:
CREATE FUNCTION View_Date (#ID_User_Incident INT)
RETURNS INT
AS
BEGIN
DECLARE #Total_Incidents INT
SELECT #Total_Incidents = COUNT(ID_Incident)
FROM Incidents i, User_Specialist u
WHERE i.ID_Incident = u.ID_User_Specialist AND u.ID_User_Specialist =#ID_User_Incident
RETURN (#Total_Incidents)
END
GO
DECLARE #Specialist_ID int;
EXEC #Specialist_ID = [dbo].View_Date
#ID_Incidentes_Usuarios = 5;
SELECT #Specialist_ID AS 'Assigned Specialist Incidents'
GO
The only thing missing is the dates range.
I believe you changed your table and field names to English from Spanish. You have missed some of them and there were some missing parenthesis. As much as I understand I changed them to understand better.
CREATE TABLE User_Specialist
(
ID_User_Specialist INT NOT NULL,
Name_User_Specialist VARCHAR(50) NOT NULL
CONSTRAINT PK_ID_User_Specialist PRIMARY KEY(ID_User_Specialist)
)
GO
CREATE TABLE Incident
(
ID_Incident INT IDENTITY(1,1) NOT NULL,
Incident_Creation_Date DATETIME NULL,
Assigned_Specialist INT NULL
CONSTRAINT FK_Assigned_Specialist FOREIGN KEY (Assigned_Specialist) REFERENCES User_Specialist(ID_User_Specialist)
)
GO
If you want to see multiple columns or rows as result you need to use Table-valued Function. I have created sample query from your table and prepared function. you can change it with what you want.
CREATE FUNCTION dbo.FN_ViewDate
(
#ID_User_Incident INT
)
RETURNS #Result TABLE
(
TotalIncidents INT
,FirstIncidentDate DATETIME
,LastIncidentDate DATETIME
)
AS
BEGIN
INSERT INTO #Result
SELECT COUNT(ID_Incident)
,MIN(Incident_Creation_Date)
,MAX(Incident_Creation_Date)
FROM Incident I
LEFT JOIN User_Specialist U ON I.Assigned_Specialist = U.ID_User_Specialist
WHERE I.Assigned_Specialist = #ID_User_Incident
RETURN
END
GO
I think this query can help you.
declare #count int = 0 , #dates varchar(200) =''
SELECT #count+=1 , #dates +=' '+ i.Incident_Creation_Date
FROM Incidents i, User_Specialist u
WHERE i.ID_Incident = u.ID_User_Specialist AND u.ID_User_Specialist =#ID_User_Incident
select #count as [count] ,#dates [incidentDates]

Why Auto_Increment not working in SQL?

CREATE TABLE Persons
(
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
When I run this code, it is not run successfully.
Syntax for MySQL
CREATE TABLE Persons (
ID int NOT NULL AUTO_INCREMENT,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);
Syntax for SQL Server
CREATE TABLE Persons (
ID int IDENTITY(1,1) PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
Syntax for Oracle
CREATE SEQUENCE seq_person
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10;
If this is Microsoft SQL Server then change to IDENTITY instead of Auto_Number. If it's Postgres then I believe you want SERIAL as the datatype instead of INT. If this is MySQL then it should work as-is.
What RDBMS are you using?