Ho can i define a integer auto increment with oracle 11g?This is my code with mysql user_id int(6) not null auto_increment primary key how can i have this line with oracle?Because i've already the same database in mysql now i want to build the same structure with oracle
You can achieve this with a sequence.
CREATE SEQUENCE seq_user;
The above will auto increment by 1, and start at 1.
To insert values using this sequence, you can do the following (for example):
INSERT INTO table_name (user_id) VALUES (seq_user.NEXTVAL);
To automate this process, you could reference the sequence in a trigger on the table, that adds this value on an insert automatically:
CREATE OR REPLACE TRIGGER user_trg
BEFORE INSERT ON table_name
FOR EACH ROW
DECLARE
BEGIN
IF(inserting)
THEN
:NEW.USER_ID := seq_user.NEXTVAL;
END IF;
END;
Related
I want to insert multiple rows in a table by user input in PL/SQL. This is my sql code: my table and my pl/sql block
CREATE TABLE tabletest
(
id NUMBER(4) PRIMARY KEY,
anytext VARCHAR2(20) NOT NULL
);
/
DECLARE
nrows INT := '&Input_N_Rows';
BEGIN
FOR n IN 1..nrows LOOP
INSERT INTO tabletest VALUES('&id', '&anytext');
END LOOP;
END;
But, it throws me an error
Error report -
ORA-00001: unique constraint (VENTA.SYS_C007513)
violated ORA-06512: at line 5
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
Please, help me.
This is my table as shown in image
This is my stored procedure for insert
but when I am trying to execute it with my project project shows error as below.
ERROR:
Cannot insert the value NULL into column 'SID', table 'AttDemo.dbo.StdMst'; column does not allow nulls. INSERT fails.
The statement has been terminated.
The easiest way to fix this would be to set the primary key to autoincrement / as an Identity.
Alternatively, you could change This:
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[STDMST_INSERT]
#STDNAME AS NVARCHAR (50)
AS
BEGIN
INSERT INTO (StdMst (StdName, EDate) VALUES(#STDNAME, GETDATE())
END
GO
To this:
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[STDMST_INSERT]
#STDNAME AS NVARCHAR (50)
AS
BEGIN
INSERT INTO (StdMst (SID, StdName, EDate) VALUES(IDENT_CURRENT('StdMst'), #STDNAME, GETDATE())
END
GO
The issue you are having is that your primary key does not allow NULL values and is not set to autoincrement.
Here are some useful links:
Set Primary Key as Identity
Setting key to autoincrement
Looks like you think that your SID column is an IDENTITY column, but it's not.
I have a page bank transfer that contains a column "transfer Number" in my asp.net application. This is an auto generated column. I am taking this value based based on the query:
IF (SELECT COUNT(1) FROM tableName) = 0
SELECT IDENT_CURRENT('tableName')
ELSE
SELECT IDENT_CURRENT('tableName') + 1
The problem is when two user login, it shows same transfer number for this page. How can I show different Transfer number for different users before inserting a record?
Thanks.
set the column as identity and the values will be generated automatically, and after insert, you can get the inserted id from the variable ##identity. Like this
Table Design
CREATE TABLE YourTable
(
SeqNo INT IDENTITY(1,1) PRIMARY KEY,
Col1 VARCHAR(50),
Col2 INT,
...
)
I have set the column SeqNo as Identity and Primary key.
Identity(1,1) means the first value will be 1 and then for each row the increment will be +1 and so on.
Now I insert a record to the table
INSERT INTO YourTable(Col1,Col2)
values('abc',1)
select ##identity
Now after the insert, the 2nd select will return me the value 1 as the value of the identity field is 1.
If I run this one more time, I will get 2 and so on
You can also call the System defined function SCOPE_IDENTITY() (in case triggers are involved) instead on ##IDENTITY
You can avoid conflicts using this since the values are generated by the database itself
I have a script below - I dont know if it will produce the same effect as auto increment. When i begin inserting rows into my database, i dont want to insert the id. I want the database to generate and insert them automatically when i insert non-id rows.
CREATE TABLE myschema.mytable
(id NUMBER PRIMARY KEY NOT NULL,
name VARCHAR2(30));
CREATE SEQUENCE myschema.test1_sequence
START WITH 1
INCREMENT BY 1;
create or replace trigger myschema.auto_increment
before insert on myschema.mytable
for each row
begin
select test1_sequence.nextval into :new.id from dual;
end;
/
Yes it will work, except that you don't have to use
id NUMBER PRIMARY KEY NOT NULL
because PRIMARY KEY already contains the NOT NULL constraint, so
id NUMBER PRIMARY KEY
is enough.
I want to update the schema of a db. I have copied the auto-generated script, but the last line after each table's script is this:
UPDATE "main"."sqlite_sequence" SET seq = 8 WHERE name = 'table';
The sec value is indeed correct for my installed DB, but it could vary on other installations. So, would it be safe to set it to 0, or should I select it from each installation's table? Or could I just skip this line and run the script without it?
If by "auto-generated" script you mean the full .dump of your database, then it will include the create table statements, and the insert statements, so you probably want the update to be executed along.
If you modify that auto-generated script, then you can obviously change the seq value as necessary.
Here is what the documentation has to say:
SQLite keeps track of the largest ROWID that a table has ever held
using the special SQLITE_SEQUENCE table. The SQLITE_SEQUENCE table is
created and initialized automatically whenever a normal table that
contains an AUTOINCREMENT column is created. The content of the
SQLITE_SEQUENCE table can be modified using ordinary UPDATE, INSERT,
and DELETE statements. But making modifications to this table will
likely perturb the AUTOINCREMENT key generation algorithm. Make sure
you know what you are doing before you undertake such changes.
In the end, you need to make sure that the seq value matches the highest value. This demonstrates:
sqlite> create table foo (a INTEGER PRIMARY KEY AUTOINCREMENT, b text);
sqlite> insert into foo values (NULL, 'blabla');
sqlite> select * from foo;
1|blabla
sqlite> .dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE foo (a INTEGER PRIMARY KEY AUTOINCREMENT, b text);
INSERT INTO "foo" VALUES(1,'blabla');
DELETE FROM sqlite_sequence;
INSERT INTO "sqlite_sequence" VALUES('foo',1);
COMMIT;