changing column addition of primary key constraint

How to change a table and edit a column and add a primary key constraint?

987018 wrote:
can add us a primary key constraint by using the command Edit?

If the column already exists, and there is no existing primary key, then

SQL> create table t (id number, descr varchar2(10));

Table created.

SQL> alter table t modify (id not null primary key);

Table altered.

If the column does not exist yet and there is no primary key, then:

SQL> create table t1 (descr varchar2(10));

Table created.

SQL> alter table t1 add (id number not null primary key);

Table altered.

John

Tags: Database

Similar Questions

  • Not Null on the column of a primary key constraint

    I came across a table defined in the same way to...

    create table t (t_id int constraint nn_t_id not null constraint pk_t primary, data key number);

    Of course, the primary key is the is not null, then is there a reason to add the redundant constraint not null?

    There are two parts to your question, I think:

    (1) why add a NOT NULL if a primary key is also reported.
    (2) why is null is NOT a name defined by the user (in this case nn_t_id).

    1 > I think you are right: there is no difference in behavior in the way that Oracle is going to reject the creation of NULL in this column (whether through the direct insert or update with a NULL value). I personally always declares the NOT NULL for columns, I know to never have null, regardless of whether the column is (member of) a PK.

    It's pretty funny, btw, the SQL standard as chosen able to be the NULL default value. I think that we all type much less, if the SQL standard chose NOT NULL default value. But that's another story.

    2 > I don't see given nullability defined by the user of the explicit names now and then, because of some "coding guideline' which dictates that constraint names must never be generated System. This guideline is often presented as a result of the exception handling of "client-side", where the constraint name is filtered SQLERRM and then more personalized to be displayed to the end user. However for NOT NULL constraint violations, I think (does not fully to check) that you are always thrown an ORA-01400 is an ORA-01407. Who nowadays both will tell you exactly (in SQLERRM) which column of this table is currently in violation of a NOT NULL constraint.

    Toon

  • When I change a column is a primary key the associated non-unique index to become unique?

    So basically I already tried this and it shows me that the associated index is not unique.

    create table employees2 in select * from employees;

    create index emp_idx on employees2 (employee_id);

    ALTER employees2 table add primary key (employe_id) using index emp_idx;

    Select * from user_indexes where index-name = "EMP_IDX";

    I was wondering if I right assuming that when you change a column to a primary key or unique while using a given index that does not have the respective index become unique.

    The textbooks I use are sometimes a little hard to understand because of the wording, also, I want to just ask someone with a little more experience than me.

    Thank you.

    your test did give the correct answer: the index is not unique if it serves to bear a unique or primary key constraint. Indeed, it is one of the benefits of the use of no unique indexes in support of UK/PK constraints (since it allows to set the unusable index before to make bulk loads; and, of course, they have also some disadvantages - for example, they need an additional logical reading to reach a line). Richard Foote explains the details in https://richardfoote.wordpress.com/2008/06/04/primary-keys-and-non-unique-indexes-whats-really-happening/ (and other items).

  • modify a primary key constraint

    Hello

    I start with oracle, and I have two doubts which cannot handle after a search on the internet.

    1. it is possible to do an ALTER TABLE for a DEFAULT CONSTRAINT?. Maybe you think that the question should be in a different way like this: oracle database takes the default condition as a constraint?. As far as I know, not done that.

    2. it is possible, with an ALTER TABLE to CHANGE a CONSTRAINT of a PRIMARY KEY, to add another field more?

    I searched the internet, but I have not found a clear answer. I use oracle 10g express edition.


    Thank you very much.

    P.D.:sorry for my English, it is not very good.

    #2-you can not 'add a column to the primary key of a table constraint' with a single ALTER, but you can delete and re-create the constraint primary key (including the additional column).

    13:25:26> create table t1 (a number primary key, b number, c number );
    
    Table created.
    
    Elapsed: 00:00:00.09
    13:25:46> alter table t1 drop primary key;
    
    Table altered.
    
    Elapsed: 00:00:00.06
    13:25:47> alter table t1 add primary key (a,b);
    
    Table altered.
    
    Elapsed: 00:00:00.09
    

    But you will not be able to drop the primary key of a table index.

    13:25:47> create table t2 (a number primary key, b number, c number ) organization index;
    
    Table created.
    
    Elapsed: 00:00:00.03
    13:25:59> alter table t2 drop primary key;
    alter table t2 drop primary key
    *
    ERROR at line 1:
    ORA-25188: cannot drop/disable/defer the primary key constraint for
    index-organized tables or sorted hash cluster
    
    Elapsed: 00:00:00.00
    13:25:59> 
    

    #1-I'm not sure I understand your question, but you can change a table column to have a default value. This isn't a "constraint", but a default value. It has an effect when you perform an insert which does not include this column.

    13:31:23> alter table t1 modify (c default 3.14);
    
    Table altered.
    
    Elapsed: 00:00:00.04
    13:31:24> insert into t1 (a,b,c) values (1,2,3);
    
    1 row created.
    
    Elapsed: 00:00:00.01
    13:31:24> insert into t1 (a,b,c) values (4,5,NULL);
    
    1 row created.
    
    Elapsed: 00:00:00.00
    13:31:24> insert into t1 (a,b) values(6,7);
    
    1 row created.
    
    Elapsed: 00:00:00.01
    13:31:24> select * from t1;
    
             A          B          C
    ---------- ---------- ----------
             1          2          3
             4          5
             6          7       3.14
    
    3 rows selected.
    
    Elapsed: 00:00:00.18
    
  • primary key constraint...

    Hi all

    I have a table 'employees' on 'hr' scheme, now, I created a user 'rahul' on the same database and pulling a request by logging in with the user "rahul" which is:

    create table rahul.employees
    as (select * from hr.employees); -ps: I have the privilege to read data from hr.employees

    Now, the above query works fine, the only issue is that employe_id is the primary key for the table hr.employees and when I check the table of rahul.employees, the pruimary key constraint is missing, however the structure of the table and the data is there according to my expectations.

    I so want to know how to create a table with data using the definition of schema_name.table_name as well as the primary key constraint.

    Concerning
    Rahul karou

    Mac_Freak_Rahul wrote:
    Thanks for the reply, but this query returns me nothing, is it a question of privilege?

    Concerning
    Rahul

    Are you an error? Otherwise, the result will be CLOB, just copy from your clint tool and paste it into a Notepad...

    between it's table

    CREATE TABLE "HR"."EMPLOYEES1"
       (     "EMPLOYEE_ID" NUMBER(6,0),
         "FIRST_NAME" VARCHAR2(20),
         "LAST_NAME" VARCHAR2(25) CONSTRAINT "EMP_LAST_NAME_NN" NOT NULL ENABLE,
         "EMAIL" VARCHAR2(25) CONSTRAINT "EMP_EMAIL_NN" NOT NULL ENABLE,
         "PHONE_NUMBER" VARCHAR2(20),
         "HIRE_DATE" DATE CONSTRAINT "EMP_HIRE_DATE_NN" NOT NULL ENABLE,
         "JOB_ID" VARCHAR2(10) CONSTRAINT "EMP_JOB_NN" NOT NULL ENABLE,
         "SALARY" NUMBER(8,2),
         "COMMISSION_PCT" NUMBER(2,2),
         "MANAGER_ID" NUMBER(6,0),
         "DEPARTMENT_ID" NUMBER(4,0),
          CONSTRAINT "EMP_SALARY_MIN" CHECK (salary > 0) ENABLE,
          CONSTRAINT "EMP_EMAIL_UK" UNIQUE ("EMAIL")
      USING INDEX  ENABLE,
          CONSTRAINT "EMP_EMP_ID_PK" PRIMARY KEY ("EMPLOYEE_ID")
      USING INDEX ENABLE,
          CONSTRAINT "EMP_MANAGER_FK" FOREIGN KEY ("MANAGER_ID")
           REFERENCES "HR"."EMPLOYEES" ("EMPLOYEE_ID") ENABLE,
          CONSTRAINT "EMP_JOB_FK" FOREIGN KEY ("JOB_ID")
           REFERENCES "HR"."JOBS" ("JOB_ID") ENABLE,
          CONSTRAINT "EMP_DEPT_FK" FOREIGN KEY ("DEPARTMENT_ID")
           REFERENCES "HR"."DEPARTMENTS" ("DEPARTMENT_ID") ENABLE
       ) 
    
  • Order columns (which make up the primary key constraint) matter in where clause of DML query for use of indexing mechanism to operate.

    Version of DB: database Oracle 11 g Enterprise Edition Release 11.2.0.3.0 - 64 bit Production


    I have a table my_table as below:
    create table my_table
    (
    my_code varchar2 (6).
    my_id varchar2 (24).
    forced pk_1 primary key (my_code, my_id)
    );


    Primary_key here's a composite key that contains columns 1 (my_code) and 2 (my_id).
    Is there that a difference in the way below queries is executed in terms of performance (use of indexing in the extraction).

    (a) select * from my_table where my_code = '123' and my_id = "456";
    (b) select * from my_table where my_id = '456' and my_code = '123';


    The understanding I have the order of the column in the where clause should be identical to the sequence in
    What primary key draws for indexing to be used by oracle in oracle other DML queries ignores indexing
    However when I used explain plain both show the same query cost with single scan with index rowid.
    so I don't know if I'm wrong in the concept that I have. Kindly help.

    Thanks in advance,
    Gerard

    Your question is answered in the Performance Tuning Guide

    Using indexes and clusters

    14.1.4 choosing composite indexes

    A composite index contains several key columns. Composite indexes can provide additional benefits compared to the index to single column:

    • Improved selectivity

      Sometimes the two or more columns or expressions, each with a low selectivity can be combined to form a composite with a high selectivity.

    • Reduced IO

      If all columns selected by a query are a composite index, then Oracle may return these values in the index without access to the table.

    A SQL statement can use a path on a composite index if the statement contains constructions that use a main part of the index.

    Note:

    This is no longer the case with the skip index scans. See "Index Skip Scans".

    A main part of an index is a set of one or more columns that have been specified first and consecutively in the list of columns in the CREATE INDEX statement that created the index.

  • Add primary key constraint

    What will happen if I do this?

    ALTER table emp
    Add primart key (employee_id, last_name);

    It's going to be two primary keys in the table?
    or what?

    987018 wrote:
    What will happen if I do this?

    ALTER table emp
    Add primart key (employee_id, last_name);

    It's going to be two primary keys in the table?
    or what?

    It will come with an error, unless you change "primart' primary '.

    If you do, you'll get a primary key that requires a combination of the two columns to be unique.

    So, you can insert two rows with the same id of employee but different names! Not a good idea.

    Simply use a code that is used as the primary key.

    Note: you can have only one primary key for a table, but you can have many unique constraints.

  • Removal of cascade of primary key constraints

    I have 3 tables. P_1 = primary key

    P_2 P_1 and P_3 reference reference P-2 referencing P_1 column.

    I dropped the PK on P_1 constraint with the cascade option. However P_3 always reference P_1.

    I thought that this was the case when I started.

    May sound like a stupid question as how I already tried, but when I delete a constraint of Pk, a 3rd table referencing a second column in tables referring to the original column that has the constraint of PK, this 3rd column will retain its FK reference at the second table... ?

    I remember reading somewhere on what is true, that at least I am sure that I read in one of the oradocs, I just want to ask.

    Thank you.

    To make it very simple thinks this way...

    Since then, you drop PK of 1 table only means that the link between the 1st and the 2nd table is broken, but that does not drop the pharmacokinetics of the 2nd table... Also the link between the 2nd and the 3rd table is intact...

    So yes, 3rd table will maintain the relationship of FK to the 2nd table.

    Hope that's clear...

  • Hi cannot see the column in the primary key as a master detail

    I have a form master detail. In the first form master, I am unable to see the primary key
    Now, the form is based was completely and I'd also like to see the visible primary key column in the main report


    If I put the primary key as rowid then I should have seen the primary key.
    Now I can see the pencil icon (the icon of pencil to the rank nedopil) instead of the actual primary key column. who's a number of data...
    I'd like to be too visible. Can we guide what should I do for this...
    Thank you

    Are not only in the hidden items? Change the settings for them to be unhidden.

  • by specifying the index tablespace in to disable the primary key constraint

    Hello
    during the ETL I want to disable constraints PK/UK to speed up the load;
    on activation of the constraint, the corresponding indices are created in a different tablespace while they were initially on the creation of the table itself
    I tried to disable PK / UK with the option 'keep the index', the index value corresponding to the status of "unusable" and use ' alter session set skip_unusable_indexes = true', but then I can't insert rows with the error.
    Error: ORA-01502: index 'xxx' or partition of such index is in unusable state
    can anyone tell if there is a way to allow the PK/UK constraint and specify the storage space for the corresponding indices, or keep the index and inserts works?
    I'd like to avoid to drop constraints / indexes and re-create them both from scratch after the load
    Thank you for the advice
    Thank you
    Rgds

    >
    can anyone tell if there is a way to allow the PK/UK constraint and specify the storage space for the corresponding indices, or keep the index and inserts works?
    I'd like to avoid to drop constraints / indexes and re-create them both from scratch after the load
    >
    Have you tried specifying the table space in the USING clause when you activate the constraint?

    ALTER TABLE emp ENABLE PRIMARY KEY USING INDEX TABLESPACE myTS;
    

    See DBA guide
    http://docs.Oracle.com/CD/B28359_01/server.111/b28310/indexes003.htm
    >
    Creating an Index associated with a constraint

    Oracle database applies a UNIQUE key or a constraint integrity PRIMARY KEY on a table by creating a unique index on the unique key or primary key. This index is created automatically by the database when the constraint is enabled. No action is required by you when you run the CREATE TABLE or ALTER TABLE statement to create the index, but you can optionally specify an ESCALATION using clause to exercise control over his creation. This includes them when a constraint is defined and enabled, and when a constraint defined but disabled is selected.
    >
    Note the last sentence two sentences:
    >
    You can optionally specify an ESCALATION using clause to exercise control over his creation. This includes them when a constraint is defined and enabled, and when a constraint defined but disabled is selected.
    >
    You realize, I hope, that the only way can Oracle KNOWS that the index contains valid values is to rebuild?

  • How to convert a single column in the primary key column

    Hi all

    I have a column that's unique cnstraint, this column contains the value as digital and also null.

    So how do you convert to primary key column

    Concerning
    Prashant

    Prashant wrote:
    Hi all

    I have a column that's unique cnstraint, this column contains the value as digital and also null.

    So how do you convert to primary key column

    Concerning
    Prashant

    Prashant,

    You must assign new values to null values. You can use a sequence to produce the figures in this column.

    Select max (your_unique_column) from your_table; -find the maximum value of this column, so you can start the sequence of this number.

    create sequence seq_for_your_table with XXXX - value max + 1 you have found

    Update your_table set your_unique_column = seq_for_your_table.nextval () where your_unique_column is null

    You can create a primary key on this column.

    Best regards

    Grosbois

    -------------------------------------------------------
    If you answer this question, please mark appropriate as correct/useful messages and the thread as closed. Thank you

  • blocking because of the primary key constraint

    Hi all

    Recently, we migrated to 9.2.0.4 to 10.2.0.4. In the new version of oracle there was a strange behavior, we have 200 + oracle database user and there was too much blocking on a particular one table, we looked and looked for the cause but found nothing exact reason, then on the internet somewhere, we found that if a primary key is disabled and no there is also foreign then blocking is caused for example

    CREATE TABLE TEST_MST
    (
    MST_COL1 NUMBER,
    MST_COL2 NUMBER,
    MST_COL3 NUMBER
    )

    CREATE TABLE TEST_DTL
    (
    MST_COL1 NUMBER,
    MST_COL2 NUMBER,
    MST_COL3 NUMBER,
    DTL_COL1 NUMBER,
    DTL_COL2 NUMBER,
    DTL_COL3 NUMBER
    )

    ALTER TABLE TEST_MST ADD)
    CONSTRAINT PK_TM
    KEY ELEMENTARY SCHOOL
    (MST_COL1, MST_COL2, MST_COL3)) ;

    ALTER TABLE TEST_DTL ADD)
    CONSTRAINT PK_TD
    KEY ELEMENTARY SCHOOL
    (MST_COL1, MST_COL2, MST_COL3, DTL_COL1, DTL_COL2)) ;

    ALTER TABLE TEST_DTL ADD)
    CONSTRAINT FK_TM_TD
    FOREIGN KEY (MST_COL1, MST_COL2, MST_COL3)
    REFERENCES TEST_MST (MST_COL1, MST_COL2, MST_COL3));

    ALTER TABLE TEST_DTL
    DISABLE THE CONSTRAINT PK_TD;


    so, I just want to be sure that's the exact reason only blocking sessions and if yes, then why not so happning.

    Thanks and Rgds,
    Piyush
  • Change of primary key on the Table clears the other columns

    Preliminary info/Installation
    JDeveloper 11 g 11.1.1.6.0
    We have a table (object view or entity) that has 8 columns. The primary key is composed of 4 1 columns.
    The table is displayed in a page fragment.
    2 of the 4 non-primary key columns are LOV.
    All the 8 columns are needed.
    The table is editable including 2 of primary key columns.

    Question
    Assume that the table displays the 3 lines. If we change a rank 1 non-primary key column and a primary key in row 2 column, everything works well when we are committed to the database
    If, however, we first change a primary key column, then a primary key on a different line column, then the column/row primary key that changed us first, gets annihilated and the other columns LOV on this same line. The 2 columns that are not LOV keep data as it should. We then get a validation error because the required data are not present. All this happens before we try all commit to the database. If debug us and look the iterator, it has the old data in this document, before and after any performance on the binding.
    If we look at and update the data via the App Module, everything works well. We have tried to update the view, etc. through valueChangeListener also with no luck.

    If you change the primary key value, it should not be the primary key.

    This is true in the design of database relational regardless of how access you the data. In addition, EO don't like if you change the primary key value.

    Have you considered adding a surrogate key (use a sequence) and make your PK course a unique key?

    John

  • Two constraints of primary key of a table with two different constraint name

    Hi guys

    I am creating the table below

    Table name: TITLE_COPY

    Column name Copy_id Title_id Status

    Key type

    PKPK, FK
    UniqueNN, UNN, UNN
    CheckAvailable, destroyed, leased, reserved
    Table Ref FKTitle
    FK Ref ColTitle_id
    Data typeNumberNumberVarchar2
    Length101015

    The SQL statement below can create it

    CREATE TABLE title_copy (copy_id NUMBER (10),)

    title_id CONSTRAINT NUMBER (10) title_copy_title_id_fk title (title_id) REFERENCES.

    primary key constraint title_copy_title_id_pk title_id (copy_id)

    status VARCHAR2 (15) NOT NULL CONSTRAINT status_ck CHECK (status IN ('AVAILABLE', 'DESTROYED', 'RENTAL', 'RESERVED'))

    );

    Two primary keys (title_id, copy_id) are created with the same name of constraint (title_copy_title_id_pk) (-> table-level constraint). What happens if I want to have two different names of the primary key constraint, say title_copy_title_id_pk for the title_id column and title_copy_copy_id_pk for the copy_id column?

    I can do that with a column-level constraint? Something like below (complains about the additional primary key)

    CREATE TABLE title_copy (copy_id NUMBER (10) CONSTRAINT title_copy_copy_id_pk PRIMARY KEY,)

    constraint title_copy_title_id_pk primary key (title_id).

    title_id CONSTRAINT NUMBER (10) title_copy_title_id_fk title (title_id) REFERENCES.

    status VARCHAR2 (15) NOT NULL CONSTRAINT status_ck CHECK (status IN ('AVAILABLE', 'DESTROYED', 'RENTAL', 'RESERVED'))

    );

    In summary, I can define two primary keys with the name of different constraint in a column-level constraint?

    There are no two primary keys. There is a primary key made up of two columns - also called a composite key.

    A table can have at least a primary key. You can, if you wish, create a unique constraint on a different set of columns.

  • By specifying the Composite primary key (columns 4 and 5)

    Hi all

    I am new to Oracle Apex,

    I have a few paintings with a composite primary key (4 or 5 columns)... When I try to create a primary key constraint, it gives me only 3 columns!

    Any help please in how can I solve this problem?

    Thanks in advance

    You are probably using the workshop for this sql Object Explorer

    which seems to be limited to 4 columns on APEX 4.2.3

    best thing to do is to create manually using the sql commands in sql workshop, here's an example:

    {code}

    CREATE UNIQUE INDEX myui ON JC_ACCOUNT

    (TITLE_ID, FAMILY_NAME GIVEN_NAME, GENDER_ID, TELEPHONE_NUMBER)

    /

    {code}

    I hope this helps you

    KR

    Martin

Maybe you are looking for

  • Blue tint to the screen

    Now my screen has a blue tint and when you start the screen is not black and blue

  • Error during installation of updates (KB958869)

    When you try to install the update "Security Update for Windows Vista for x 64-based systems (KB958869) I have an error message' WindowsUpdate_8007065E * original title - when you try to install the update "security update for Windows Vista (KB958869

  • Received a phone call from someone who said he could help me because his company had received the error messages from my computer - XINFINITY?

    So I recently received calls from this guy who claims to be with Microsoft Error Reporting service, but I thought something was fishy when he couldn't tell me anything specific on my computer, which he would be able to know right away. Also, he could

  • F10 does not

    I have an Acer Aspire 5349-2592 running Windows 7.  It will not start.  I have a set of recovery discs and have tried to recover them using the standard process, start the computer and press F10 when the logo appears.  It does not help, I get the sta

  • Declaring new file c ++ for the new qml file

    I have a qml file, the button I click on function Boutonclique clal #include "applicationui.hpp" #include "homepage.hpp" #include #include #include #include #include #include #include using namespace bb::cascades; AbstractPane *root; ApplicationUI::A