How to drop all graphs on AgensGraph? - agens-graph

I want to drop all graphs on agensgraph with drop database.
Is there query finding all graphs?
agens=# SELECT GRAPHNAME FROM ?????;
graphname
-----------
graph1
graph2

Use AG_GRAPH table for finding all graphs.
And drop graph one by one.
agens=# SELECT GRAPHNAME FROM AG_GRAPH;
graphname
-----------
graph1
graph2
graph
(3 rows)
agens=# DROP GRAPH GRAPH CASCADE;
NOTICE: drop cascades to 4 other objects
DETAIL: drop cascades to sequence graph.ag_label_seq
drop cascades to vlabel ag_vertex
drop cascades to elabel ag_edge
drop cascades to vlabel v
DROP GRAPH
agens=# DROP GRAPH GRAPH1 CASCADE;
NOTICE: drop cascades to 5 other objects
DETAIL: drop cascades to sequence graph1.ag_label_seq
drop cascades to vlabel ag_vertex
drop cascades to elabel ag_edge
drop cascades to vlabel v
drop cascades to elabel e
DROP GRAPH
agens=# DROP GRAPH GRAPH2 CASCADE;
NOTICE: drop cascades to 5 other objects
DETAIL: drop cascades to sequence graph2.ag_label_seq
drop cascades to vlabel ag_vertex
drop cascades to elabel ag_edge
drop cascades to vlabel v
drop cascades to elabel e
DROP GRAPH

Related

How to create unique constraints on AgensGraph

I want to use "id" property as primary key on label.
agens=# create vlabel v;
CREATE VLABEL
agens=# create (:v{id:1});
GRAPH WRITE (INSERT VERTEX 1, INSERT EDGE 0)
agens=# create (:v{id:1});
GRAPH WRITE (INSERT VERTEX 1, INSERT EDGE 0)
But, I don't know grammar for create constraint.
How to create unique constraints on AgensGraph.
Use unique property index on AgensGraph.
agens=# create (:v{id:1});
GRAPH WRITE (INSERT VERTEX 1, INSERT EDGE 0)
agens=# create unique property index on v(id);
CREATE PROPERTY INDEX
agens=# create (:v{id:1});
ERROR: duplicate key value violates unique constraint "v_id_idx"
DETAIL: Key ((properties.'id'::text))=(1) already exists.

How to drop "GRAPH" on AgensGraph?

I tried to drop graph. An error occurred when dropping graph.
# drop graph graph;
ERROR: cannot drop graph graph because other objects depend on it
DETAIL: sequence graph.ag_label_seq depends on schema graph
label ag_vertex depends on schema graph
label ag_edge depends on schema graph
HINT: Use DROP ... CASCADE to drop the dependent objects too.
How to drop "GRAPH"?
When dropping graph, all data & schema must be drop before.
For convenience, optional "CASCADE" clause is usable.
# drop graph graph cascade;
NOTICE: drop cascades to 3 other objects
DETAIL: drop cascades to sequence graph.ag_label_seq
drop cascades to label ag_vertex
drop cascades to label ag_edge
DROP GRAPH

Oracle drop constraint cascade equivalent in Sql Server

In Oracle, to drop the constraint PK_SAI I use the syntax:
ALTER TABLE "SAISIE"
DROP CONSTRAINT "PK_SAI" CASCADE;
What is the equivalent of this in SQL Server?
You are thinking of the CASCADE feature on FOREIGN KEY constraints, in relation to actual DELETE statements.
ALTER TABLE t2 add constraint FK_T2 foreign key(t_id) references t(id)
ON DELETE CASCADE;
Dropping a constraint with CASCADE does not delete any rows.
DELETE deletes rows, if you have enabled ON DELETE CASCADE.
Dropping the constraint simply drops the constraint (and associated indexes and dependent constraints), not data rows. In SQL Server ALTER TABLE ... I am not aware that there is a "CASCADE" option as in Oracle.
From Oracle docs http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_3001.htm#i2103845 for the ALTER TABLE statement:
CASCADE Specify CASCADE if you want all other integrity constraints that depend on the dropped integrity constraint to be dropped as well.

Alterations in views

