Finding key constraints in MonetDB - monetdb

I am trying to get the key constraints on the table name 'meta'. But the information schema doesn't work and I am new to MonetDB. Is there any way I can find the primary key and foreign key in my table?
The Query I tried:
SELECT *
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
AND TABLE_NAME = 'meta'
But this query didn't work, Since MonetDB query is different and I used /w-1 to get the table content and its data types but don't know about how to get key details.
Thanks for the help.

You can use the \d <table name> meta command in mclient. See this answer for more details.

Related

Finding the composite keys of a table

I have a huge table that i am querying and i just wanted to know how do i find the composite keys of a table as this table does not have a primary key however it contains an auto incremented id.
The table looks like this :
I am a beginner to SQL so any help will be appreciated thanks.

Is there a Perl module that automatically captures dependencies between Oracle tables based on foreign keys?

Does anyone know if there is a Perl module that once given a schema or table, would scan the Oracle database for all related tables based on foreign keys and put them in a hash for further processing?
Something like this for instance:
table 1 = sex, fields sex_id + description
table 2 = person, field person_id + sex_id + other fields
The code would create a hash with among it a key for sex_id, pointing to table sex.
etc
DBIx::Class::Schema::Loader should do the trick, though I have not tried it with Oracle, it is listed as supported in the docs, and worked fine for mysql for me.

How can I insert to Cassandra with CQL, to table with only primary key, using UPDATE?

I need to insert new rows to Cassandra, to a table that has only primary key columns, e.g.:
CREATE TABLE users (
user_id bigint,
website_id bigint,
PRIMARY KEY (user_id, website_id)
)
The obvious way to do it would be by INSERT:
INSERT INTO users(user_id, website_id) VALUES(1,2);
But I want to do it with use of Hadoop CqlOutputFormat and CqlRecordWriter only supports UPDATE statements. That's usually not a problem as UPDATE is in theory semantically the same as INSERT. (It will create rows if given primary key does not exist).
But here... I don't know how to construct UPDATE statement - it seems that CQL just does not
support my case, where there are non-primary key columns. See what I tried:
> update users set where user_id=3 and website_id=2 ;
Bad Request: line 1:18 no viable alternative at input 'where'
> update users set website_id=2 where user_id=3;
Bad Request: PRIMARY KEY part website_id found in SET part
> update users set website_id=2 where user_id=3 and website_id=2;
Bad Request: PRIMARY KEY part website_id found in SET part
> update users set website_id=2,user_id=1;
Bad Request: line 1:40 mismatched input ';' expecting K_WHERE
Some ideas on how to resolve it?
Many thanks.
Not sure if you can do this with update like that. But why not just create a new dummy column that you never use for anything else? Then you could do
update users set dummy=1 where user_id=3 and website_id=2;
You can't update primary key values in Cassandra as you have explained. As a solution you could also delete the row and insert a new one with the correct value in it. It's just a bit cleaner than creating two rows with one incorrect.

Enforcement of unique/primary key - drop index

I am trying to drop an index :
DROP INDEX PK_CHARGES
but I get this error
cannot drop index used for enforcement of unique/primary key
Why I am getting this error? I will provide further information if you need any.
How to solve it?
Edit I have no primary key in the table, but I found this weird index that I don't remember I had added:
index name = SYS_C0040476 which have the same columns
You can query the ALL_CONSTRAINTS performance view to see which constraint the index is used by, and which table it applies to, e.g:
select owner, constraint_name, constraint_type,
table_name, index_owner, index_name
from all_constraints
where index_name = 'PK_CHARGES';
I would expect the table name to be 'CHARGES', the constraint name to match the index name, and the constraint type to be 'P'. But since you have a table in mind, perhaps the names aren't following a helpful convention. Maybe an old version of the table was renamed, which would leave the constraints against the new name (e.g. CHARGES_BACKUP or something).
You said you click on the table, then on the view. Perhaps you're not looking at the table that the constraint/index is on; or perhaps you're looking at a view on top of the actual table. You also mention a SYS_ index on the same columns - which can't be on the same table. Do you have multiple similar tables, or access to multiple schemas? You shold run the above query for that index too. As mentions above, you might find an old version (or versions) of the table.
Once you've identified which table the constraint is on, you'll need to decide whether you should actually be keeping it, and if not you can remove it by dropping the constraint with an ALTER TABLE command.
The problem with
But I found this weird index that I dont rember I hadve add
comes because you didn't add it. You had a primary key, then you dropped it, but when you do that Oracle doesn't drop the associated unique index that every primary key has.
So when you drop a primary key you have to drop the unique index of that primary key, that amazingly has the same name as the primary key had.
So for dropping a MY_TABLE_PK you must do:
ALTER TABLE MY_TABLE DROP PRIMARY KEY DROP INDEX;
so you ensure that the index is dropped as well.
"from pl/sql I right click on the table"
The problem with IDEs is that they make us feel very productive, because we can do things with just a click instead of writing some code. This is a problem because when something unusual happens the IDE is no good for investigation and we lack the understanding of the underlying structure of the database which we need to help ourselves.
If you want to learn the Oracle database the worst thing you can do is download SQL Developer or PLSQL Developer or TOAD. They're all fine tools but the only people who should use them are the people who don't need to use them.
the following worked for me with unique index:
ALTER INDEX UX_CHARGES UNUSABLE
/
DROP INDEX UX_CHARGES
/
see: https://docs.oracle.com/cd/E18283_01/server.112/e17120/indexes004.htm#insertedID3

Oracle unique constraint error message

I am maintaining a legacy application and I recently got contacted that people are getting an error message when they try to fill one of our oracle tables. Now, those oracle tables are not in our care, but I still want to try out something to help find the problem.
Anyway, the error message is the following:
java.sql.SQLException: ORA-00001: unique constraint (REO0.PK_TableName) violated :
I know I can find a lot of information online through google and here about this error message. That is not what my question is about.
The question is: the tablename shown here (which I put in bold), is that
the name of the table, or is the
PK_ part added to represent 'primary key' ?
Reason why I ask is: I can't directly get to this database, but somehow I can see all tables in REO0 and I can find one with TableName but not one with *PK_TableName* as the name for a table. So if this PK_ would refer to something like 'primary key' (which the constraint of is violated) then it would make a bit more sense.
PK_tablename is the name of the constraint, and as Alex Poole states in a good comment, it has been specified in the DDL (CREATE TABLE ... (columns, CONSTRAINT PK_tablename PRIMARY KEY(columns...) ), or ALTER TABLE ... ADD CONSTRAINT PK_tablename PRIMARY KEY(columns...) or CREATE UNIQUE INDEX PK_tablename ON ... (columns) for example). When no name has been given, Oracle generates a name which begins with SYS.
Note that usually PK_x suggests a primary key for table x, but your constraint might also be a foreign key constraint or a not null constraint for example.
The following query will tell you all:
SELECT * FROM all_constraints WHERE constraint_name = 'PK_TABLENAME'

Resources