How to add Default SEQ.nextval() to PK-Column in SQL datamodeler - sql

I'm dealing with one issue.
Using sql datamodeler I tried to add an autoincrementing primary key column.
In sql it looks like PK_ID NUMBER(10) default seq_pk_id.nextval().
But how can I tell sql datamodoler to do that?
There's an autoincrement option in the relational view but it just add the creation of the sequence to the DDL not the default option on the primary key column.
Anyone any idea?

you can use a trigger for this column:
CREATE OR REPLACE TRIGGER SET_PK_ID
BEFORE INSERT ON Table_Name
FOR EACH ROW
BEGIN
SELECT seq_pk_id.nextval
INTO :new.PK_ID
FROM dual;
END;

Related

How to modify column to auto increment in PL SQL Developer?

I have created one table in PL SQL Developer.
CREATE TABLE Patient_List
(
Patient_ID number NOT NULL,
Patient_Name varchar(50) NOT NULL,
Patient_Address varchar(100) NULL,
App_Date date NULL,
Descr varchar(50),
CONSTRAINT patient_pk PRIMARY KEY(Patient_ID)
);
I want to auto increment Patient_ID, I tried altering the table and modifying the Patient_ID column but it's showing an error "invalid ALTER TABLE option"
ALTER TABLE Patient_List
MODIFY Patient_ID NUMBER NOT NULL GENERATED ALWAYS AS IDENTITY;
Please help, Thanks in advance.
This is not possible.
Oracle 10g didn't even have identity columns, they were introduced in Oracle 12.1
But even with a current Oracle version, you can't convert a regular column to an identity column. You would need to add a new one.
Before identity columns, the usual way was to create a sequence and a trigger to populate the column.
See here: How to create id with AUTO_INCREMENT on Oracle?
If anybody wants to modify existing column as auto_increment use this three lines
alter table Product drop column test_id;
create sequence Product_test_id_seq INCREMENT BY 1 nocache;
alter table Product add test_id Number default Product_test_id_seq.nextval not null;

Can I use tabs in a sql file run at OracleDB?

Ive got Following code:
CONSTRAINT PK_I_TBOWXXX PRIMARY KEY(I_RID) USING INDEX TABLESPACE Index_Data,
CONSTRAINT UK_I_TBOWXXX UNIQUE(I_CID, ID) USING INDEX TABLESPACE Index_Data
and i want it to fomat like this:
CONSTRAINT PK_I_TBOWXXX PRIMARY KEY (I_RID) USING INDEX TABLESPACE Index_Data,
CONSTRAINT UK_I_TBOWXXX UNIQUE (I_CID, ID) USING INDEX TABLESPACE Index_Data
can I use tabs for this without worrying about errors?
Or should i use spaces / should i just keep it like that?
It should work on 10g and 11g!
Abosultely.
oracle ignores all the additional spaces/tabs, so you can work on making your code look as neat as you want it to. :)
create table oats.testcreate
(
id number,
name varchar2(20)
);
Will work just just the same as..
create table oats.testcreate
(
id number,
name varchar2(20)
);
Although only one of them looks like the code you can understand :)

SQL Date option

I have a little doubt, I want to create a table that had a date that can't be bigger the 2012/12/31, i searched on google but only had exemples on SELECT. I'm gonna put an example:
CREATE TABLE example(
IDExample number (8) primary key,
DateExample date // Here i want to put that condition, is it possible?
);
If you're using SQL Server you can add check contraint on the column in the following way.
ALTER TABLE dbo.example ADD CONSTRAINT CK_DateExample
CHECK (DateExample < '20130101')
If you're using Oracle, the syntax is very similar:
ALTER TABLE dbo.example ADD CONSTRAINT CK_DateExample
CHECK (DateExample < DATE '2013-01-01')

SQL unique index without leading zeros

I have set-up a table using the following SQL script:
CREATE TABLE MY_TABLE (
ID NUMBER NOT NULL,
CODE VARCHAR2(40) NOT NULL,
CONSTRAINT MY_TABLE PRIMARY KEY (ID)
);
CREATE UNIQUE INDEX XUNIQUE_MY_TABLE_CODE ON MY_TABLE (CODE);
The problem is that I need to ensure that CODE does not have a leading zero for its value.
How do I accomplish this in SQL so that a 40-char value without a leading zero is stored?
CODE VARCHAR2 NOT NULL CHECK (VALUE not like '0%')
sorry - slight misread on the original spec
If you can guarantee that all INSERTs and UPDATEs to this table are done through a stored procedure, you could put some code there to check that the data is valid and return an error if not.
P.S. A CHECK CONSTRAINT would be better, except that MySQL doesn't support them.

Replace into equivalent for postgresql and then autoincrementing an int

Okay no seriously, if a PostgreSQL guru can help out I'm just getting started.
Basically what I want is a simple table like such:
CREATE TABLE schema.searches
(
search_id serial NOT NULL,
search_query character varying(255),
search_count integer DEFAULT 1,
CONSTRAINT pkey_search_id PRIMARY KEY (search_id)
)
WITH (
OIDS=FALSE
);
I need something like REPLACE INTO for MySQL. I don't know if I have to write my own procedure or something?
Basically:
check if the query already exists
if so, just add 1 to the count
it not, add it to the db
I can do this in my php code but I'd rather all that be done in postgres C engine
You have to add a unique constraint first.
ALTER TABLE schema.searches ADD UNIQUE (search_query);
The insert/replace command looks like this.
INSERT INTO schema.searches(search_query) VALUES ('a search query')
ON CONFLICT (search_query)
DO UPDATE SET search_count = schema.searches.search_count + 1;