constraints

How can I drop a “not null” constraint in Oracle when I don't know the name of the constraint?

我是研究僧i 提交于 2019-11-28 04:02:41
I have a database which has a NOT NULL constraint on a field, and I want to remove this constraint. The complicating factor is that this constraint has a system-defined name, and that constraint's name differs between the production server, integration server, and the various developer databases. Our current process is to check in change scripts, and an automated task executes the appropriate queries through sqlplus against the target database, so I'd prefer a solution that could just be sent straight into sqlplus. On my own database, the SQL to drop this would be: alter table MYTABLE drop

Postgresql: Conditionally unique constraint

有些话、适合烂在心里 提交于 2019-11-28 03:52:57
I'd like to add a constraint which enforces uniqueness on a column only in a portion of a table. ALTER TABLE stop ADD CONSTRAINT myc UNIQUE (col_a) WHERE (col_b is null); The WHERE part above is wishful thinking. Any way of doing this? Or should I go back to the relational drawing board? PostgreSQL doesn't define a partial (ie conditional) UNIQUE constraint - however, you can create a partial unique index . PostgreSQL uses unique indexes to implement unique constraints, so the effect is the same, you just won't see the constaint listed in information_schema . CREATE UNIQUE INDEX stop_myc ON

Add primary key to existing table

删除回忆录丶 提交于 2019-11-28 03:36:59
I have an existing table called Persion . In this table I have 5 columns: persionId Pname PMid Pdescription Pamt When I created this table, I set PersionId and Pname as the primary key . I now want to include one more column in the primary key - PMID. How can I write an ALTER statement to do this? (I already have 1000 records in the table) drop constraint and recreate it alter table Persion drop CONSTRAINT <constraint_name> alter table Persion add primary key (persionId,Pname,PMID) edit: you can find the constraint name by using the query below: select OBJECT_NAME(OBJECT_ID) AS

Creating composite foreign key constraint

被刻印的时光 ゝ 提交于 2019-11-28 03:16:55
问题 I am trying to create a composite foreign key relationship/constraint. All tables are empty. I have this table: CREATE TABLE [dbo].[ChemSampleValueTest]( [SampleNumber] [int] NOT NULL, [ParameterID] [int] NOT NULL, [Value] [numeric](18, 6) NOT NULL, [Accuracy] [varchar](50) NULL, [ResultGroupID] [int] NOT NULL, [QAState] [nvarchar](32) NOT NULL, CONSTRAINT [PK_SampleValueTest] PRIMARY KEY CLUSTERED ( [SampleNumber] ASC, [ParameterID] ASC, [ResultGroupID] ASC ) ) ON [PRIMARY] and this table:

How to drop all user tables?

Deadly 提交于 2019-11-28 02:31:22
How can I drop all user tables in oracle? I have problem with constraints. When I disable all it is still no possible. Henry Gao BEGIN FOR cur_rec IN (SELECT object_name, object_type FROM user_objects WHERE object_type IN ('TABLE', 'VIEW', 'MATERIALIZED VIEW', 'PACKAGE', 'PROCEDURE', 'FUNCTION', 'SEQUENCE', 'SYNONYM', 'PACKAGE BODY' )) LOOP BEGIN IF cur_rec.object_type = 'TABLE' THEN EXECUTE IMMEDIATE 'DROP ' || cur_rec.object_type || ' "' || cur_rec.object_name || '" CASCADE CONSTRAINTS'; ELSE EXECUTE IMMEDIATE 'DROP ' || cur_rec.object_type || ' "' || cur_rec.object_name || '"'; END IF;

Adding an one-out-of-two not null constraint in postgresql

戏子无情 提交于 2019-11-28 02:27:13
问题 If I have a table in Postgresql: create table Education ( id integer references Profiles(id), finished YearValue not null, started YearValue, qualification text, schoolName text, studiedAt integer references Organizations(id), primary key (id) ); I need to make a constraint so that either schoolName or studiedAt needs to not be null (one of them has to have information in it). How do I do this? 回答1: You can use a check constraint e.g. constraint chk_education check (schoolName is not null or

A constraint that only allows one of two tables to reference a base table

不羁的心 提交于 2019-11-28 01:11:27
问题 I have 3 tables. A base table, call it Table A, and two tables that reference Table A, Call them Table X and Table Y. Both X and Y have a foreign key contraint that references Table A. The Foreign Key of X and Y is also their own Primary Key. I'd like to know if it is possible to add a constraint that will only allow one of these tables to contain a recrod that references Table A. So if X has a record that references A then Y can't have one and if Y has a record that references A then X can't

SQL Constraint: date A is before date B — How?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 01:07:08
I'm creating an SQL table that needs the fields from=date and to=date but I'd like to make a constraint so that to cannot be before from . My program will check for it but I'd like to learn how to enforce it with SQL. I've written SQL before but never really used constraints and don't know how they work. So the question is: Using standard SQL, how do I make sure that From is before To ? create table foo ( from_date date, to_date date, constraint check_dates check (from_date < to_date) ); Or if you need to apply this to an existing table, use: alter table foo add constraint check_dates check

How to write a constraint concerning a max number of rows in postgresql?

荒凉一梦 提交于 2019-11-27 22:47:31
I think this is a pretty common problem. I've got a table user(id INT ...) and a table photo(id BIGINT, owner INT) . owner is a reference on user(id) . I'd like to add a constraint to the table photo that would prevent more than let's say 10 photos to enter the database for each users. What's the best way of writing this? Thx! Quassnoi is right; a trigger would be the best way to achieve this. Here's the code: CREATE OR REPLACE FUNCTION enforce_photo_count() RETURNS trigger AS $$ DECLARE max_photo_count INTEGER := 10; photo_count INTEGER := 0; must_check BOOLEAN := false; BEGIN IF TG_OP =

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1922-1' for key 'IDX_STOCK_PRODUCT'

 ̄綄美尐妖づ 提交于 2019-11-27 21:28:59
While creating product, at the last step after retrieving for a time, Magento gives following error-: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1922-1' for key 'IDX_STOCK_PRODUCT' What I am doing is, by capturing product id, I am putting it's entry in custom table. I have connected to Magento database externally. Surprisingly data is inserted in both Magento's base table & also in Custom table but why it is giving me that error after product saving...? I cleared cache, browser cookies. Also remove /var/cache, /var/session. still giving error. Can anybody suggest a