In MySQL, I can use AUTO INCREMENT to generate unique IDs for my application’s customers. How do I get similar functionality when using CockroachDB?
Applications cannot use constructs like SEQUENCE or AUTO_INCREMENT and also expect horizontal scalability -- this is a general limitation of any distributed database. Instead, CockroachDB provides its own SERIAL type which generates increasing but not necessarily contiguous values.
For example, you would use:
CREATE TABLE customers (id SERIAL PRIMARY KEY, name STRING);
Then when you’re inserting values, you would use something like:
INSERT INTO customers (name) VALUES ('Kira Randell') RETURNING id;
This would return the randomly generated ID, which you’d be able to use elsewhere in your application
Related
I am using Oracle 12c and I have an IDENTITY column set as GENERATED ALWAYS.
CREATE TABLE Customers
(
id NUMBER GENERATED ALWAYS AS IDENTITY,
customerName VARCHAR2(30) NULL,
CONSTRAINT "CUSTOMER_ID_PK" PRIMARY KEY ("ID")
);
Since the ID is automatically from a sequence it will be always unique.
Do I need a PK on the ID column, and if yes, will it impact the performance?
Would an index produce the same result with a better performance on INSERT?
No, you don't need a primary key necessarily, but you should always provide the optimiser as much information about your data as possible - including a unique constraint whenever possible.
In the case of a surrogate key (like your ID), it's almost always appropriate to declare it as a Primary Key, since it's the most likely candidate for referential constraints.
You could use an ordinary index on ID, and performance of lookups will be comparable depending on data volume - but there is virtually no good reason in this case to use a non-unique index instead of a unique index - and there is no good reason in this case to avoid the constraint which will require an index anyway.
Yes, you always should have a Uniqueness constraint (with rare exceptions) on an ID column if indeed it is (and should be) unique, regardless of the method by which it is populated - whether the value is provided by your application code or via an IDENTITY column.
I'm trying to generate a sorted GUID as a primary key in Oracle. In SQL Server I could use one of the following to sort the rows physically
By clustered primary key as a unique identifier.
By NEWSEQUENTIALID.
I have searched for an Oracle equivalent but failed to find a solution. I know about Is there a way to create an auto-incrementing Guid Primary Key in an Oracle database?, but there's no indication whether SYS_GUID() is sorted.
How could I create a sequential primary key in Oracle?
If you want to create a GUID then SYS_GUID() is what you should be using, you can create this in a table as per the linked question. It's unclear from the documentation whether SYS_GUID() is incrementing. It might be but that's not really a statement that imparts trust.
The next part of your question (and some comments) keeps asking about clustered primary keys. This concept does not exist in the same way in Oracle as it does in SQL Server and Sybase. Oracle does have indexed organized tables (IOT) ...
... a table stored in a variation of a B-tree index structure... rows
are stored in an index defined on the primary key for the table. Each
index entry in the B-tree also stores the non-key column values. Thus,
the index is the data, and the data is the index.
There are plenty of uses for IOTs but it's worth bearing in mind you're altering the physical structure of the database on the disk for "performance reasons". You're doing the ultimate of all premature optimizations using something that has both negative and positive aspects. Read the documentation and be sure that this is what you want to do.
I would generally use an IOT only when you don't care about DML performance but when you do a lot of range scans, or you need to order by the primary key. You create an IOT in the same way as you would an ordinary table, but because everything you want is now part of the table everything goes in your table definition:
create table test_table (
id raw(32) default sys_guid()
, a_col varchar2(50)
, constraint pk_my_iot primary key (id)
) organization index;
It's worth noting that even with an IOT you must use an explicit ORDER BY in order to guarantee returned order. However, because of the way this is stored Oracle can table a few short cuts:
select *
from ( select *
from test_table
order by id )
where rownum < 2
SQL Fiddle.
As with everything, test, don't assume that this is the structure you want.
Oracle has as SYS_GUID() function, which generates a 16-byte RAW datatype. But, I'm not sure what you mean by "sorted GUID". Can you elaborate?
Do you mean you need each generated GUID to sort "after" the previously generated GUID? I looked at the SYS_GUID() function, and it seems to generate GUIDs in sorted order, but looking at the documentation, I don't see anything that says that is guaranteed.
If I understand your question correctly, I'm not sure it's possible.
You may be able to use SYS_GUID() and prepend a sequence, to get your desired sort order?
Can you explain more about your use case?
Adding the following in response to comment:
Ok, now I think I understand. What I think you want, is something called an IOT, or Index Organized Table, in Oracle. It's a table that has an index strucure, and all data is clustered, or grouped by the primary key. More information is available here:
http://docs.oracle.com/cd/E16655_01/server.121/e17633/indexiot.htm#CNCPT721
I think that should do what you want.
I have a table in Oracle Database which has 60 columns. Following is the table structure.
ID NAME TIMESTAMP PROERTY1 ...... PROPERTY60
This table will have many rows. the size of the table will be in GBs. But the problem with the table structure is that in future if I have to add a new property, I have to change the schema. To avoid that I want to change the table structure to following.
ID NAME TIMESTAMP PROPERTYNAME PROPERTYVALUE
A sample row will be.
1 xyz 40560 PROPERTY1 34500
In this way I will be able to solve the issue but the size of the table will grow bigger. Will it have any impact on performance in terms on fetching data. I am new to Oracle. I need your suggestion on this.
if I have to add a new property, I have to change the schema
Is that actually a problem? Adding a column has gotten cheaper and more convenient in newer versions of Oracle.
But if you still need to make your system dynamic, in a sense that you don't have to execute DDL for new properties, the following simple EAV implementation would probably be a good start:
CREATE TABLE FOO (
FOO_ID INT PRIMARY KEY
-- Other fields...
);
CREATE TABLE FOO_PROPERTY (
FOO_ID INT REFERENCES FOO (FOO_ID),
NAME VARCHAR(50),
VALUE VARCHAR(50) NOT NULL,
CONSTRAINT FOO_PROPERTY_PK PRIMARY KEY (FOO_ID, NAME)
) ORGANIZATION INDEX;
Note ORGANIZATION INDEX: the whole table is just one big B-Tree, there is no table heap at all. Properties that belong to the same FOO_ID are stored physically close together, so retrieving all properties of the known FOO_ID will be cheap (but not as cheap as when all the properties were in the same row).
You might also want to consider whether it would be appropriate to:
Add more indexes in FOO_PROPERTY (e.g. for searching on property name or value). Just beware of the extra cost of secondary indexes in index-organized tables.
Switch the order of columns in the FOO_PROPERTY PK - if you predominantly search on property names and rarely retrieve all the properties of the given FOO_ID. This would also make the index compression feasible, since the leading edge of the index is now relatively wide string (as opposed to narrow integer).
Use a different type for VALUE (e.g. RAW, or even in-line BLOB/CLOB, which can have performance implications, but might also provide additional flexibility). Alternatively, you might even have a separate table for each possible value type, instead of stuffing everything in a string.
Separate property "declaration" to its own table. This table would have two keys: beside string NAME it would also have integer PROPERTY_ID which can then be used as a FK in FOO_PROPERTY instead of the NAME (saving some storage, at the price of more JOIN-ing).
I have three oracle environments. If I make changes to the data in my DEV environment, I want to be able to take those changes and move them to my other environments.
The challenge is that the data in the other environments will not have the same primary key. So the compare would have to look at the other columns in the table. If the table referenced another table, it would have to include columns from that table as well.
So, for example, table A might have 4 columns that were all IDs to other tables, and so manipulating table A would require referring to those other tables. Can anyone suggest a product for this?
Updated Requirements: Nothing about the systems as they currently stand can be changed. IDs will never be in synch. The synchronization must only happen when desired (after our bi-monthly updates). Synchronization can not be done over DB links (though the DEV system can read data across the DB links). I can definitely write the SQL to do all this, its just the kind of thing that is prone to error (and typos) and if there is a 3rd party application out there that can take care of this for me, I'd rather spend the money on that.
Updates as per request: All systems are 11g Enterprise. The amount of data is very small, bandwidth isn't an issue. The synchronization happens every couple of months. Basically, we have rules that tell our UI how to behave. Those rules are stored in various tables. From time to time, we change those rules. This can be thousands of records, but not tens of thousands. We don't want the kind of synchronization that one might want from 'live' data. What I need to be able to do is specify the two database instances, and the tables in question, and then have a SQL script generated that, effectively, moves the changes from database A to database B. Given the nature of the data, I've been considering simply truncating the target tables and sqlloading the data into the other environments. That isn't quite as simple as it sounds as there is one 'live' table that refers to these records and that would require some specific updates potentially (I'm actively researching this possibility as well, it just isn't as simple as it sounds).
Here's code that will provide examples of what I mean. You'll notice IDs, and those are NOT consistent across environments, which is what makes this tricky.
drop table mydata;
drop table MYTOWN;
drop table MYJOB;
drop table MYEMPLOYER;
drop table mystate;
create table MYJOB
(MYJOBID varchar2(1000) NOT NULL PRIMARY KEY,
MYJOB varchar2(1000));
create table MYEMPLOYER
(MYEMPLOYERID varchar2(1000) NOT NULL PRIMARY KEY,
MYEMPLOYER varchar2(1000));
create table MYSTATE
(MYSTATEID varchar2(1000) NOT NULL PRIMARY KEY,
MYSTATE varchar2(1000));
create table MYTOWN
(MYTOWNID varchar2(1000) NOT NULL PRIMARY KEY,
MYTOWN varchar2(1000),
MYSTATEID varchar2(1000),
CONSTRAINT MYSTATE_FK FOREIGN KEY (MYSTATEID) REFERENCES MYSTATE (MYSTATEID) ENABLE);
create table MYDATA
(MYDATAID varchar2(1000) NOT NULL PRIMARY KEY,
MYTOWNID varchar2(1000),
MYJOBID varchar2(1000),
MYEMPLOYERID varchar2(1000),
CONSTRAINT MYTOWN_FK FOREIGN KEY (MYTOWNID) REFERENCES MYTOWN (MYTOWNID) ENABLE,
CONSTRAINT MYJOB_FK FOREIGN KEY (MYJOBID) REFERENCES MYJOB (MYJOBID) ENABLE,
CONSTRAINT MYEMPLOYER_FK FOREIGN KEY (MYEMPLOYERID) REFERENCES MYEMPLOYER (MYEMPLOYERID) ENABLE
);
create sequence mydataid_seq;
insert into myemployer values ('937436', 'Bank Of America');
insert into myemployer values ('43', 'Google');
insert into myemployer values ('2', 'Toms Taxi');
insert into myjob values ('8','Programmer');
insert into myjob values ('10','Cook');
insert into myjob values ('5','Driver');
insert into mystate values ('7643','MA');
insert into mystate values ('23','CA');
insert into mystate values ('54','NM');
insert into mytown values ('4743','BOSTON','7643');
insert into mytown values ('321','SANDIEGO','23');
insert into mytown values ('92037','SANTA FE','54');
insert into mydata values ('78','4743','8','937436');
insert into mydata values ('23455','321','10','43');
insert into mydata values ('901','92037','5','2');
--to select a unique row
select mt.mytown, ms.mystate, mj.myjob, me.myemployer
from mydata md, mytown mt, mystate ms, myemployer me, myjob mj
where md.mytownid=mt.mytownid
and mt.mystateid=ms.mystateid
and md.myjobid=mj.myjobid
and md.myemployerid=me.myemployerid;
--to delete a row
delete from mydata md where md.mydataid =
(select md.mydataid
from mydata md, mytown mt, mystate ms, myemployer me, myjob mj
where md.mytownid=mt.mytownid
and mt.mystateid=ms.mystateid
and md.myjobid=mj.myjobid
and md.myemployerid=me.myemployerid
and mt.mytown='SANDIEGO'
and ms.mystate='CA'
and mj.myjob='Cook'
and me.myemployer='Google');
--to insert a row
insert into mydata (mydataid,mytownid, myjobid, myemployerid)
(select mydataid_seq.nextval, mt.mytownid, mj.myjobid, me.myemployerid
from mytown mt, mystate ms, myemployer me, myjob mj
where mt.mytown='SANTA FE'
and mj.myjob='Programmer'
and me.myemployer='Toms Taxi'
and ms.mystate='NM'
);
Look at Oracle GoldenGate.
http://www.oracle.com/technetwork/middleware/goldengate/overview/index.html
May be it would be like a heavy gun solution.
The best solution depends on your concrete needs (how much data? how often? how much transformation must done? bandwidth between servers? their load? and so on...).
Oracle has many replication options.
Update:
Advise about GoldenGate is still actual, but it isn't cheap solution.
But, if you have some coding skills and interest, then it maybe easier to:
use db-link and write PLSQL packages with a syncing logic. Then, you can call it when you need, or run it regularly as Job/Task. Central database must see others and must have an ability to establish a direct connection.
or you can write an external application and place a syncing logic there. Databases could be in separate DMZs.
Of course, you can use Oracle Streams (11g enterprise includes it), but i can't guarantee that it would work without additional codding in your case. So, this approach can turn into first one with complications.
Sometimes, using serious replication solutions take more time for setting up them and maintain. In your case, you need just a syncing.
And you can find some kind of ready and free solutions. But, in most cases they suffer from poor quality. So, I don't recommend them.
PS:
You can solve inconsistence of data, by using two approaches:
Easiest: use oracle sequences with different start values for key columns for each database (don't forget to set up max value too, to prevent overlapping)
Normal solution: redesign your data model to include information about office/server
what is use-case of IOT (Index Organized Table) ?
Let say I have table like
id
Name
surname
i know the IOT but bit confuse about the use case of IOT
Your three columns don't make a good use case.
IOT are most useful when you often access many consecutive rows from a table. Then you define a primary key such that the required order is represented.
A good example could be time series data such as historical stock prices. In order to draw a chart of the stock price of a share, many rows are read with consecutive dates.
So the primary key would be stock ticker (or security ID) and the date. The additional columns could be the last price and the volume.
A regular table - even with an index on ticker and date - would be much slower because the actual rows would be distributed over the whole disk. This is because you cannot influence the order of the rows and because data is inserted day by day (and not ticker by ticker).
In an index-organized table, the data for the same ticker ends up on a few disk pages, and the required disk pages can be easily found.
Setup of the table:
CREATE TABLE MARKET_DATA
(
TICKER VARCHAR2(20 BYTE) NOT NULL ENABLE,
P_DATE DATE NOT NULL ENABLE,
LAST_PRICE NUMBER,
VOLUME NUMBER,
CONSTRAINT MARKET_DATA_PK PRIMARY KEY (TICKER, P_DATE) ENABLE
)
ORGANIZATION INDEX;
Typical query:
SELECT TICKER, P_DATE, LAST_PRICE, VOLUME
FROM MARKET_DATA
WHERE TICKER = 'MSFT'
AND P_DATE BETWEEN SYSDATE - 1825 AND SYSDATE
ORDER BY P_DATE;
Think of index organized tables as indexes. We all know the point of an index: to improve access speeds to particular rows of data. This is a performance optimisation of trick of building compound indexes on sub-sets of columns which can be used to satisfy commonly-run queries. If an index can completely satisy the columns in a query's projection the optimizer knows it doesn't have to read from the table at all.
IOTs are just this approach taken to its logical confusion: buidl the index and throw away the underlying table.
There are two criteria for deciding whether to implement a table as an IOT:
It should consists of a primary key (one or more columns) and at most one other column. (okay, perhaps two other columns at a stretch, but it's an warning flag).
The only access route for the table is the primary key (or its leading columns).
That second point is the one which catches most people out, and is the main reason why the use cases for IOT are pretty rare. Oracle don't recommend building other indexes on an IOT, so that means any access which doesn't drive from the primary key will be a Full Table Scan. That might not matter if the table is small and we don't need to access it through some other path very often, but it's a killer for most application tables.
It is also likely that a candidate table will have a relatively small number of rows, and is likely to be fairly static. But this is not a hard'n'fast rule; certainly a huge, volatile table which matched the two criteria listed above could still be considered for implementations as an IOT.
So what makes a good candidate dor index organization? Reference data. Most code lookup tables are like something this:
code number not null primary key
description not null varchar2(30)
Almost always we're only interested in getting the description for a given code. So building it as an IOT will save space and reduce the access time to get the description.