Following is the code for sample table I have created:
SQL> create table a (a1 number(2), a2 number(2) not null);
Table created.
Now suppose I create a view on this table as:
SQL> create view aview as select a1 from a;
View created.
I need the opinions on 3 questions: a. Is there a way to insert values in this view. b. If I drop the view using drop view statement. Is there a way to recover like for tables using flashback? c. If I want to add constarint like primary key to this view it is not allowing me. I want to know why?This is what I tried:
SQL> alter view aview add primary key(a1);
alter view aview add primary key(a1)
*
ERROR at line 1:
ORA-00922: missing or invalid option
SQL> alter view aview add primary key a1;
alter view aview add primary key a1
*
ERROR at line 1:
ORA-00906: missing left parenthesis
I am using Oracle 11g.
You can add constraints to a view but the documentation says:
View Constraints
Oracle Database does not enforce view constraints. However, you can enforce constraints on views through constraints on base tables.
You can specify only unique, primary key, and foreign key constraints on views, and they are supported only in DISABLE NOVALIDATE mode. You cannot define view constraints on attributes of an object column.
So to create your primary key you'd need:
SQL> alter view aview add primary key(a1) disable novalidate;
view AVIEW altered.
or with a named constraint:
alter view aview add constraint aview_pk primary key(a1) disable novalidate;
But as it isn't enforced you can still have duplicate values in the view, and you can insert new duplicate values.
You can't insert directly into the view as the base table as a not-null constraint on a2, which you aren't providing. You'd get an ORA-01400. If you have a default value you can use for a2 then you could add that to the table definition:
create table a (a1 number(2), a2 number(2) default 0 not null);
create view aview as select a1 from a;
alter view aview add constraint aview_pk primary key(a1) disable novalidate;
insert into aview (a1) values (42);
select * from a;
A1 A2
---------- ----------
42 0
Or you could create an instead-of trigger, which is what you'd need to do anyway if the view was more complicated (with joins, for example):
create table a (a1 number(2), a2 number(2) not null);
create view aview as select a1 from a;
alter view aview add constraint aview_pk primary key(a1) disable novalidate;
create trigger aview_trig
instead of insert on aview
begin
insert into a values (:new.a1, 0);
end;
/
insert into aview (a1) values (42);
select * from a;
A1 A2
---------- ----------
42 0
But even with the primary key on the view you can do:
insert into aview (a1) values (42);
select * from a;
A1 A2
---------- ----------
42 0
42 0
If you want to the table to not allow duplicates you'd need a primary or unique key on that. If you want the table to have duplicates but the view to not sure them, make it distinct:
create view aview as select distinct a1 from a;
... but then the default value won't be enough to allow you to insert into the view (you'd get ORA-01732) and you would have to use an instead of trigger.
If you drop the view it is not held in the recycle bin:
drop view aview;
drop table a;
select type, original_name, object_name, operation from user_recyclebin;
TYPE ORIGINAL_NAME OBJECT_NAME OPERATION
----- ------------------ ------------------------------ ---------
TABLE A BIN$/KaYRJ66eC3gQwEAAH/46Q==$0 DROP
That only keeps objects that contain data. As the view has no data, it's just a predefined query, there is nothing to store really, and it's simple enough to recreate the view from its DDL - you aren't risking losing data as you would with a table drop.

In which cases will Oracle create indexes automatically?

As far as I know (this page) Oracle automatically creates an index for each UNIQUE or PRIMARY KEY declaration. Is this a complete list of cases when indexes are created automatically in Oracle?
I'll try to consolidate given answers and make it community wiki.
So indexes are automatically created by Oracle for such cases:
APC: For primary key and unique key unless such indexes already exist.
APC: For LOB storage and XMLType.
Gary: For table with a nested table.
Jim Hudson: For materialized view.
Firstly, Oracle does not always create an index when we create a primary or unique key. If there is already an index on that column it will use it instead...
SQL> create table t23 (id number not null)
2 /
Table created.
SQL> create index my_manual_idx on t23 ( id )
2 /
Index created.
SQL> select index_name from user_indexes
2 where table_name = 'T23'
3 /
INDEX_NAME
------------------------------
MY_MANUAL_IDX
SQL>
... note that MY_MANUAL_IDX is not a unique index; it doesn't matter ...
SQL> alter table t23
2 add constraint t23_pk primary key (id) using index
3 /
Table altered.
SQL> select index_name from user_indexes
2 where table_name = 'T23'
3 /
INDEX_NAME
------------------------------
MY_MANUAL_IDX
SQL> drop index my_manual_idx
2 /
drop index my_manual_idx
*
ERROR at line 1:
ORA-02429: cannot drop index used for enforcement of unique/primary key
SQL>
There is another case when Oracle will automatically create an index: LOB storage....
SQL> alter table t23
2 add txt clob
3 lob (txt) store as basicfile t23_txt (tablespace users)
4 /
Table altered.
SQL> select index_name from user_indexes
2 where table_name = 'T23'
3 /
INDEX_NAME
------------------------------
MY_MANUAL_IDX
SYS_IL0000556081C00002$$
SQL>
edit
The database treats XMLType same as other LOBs...
SQL> alter table t23
2 add xmldoc xmltype
3 /
Table altered.
SQL> select index_name from user_indexes
2 where table_name = 'T23'
3 /
INDEX_NAME
------------------------------
MY_MANUAL_IDX
SYS_IL0000556081C00002$$
SYS_IL0000556081C00004$$
SQL>
No, we're getting closer but that's not quite a complete list yet.
There will also be an index automatically created when you create materialized view since Oracle needs to be able to quickly identify the rows when doing a fast refresh. For rowid based materialized views, it uses I_SNAP$_tablename. For primary key materialized views, it uses the original PK name, modified as necessary to make it unique.
create materialized view testmv
refresh force with rowid
as select * from dual;
select index_name from user_indexes where table_name = 'TESTMV';
Index Name
--------------
I_SNAP$_TESTMV
And another one, if you create a table with a nested table you get an index created automatically. Object based storage in general can do this as there can be hidden tables created.
I think schema-based XMLTypes will also do it.
Yes, that's the complete list. Oracle automatically creates an index for each UNIQUE or PRIMARY KEY declaration.

Resources