Adding Constraints to an existing table
Hi,
I've consulted the forum, documentation, the internet and text books but cannot find the correct SQL command to add constraints to an existing table
Obviously I would
ALTER TABLE Turkey ADD CONSTRAINT Whitemeat
But what comes next is puzzling, the values of column Whitemeat can be any value other than Dinner, Gravy, Pumpkin, or Sauce
Any assistance would be great
Thank You
Hi,
user8998591 wrote:
Sorry,
This is what I'm trying to run
ALTER TABLE TURKEY add
CONSTRAINT TURKEY_WHITEMEAT_CK CHECK WHITEMEAT NOT IN ('Dinner', 'Gravy', 'Pumpkin', 'Sauce');And this is the error message I am getting
Error report:
SQL Error: ORA-00906: missing left parenthesis
00906. 00000 - "missing left parenthesis"
*Cause:
*Action:Thank you for your assistanceSee the [SQL Language manual|http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/statements_3001.htm#sthref5176] for the correct syntax.
The condition after the keyword CHECK has to be in parentheses.
To add a CHECK constraint to an existing table, I think you have to modify the column.
ALTER TABLE turkey
MODIFY ( whitemeat CONSTRAINT turkey_whitemeat_ck
CHECK ( whitemeat NOT IN ( 'Dinner'
, 'Gravy'
, 'Pumpkin'
, 'Sauce'
) ) );Edited by: Frank Kulash on Jan 22, 2010 4:37 PM
Similar Messages
-
How add primary key constraint to already existing table with data
I want apply primary key constraint to already existing table with data
is there any command or way to doAlternatively, assuming you want to ensure uniqueness in your primary key column you can do this:
alter table <table name> add constraint <cons name> primary key (col1,col2) exceptions into <exception_table>
If the altter table statement fails this will populate the EXCEPTIONS table with the rows that contain duplicate values for (col1,col2).
You will need to run (or get a DBA to run) a script called UTLEXCPT.SQL (which will be in the $ORACLE_HOME/rdbms/admin directory) if you don't already have an EXCEPTIONS table.
Cheers, APC -
Adding fields to an existing table without dropping it
Hi Guys,
Just wondering if anyone can tell me if it's possible to write an SQL statement to add extra fields to an existing table without dropping it then re-creating and also what SQL would be needed. The added fields will be part of an exclusive arc, so can contain NULL values.
Any help would be much appreciated.
AntonHi,
You can Use the ALTER TABLE statment.
like consider emp table having 3 columns empno,enmae,deptno.
now u want to add sal and mgr columns.
u can do it like this
ALTER TABLE ADD ((sal NUMBER(5,2),(mgr NUMBER));
If u want to modify the column datatype
u can use ALTER TABLE MODIFY (<column datatype>............);
Trinath Somanchi,
Hyderabad. -
Adding a UNIQUE Constraint to an existing table
Hi,
I got this issue because of the existing data. My issue is in my table I want to add a unique constraint to two columns but i cannot add this becuase of the existing repeating data. And I cannot do a data repair to fix the repeating data since the customer is reling on this data so we can not get the decision to repair the data.
As a solution I try this method, by adding a function to check the repeating data before inserting the data to the table. It's working fine to a user but when it come to multiple users it's not working because users can log to the database and can do their transactions simultaneously.
My question is; is there a way in oracle to add a constraint to the data that can add in future not to the old existing data?
Thanks,
Darex.user9359353 wrote:
Hi,
As a solution I try this method, by adding a function to check the repeating data before inserting the data to the table. It's working fine to a user but when it come to multiple users it's not working because users can log to the database and can do their transactions simultaneously.
show us what is "not working". if you are calling this function from a trigger the correct way, the first person to commit will have their data inserted and the next person will not be able to insert.
edit: you may want to have a read through this thread: where I was encountering a problem with multi-row validation using triggers:
Row level validation dependant on other rows?
note this post:
Rob van Wijk wrote:
Hi WhiteHat,
Here are two blogposts of mine about this subject that you might find useful.
One with some guidelines about implementing entity rules: http://rwijk.blogspot.com/2008/08/implementing-entity->rules.html
And one with an example how to implement (among others) an overlap check in the context of another option for your >question, the product RuleGen: http://rwijk.blogspot.com/2008/05/rulegen-test.html
Hope this helps.
Regards,
Rob.Edited by: WhiteHat on Jun 8, 2011 3:51 PM -
Implications of adding new key fields to existing table
Hi All,
I have searched forum regarding this. But didn't find exact answer.
We are planning to add new key fields to existing Ztable. I want to know the implications of this.
I have checked the where used list of table and found no impact. Only one point is making me to think again and again.
If we add key fields to existing table then we have to adjust the table from SE14 to activate it. But this adjustment doesn't ask for TR.
So if i release my TR, entries in other systems will also be adjusted accordingly??? Business is OK with the new fields values to be blank for existing entries.
Thanks,
Vinod.Yes the data in transported systems will also be adjusted. If this table contains too much data make transport at late hours because it will take long and table can't be used while it's adjusted. In this adjustment process data is copied to a temporary table and moved back to original table using move-corresponding command after key added. There will data loss if you remove a key or key fields field length but in your case it shouldn't be a problem.
Edited by: Gungor Ozcelebi on Jul 2, 2009 9:18 AM -
Hi Guys,
I have to add a new Big INT column to existing table in production, which holds 700 million records and would like to know the impact?
I have been tolled by one of my colleagues that last time they tried adding a column to same table during working hour and it locked out the table and impacted the users.
Please suggest/share If any one had similar experience.
Thanks Shiven:) If Answer is Helpful, Please VoteIf you add a new column to a table using an ALTER TABLE ADD command and specify that the new column allows NULLs and you do not define a default value, then it will take a table lock. However, once it gets the table lock, it will essentially run instantly
and then free the table lock. That will add this new column as the last column in the table, for example
ALTER MyTable ADD MyNewColumn bigint NULL;
But if you your change adds a new column with a default value, or you do something like using table designer to add the new column in the middle of the current list of columns, then SQL will have to rewrite the table. So it will get a table lock, rewrite
the whole table and then free the table lock. That will take a considerable amount of time and the table lock will be held for that whole period of time.
But, no matter how you make the change, if at all possible, I would not alter a table schema on a production database during working hours. Do it when nothing else is going on.
Tom -
Hi gurus,
Please tell me how to add row to the existing table where we enter conditions while defining the calculation schema.
i.e. Control data screen for calculation schema.Hi Diwakar,
Two ways of creating the 3 static rows intially is either create 3 rows manually Or create a single row in table and then in Binding check the check box "Repeat Row for each Item Data" and set min count to "1" and initial count to "3"
and in add button use the same code as you are using with addInstance(1)
Or try this below:
TableSubform.Item_Table.Row1.instanceManager.addInstance(1);
Sachin -
How to add an unique column to an existing table?
How to add an unique column to an existing table?
I have a large table which has no unique constraint. and I want to add an unique column for it. How to do it?
Does adding a sequence is a good choice? How to do it?
Thank youHi,
alter table tablename
add constraint contraint_name unique (columnname);but before that you need to check in the table.column there is no duplicate record exist.
Does adding a sequence is a good choice?
Your talking about unique constraint then yes.
Regards,
Taj -
Challenge of the day - CMR existing tables
Hey all!
I have an existing application that contains 1:N table relationships that are already populated with data. How can I convert the EJB's (EJB2.0/CMP2.0) to use CMR?
As a reference, I have tried this with Jboss 3.2.2 and XDoclet and have only had success with @jboss:auto-key-fields which will make a third table that is blank and only populated when data is added through the EJB relationship, not the existing populated data (that, and why use a third table?).
Maybe this is the only way, not sure as new to CMR, can anyone be my hero and enlighten me please?I guess I'm still a little confused about some parts
of this. I do already have existing tables (with
indicies). I do have one big question:
With CMR, will I be required to create a third table
for the relationship?
If not, then it seems that as long as the correct
mappings (within CMP) have taken place, I should be
immediately up and running without changes. This is
the route I initially took with:
* @jboss.relation
* related-pk-field="mypk"
* fk-column="mypk_fk"
But unfortunately I must have missed something as can
not get it to work correctly. If someone can help
point me to one way (a third table) or the other
(pk/fk mapping within CMP) that will help me on my
way!Well, here it follows what I'm doing for my People-Address (1:1) relationships and which is working:
//persistent-relationships
* @ejb.interface-method
* view-type="local"
* @ejb.relation
* name="People-Address"
* role-name="1-People-1-Address"
* multiple="no"
* target-role-name="1-Address-1-People"
* target-multiple="no"
* target-ejb="Address"
* @jboss.relation
* fk-constraint="true"
* related-pk-field="addressId"
* fk-column="addressId"
public abstract AddressLocal getHomeAddress();
AddressLocal is obviously the LocalInterface of the Address entity bean and here we are within the People bean (which is supposed to be the main table). In a relationship, it's important to understand that both the source (People entity in this case) and the target (Address entity in this case) must be set. If there could be more than one Address for one People, than you would have set:
...snip
* @ejb.relation
* name="People-Address"
* role-name="1-People-N-Address"
* multiple="yes"
* target-role-name="1-Address-1-People"
* target-multiple="no"
* target-ejb="Address"
Hope it will help,
Marco -
Hi All,
I need to get the "Creation Script" of the existing table using c# and without using SMO dlls (is it possible? I don't know).
I.e. In SQL Management Studio -> right click on any table -> Script table as -> Create To - > open in the new query editor window. This will give you the schema of the table with the constraints of the table.
For E.g. In Northwind database, for the table "Categories", I would like to get it as show below
USE [Northwind]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Categories](
[CategoryID] [int] IDENTITY(1,1) NOT NULL,
[CategoryName] [nvarchar](15) NOT NULL,
[Description] [ntext] NULL,
[Picture] [image] NULL,
CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED
[CategoryID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
I would like to get the same schema using c#. Please help.
Thanks & Regards,
Kalai.SMO is the easiest way to get this. This is what Management Studio uses. If you can't use SMO, get a Profiler trace of the queries that SMO executes when generating the script and execute the same using ADO.NET.
Regards,
Farooq Mahmud
Support Escalation Engineer
• Microsoft Health Solutions Group -
How can I convert an existing table to IOT
Hi,
My Oracle version is 10.2.0.4
Below is my structure of my test_table .
It has 1141580 rows.
I want to convert this into index Organized table. It has no secondary indexes.
How can I convert an existing table to IOT.
CREATE TABLE test_table (
Column1 NUMBER NOT NULL,
Column2 DATE NOT NULL,
Column3 VARCHAR2(100) NOT NULL,
Column4 VARCHAR2(8) NULL,
Column5 VARCHAR2(40) NOT NULL,
Column6 VARCHAR2(3) NULL,
Column7 NUMBER NOT NULL,
Column8 NUMBER NULL,
Column9 VARCHAR2(32) NULL,
ALTER TABLE test_table
ADD CONSTRAINT test_table_pk2 PRIMARY KEY (
Column1,
Column2
USING INDEX
STORAGE (
INITIAL 30720 K
ALTER TABLE test_table
ADD CONSTRAINT test_table_cons1 FOREIGN KEY (
Column1
) REFERENCES test_table2 (
Column1
/alter table doesn't allow to change the organization of a table.
So the logical answer to your question is
You can not
and your question
redundant.
As most other questions here.
So the answer should have been
- create a new table
- insert all rows
- drop the old table
- rename the new table
Oracle is not about rocket science.
Oracle is about reading documentation and using brains.
If you do that, maybe, one day, you will be a bigboy.
If you keep asking redundant questions you will remain a littleboy.
Sybrand Bakker
Senior Oracle DBA -
Datapump : How to append data in an existing table
Hello Everyone,
We are new to Datapump.
We try to extract data from one user/schema and to append it into another user/schema.
First we tried Tt use the parameter table_exists_action=append during the importation but we receive this error (but the rows are appended):
ORA-39152: Table "XXXXX"."YYYYY_ZZZ" exists. Data will be appended to existing table but all dependent metadata will be skipped due to table_exists_action of append
Which I don't expect since the utility have been told to, indeed, append data.
Next we tried to use CONTENT=DATA_ONLY on exportation and importation but the importation never end.
How can we append data into a table's user/schema without having an error?
Best regards.
CarlIGNORE=Y during the import.it does the same operation. if the table already exists,it ignores and proceed with importing/appending data to the tables. same way, they do have indexes=n and constraints=n option.
both export/import have equivalent options for fitering to our requirement and datapump has one step above classic import in which you can filter upto metadata object as well. -
How to update (add new data in different tab) existing table from Excel
i've an existing table, for instance User Profile table, it consists of few tab in the table which contains different data... recently i've added new tab to the existing table and i would like to upload a particular data for this new tab... is there any way to upload (insert new data for the tab on existing data) this particular data into the existing table from Excel file?
could it be done by using lsmw?
Edited by: Yeong Kang Liew on Apr 5, 2010 4:35 AMCheck HELP on MODIFY & UPDATE statements.
-
Oracle 11g imp erroneously tries to recreate existing tables with CLOBs?
I have a shell script for loading database dumps from both Datapump and the older exp/imp.
Often when loading dumps, I need to rename the schema owner and tablespace names (which is handled by REMAP_SCHEMA and REMAP_TABLESPACE in Datapump).
However I have a whole bunch of dumps created with exp at this point and not that many Datapump dumps yet. As such the old style dumps are handled by the shell script in this way:
1) A first pass imp is run using INDEXFILE to generate a file with the SQL to create tables and indexes. Options also include FROMUSER and TOUSER.
2) A series of sed command edit the SQL file to change the tablespace names (which are schema owner specific in our case).
3) The editted SQL file is run with sqlplus to create the tables and indexes.
4) A second pass imp is run to load the table rows as well as triggers, stored procedures, views, etc. Options include FROMUSER, TOUSER, COMMIT=Y, IGNORE=Y, BUFFER, STATISTICS=NONE, CONSTRAINTS=N
This shell script has been working great for loading exp dump files into Oracle 9 and Oracle 10 databases, but now that I'm trying to load these dumps into Oracle 11, it fails.
The problem is in step 4, the imp program is trying to create some of the tables that already were created with sqlplus in step 2. The problematic tables all seem to have CLOB columns in them. The table creation fails because it tries to use the tablespace names from the dump file, which do not exist in the destination database. And when the table creation fails, imp then decides not to load the rows for those table.
This seems like a bug in the Oracle 11 imp program. I don't understand why it thinks it needs to recreate tables that already exist when those tables have CLOB columns. Is there something different about CLOB columns in Oracle 11 that I should know about that might be confusing imp into thinking that it needs to create tables when they already exist? Maybe I need to do something to those tables in SQL so that imp does not think it needs to recreate them?
I know that the tables with the CLOBs were created correctly because I was trying to find some way to workaround this. For step 4, I tried using DATA_ONLY=Y, in which case imp does not try to create the tables and just loads the table rows. Of course using DATA_ONLY, I don't get a lot of other things like triggers, view and stored procedures. I started to try to get around that by doing 3 passes with imp, so that I could pick up the missing pieces by using an imp pass with ROWS=N, but strangely that has the same problem of trying to recreate the existing tables.The only solution I've found so far as a workaround is rather convoluted.
1. I took an export using datapump's expdp of SCHEMA1 (in 10g it will skip the table with the xmltype).
2. I imported the data to my empty schema (SCHEMA2) using impdp. To avoid the error that the type already exists with another OID, I used the TRANSFORM=oid:n parameter e.g.
impdp user/pwd dumpfile=noxmltable.dmp logfile=importallbutxmltable.log remap_schema=SCHEMA1:SCHEMA2 TRANSFORM=oid:n directory=MYDUMPDIR
3. I then manually created my xmltype table in the SCHEMA2 and did a select into to load it (make sure you have the select privileges to do so):
INSERT INTO SCHEMA2.XMLTABLE2 SELECT * FROM SCHEMA1.XMLTABLE1;
4. I am still taking an export with exp of the xmltable as well even though I'm not sure I can do anything with it.
Thanks!
Edited by: stacyz on Jul 28, 2009 9:49 AM -
Is it possible to import data into an existing table?
Hi.. everyone.
Is it possible to import data into an existing table
by using import utility?
The exported table and the existing table have the same name
and table schema.
I want to append "new data(exported from oracle8.0.5)" to
"an existing partitioned-table" by using "oracle 9i import utility".
Thanks in advance.
Have a nice day.
Ho.SQL> select index_name
2 from user_indexes
3 where table_name = 'IMPORTPK';
INDEX_NAME
SYS_C007610
SQL> create view importpk_view as select * from importpk;
View created.
SQL> disconn
Disconnected from Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Pr
oduction
With the Partitioning, OLAP and Data Mining options
SQL> exit
C:\>exp userid=oracle/oracle tables=importpk
Export: Release 10.1.0.2.0 - Production on Tue Sep 5 08:30:50 2006
Copyright (c) 1982, 2004, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Produc
tion
With the Partitioning, OLAP and Data Mining options
Export done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table IMPORTPK 10 rows exported
Export terminated successfully without warnings.
C:\>imp userid=oracle/oracle tables=importpk ignore=y
Import: Release 10.1.0.2.0 - Production on Tue Sep 5 08:31:12 2006
Copyright (c) 1982, 2004, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Produc
tion
With the Partitioning, OLAP and Data Mining options
Export file created by EXPORT:V10.01.00 via conventional path
import done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
. importing ORACLE's objects into ORACLE
. . importing table "IMPORTPK"
IMP-00019: row rejected due to ORACLE error 1
IMP-00003: ORACLE error 1 encountered
ORA-00001: unique constraint (ORACLE.SYS_C007610) violated
Column 1 1
Import terminated successfully with warnings.
C:\>sqlplus/nolog
SQL*Plus: Release 10.1.0.2.0 - Production on Tue Sep 5 08:33:20 2006
Copyright (c) 1982, 2004, Oracle. All rights reserved.
1.No
Note : if table content primary key and index.
SQL> conn oracle/oracle
Connected.
SQL> create table withoutcons ( no number);
Table created.
SQL> insert into withoutcons select * from importpk;
10 rows created.
SQL> create view withoutcons_view as select * from withoutcons;
View created.
SQL> disconn
Disconnected from Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Pr
oduction
With the Partitioning, OLAP and Data Mining options
SQL> exit
C:\>exp userid=oracle/oracle tables=withoutcons
Export: Release 10.1.0.2.0 - Production on Tue Sep 5 08:36:05 2006
Copyright (c) 1982, 2004, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Produc
tion
With the Partitioning, OLAP and Data Mining options
Export done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table WITHOUTCONS 10 rows exported
Export terminated successfully without warnings.
C:\>imp userid=oracle/oracle tables=withoutcons ignore=y
Import: Release 10.1.0.2.0 - Production on Tue Sep 5 08:37:17 2006
Copyright (c) 1982, 2004, Oracle. All rights reserved.
Connected to: Oracle Database 10g Enterprise Edition Release 10.1.0.2.0 - Produc
tion
With the Partitioning, OLAP and Data Mining options
Export file created by EXPORT:V10.01.00 via conventional path
import done in WE8MSWIN1252 character set and AL16UTF16 NCHAR character set
. importing ORACLE's objects into ORACLE
. . importing table "WITHOUTCONS" 10 rows imported
Import terminated successfully without warnings.
2. Yes
If table content no unique constraints
Message was edited by:
user526020
Maybe you are looking for
-
I am from Brazil and I am going to USA in the end of the year. I know it takes 1-2 weeks to ship the iphone 4s, so I will buy it and send it to the hotel address . They will keep my package until I arrive. What do I have to do? Give an authorization
-
Using an Airport Extreme with a wireless router
I use to have an original Airport Base station connected to a D-Link DSL504 ADSL Wired Router. All worked well. Then my basestation died so I upgraded my router to a Netgear DG834GT [108mps] ADSL wireless router. With this router I have never been ab
-
How do i get my ipad to sync itunes with my computer
how do i get my ipad to sync i tunes with my computer
-
No mail is sent from SAP Workflow
Hi We are sending mail from SAP Workflow to Lotus notes email. We have made the correct configuration in su01d for users whom we are sending by mentioning email address and communication method as email. We have also checked that program RSCONN01 is
-
I downloaded ios8 on my itunes but when it was putting it onto the iphone it came up with an error message saying it could not be updated (-1). It then went into recovery and when restoring it says it cannot be restored in an error message saying an