How to make constraints work in Snowflake? - snowflake-cloud-data-platform

Is there a way for constraints to actually work in Snowflake?
A primary key is created. Still duplicates can be inserted in the table. Giving options like cascade update and delete cascade are not working with Foreign key
Can someone please help?

if you read the Snowflake documentation you will see that only NOT NULL constraints are enforced, all other constraint types are informational only.
I am guessing that the reason for this is that Snowflake is an analytical, rather than an OLTP, database and therefore the expectation is that constraints are enforced in your ELT processes (as is normal practice) rather than in the DB.

Snowflake does not enforce constraints except not null.
Snowflake Notes . I think we cannot enforce a constraint in snowflake database but you can apply the constraint in your ETL tool(if using)

Related

Unique constraint still alows duplicate values

I have a table with a unique constraint on event_time and card_nr, nevertheless, I'm still able to insert duplicate values to the table using the statements below.
The screenshot below shows the table I used as well as my DDL & insert queries.
There is also an EXPLAIN statement to prove that the columns are unique.
An error should be thrown since I'm violating my constraint values?
Supported Constraint Types
Snowflake supports the following constraint types from the ANSI SQL standard:
UNIQUE
PRIMARY KEY
FOREIGN KEY
NOT NULL
...
Snowflake supports defining and maintaining constraints, but does not enforce them, except for NOT NULL constraints, which are always enforced.
What is the point of supporting a unique constraint but not enforcing it?
Constraints
Constraints are provided primarily for data modeling purposes and compatibility with other databases, as well as to support client tools that utilize constraints. For example, Tableau supports using constraints to perform join culling (join elimination), which can improve the performance of generated queries and cube refresh.

JPA Foreign Key Restrict On Delete

Is it possible to enforce a FK constraint with only JPA annotations and not in the DB?
I want to Restrict/No Action on delete to enforce that a FK reference cannot be deleted while it is being used in a parent table. However, I have not been able to accomplish this functionality without adding the FK Constraint at the DB level.
Is this possible or is it required to also add the constraint to the DB as well?
Thank you
Nope, it's not possible. JPA relies on the DB to enforce FK constraints.
With Hibernate, you can provide a custom delete query using #SqlDelete. You could e.g. use a procedure to check if the constraint is going to be violated. I imagine that's not what you're looking for, though.

DbUnit: insert data into DB2 database after turning of foreign keys

I'm trying to insert initial data into a DB2 database in DbUnit using DatabaseOperation.INSERT.execute(...) which works fine with some datasets. In order to insert some datasets however, I need to disable foreign key constraints first (because the tables in some datasets can be listed in a 'wrong' order).
I'm disabling the foreign key constraints with command SET INTEGRITY FOR <table_name> OFF, but when I try to insert the data after calling that command, I get this error:
com.ibm.db2.jcc.am.SqlException: DB2 SQL Error: SQLCODE=-668, SQLSTATE=57016, SQLERRMC=1;SCHEMA.TABLE, DRIVER=4.17.30
The IBM error code explanation isn't much helpfull here. Is there something I need to do after setting integrity on a table and before inserting data into that table?
EDIT:
I found this in the documentation for the OFF statement: "Specifies that the tables are placed in set integrity pending state. Only very limited activity is allowed on a table that is in set integrity pending state."
If I understand it correctly, this means that when I turn off the integrity checks on a table, I cannot perform any write/modify operations on it! What's the point of turning the integrity check off then? I need to find a way to do this.
You are not "disabling the foreign key constraints with command SET INTEGRITY". SET INTEGRITY OFF basically means "I'm not sure about the integrity of this table data, so I'd rather restrict access to it until I figure out what's wrong".
To temporary disable foreign key verification you might try ALTER TABLE foo ALTER FOREIGN KEY bar NOT ENFORCED.

Relationship between tables to prevent insertion of records where foreign key doesn't exist

Hi I've set up two very basic tables. One table will act as a look up, with an identity field as a primary key. The other table uses the look up ID as a foreign key.
I have created a relationship constraint so now I cannot delete from the look up if the foreign key is used in the "main" table.
However my issue is i can add a record with a foreign key that doesn't exist.
To my way of thinking this shouldn't be allowed, can anyone tell me what setting I need to use to enforce this and whether this is typical database design or not?
Thanks Dave
You way of thinking is correct. Good database design provides some way of enforcing what is called "Referential Integrity". This is simply a buzzword for the concept you have derived on your own. Namely that a foreign key should be rejected if it refers to a non existent row. For a general discussion of referential integrity, see the following Wikipedia article. It's short.
http://en.wikipedia.org/wiki/Referential_integrity
Some pprogrammers would like to enforce referential integrity inside their programs. In general, it's a much better plan to define a referential integrity constraint inside the database, and let the DBMS do the enforcement. It's easier, it's faster, and it's more effective.
The SQL Data Definition Language (DDL) provides a way to declare a foreign key constraint when you create a table. The syntax differs a little between different dialects of SQL, but it's basically the same idea in all of them. Here's a capsule summary.
http://www.w3schools.com/sql/sql_foreignkey.asp
The documentation for SQL Server should have a description of the referential integrity constraint under the CREATE TABLE command.

Turn off referential integrity in Derby? is it possible?

Is there a setting in derby, for example an sql query ala "SET DATABASE REFERENTIAL INTEGRITY FALSE" where i can turn on and off referential integrity?
If you have a constraint that you don't wish to enforce, you can use DROP CONSTRAINT to drop it.
I frequently drop a constraint for a period of time while I am re-structuring my database, then re-add the constraint subsequently when I have the new data arranged as I want it.
According to this post on the mailing list (from 2006) it is not possible:
http://www.mail-archive.com/derby-user#db.apache.org/msg05345.html
I couldn't find anything in the manual either.
And the list of jdbc parameters has nothing, too.

Resources