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.
Related
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
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;
So I have two tables, I want to have a column on one table increment or decrement as items on another table are added or deleted, the link is the FOREIGN key.
I have two triggers but I'm not sure if they would work.
So I'd like some confirmation as to whether or not I'm barking up the right tree,if I'm going wrong or not an any fixes improvements?
SQL=
CREATE TABLE IF NOT EXISTS Agents(
Id INTEGER PRIMARY KEY,
Name TEXT,
Office_Count INT,
);
CREATE TABLE IF NOT EXISTS Branches(
Id INTEGER PRIMARY KEY,
Street_Address TEXT,
City TEXT,
Postcode TEXT,
Agents_Id INTEGER,
FOREIGN KEY(Agents_Id) REFERENCES Branches(Id)
);
CREATE TRIGGER Branches_Count_Increment AFTER INSERT ON Branches
BEGIN
UPDATE Agents SET
Office_Count=(MAX(Office_Count)+ 1 FROM Branches Where Agents_Id=Agents.Id) WHERE Id=NEW.Id;
END;
CREATE TRIGGER Branches_Count_Decrement AFTER DELETE ON Branches
BEGIN
UPDATE Agents SET
Office_Count=(MAX(Office_Count)- 1 FROM Branches Where Agents_Id=Agents.Id) WHERE Id=NEW.Id;
END;
You are barking in the general direction of an okay tree.
But the outer WHEREs use the wrong ID: the agents table must use an agent ID, which NEW.id is not.
And the MAX is not needed, and a subquery would need a SELECT.
UPDATE Agents
SET Office_Count = Office_Count + 1
WHERE Id = NEW.Agents_Id;
I have a table
CREATE TABLE "myTable" (
"id" INTEGER NOT NULL,
"name" VARCHAR,
PRIMARY KEY ("id")
)
and let's say it has 1 record
1 - James
I want to insert a new record. If it doesn't exist, insert it. If it does, do nothing.
I am not sure if the below query, is the right way to do this:
INSERT or IGNORE INTO myTable(id, name) VALUES(1, "Tom");
I tried it and I didn't get any error..
Your statement is fine for what you want to do. However, you don't need to supply a value for id in SQLite. You can just do:
INSERT INTO myTable(name)
VALUES('Tom');
This will auto-increment the id, so you don't have to worry about duplicates.
You used INSERT IGNORE, and the row won't actually be inserted because it results in a duplicate key. Your id column is a PRIMARY KEY, and you already have the value 1 stored in the database.
The statement won't generate an error but it will generate a warning.
i have a table called scanHistory that holds scanID and HostID as foreign key which are actually primary key to their respective tables. I want to write an insert query in SQLite to avoid inserting same values of scanID to its respective HostID meaning No two same hosts can have same scanID something Like:
ScanID - 100 HostID - 1
ScanID - 100 HostID - 2
ScanID - 200 HostID - 1
I tried this query
INSERT INTO scanHistory (NULL,4000,1) Select 1 WHERE NOT EXISTS (SELECT 1 from scanHistory);
But it's giving me error stating
Error: near "NULL": Syntax Error
what i am doing wrong? The table looks like this
CREATE TABLE scanHistory(
ScanHistoryID INTEGER PRIMARY KEY AUTOINCREMENT,
HostID INTEGER REFERENCES host(HostID),
ScanID INTEGER REFERENCES scan(ScanID));
How about just letting the database enforce it?
CREATE UNIQUE INDEX ix_scan_host ON ScanHistory(ScanID, HostID);
Now you can no longer insert duplicate HostID/ScanID pairs.
An SQLfiddle to test with.
If you just mean that ScanID needs to be unique, you can create the unique index just on that, and the database will enforce that no two rows with the same ScanID can be inserted;
CREATE UNIQUE INDEX ix_scan_host ON ScanHistory(ScanID);
If you want an error back when you try to insert a value, just use INSERT INTO... as usual. If you want to ignore the error, use INSERT OR IGNORE INTO....
EDIT: To do it using SQL only, you can use;
INSERT INTO ScanHistory (ScanID, HostID) SELECT 100, 1
WHERE NOT EXISTS (
SELECT 1 FROM ScanHistory WHERE ScanID=100 AND HostID=1
);