Primary Key Issue With Creating Tables

Hi,
I have the following scripts to create my two tables in oracle 10g, i was wondering wether or not i had correctly set up my primary key or if there is a better way of doing it than the way i have.
Here are my two scripts for my tables:
CREATE TABLE CUSTOMER (
fname varchar2(15) NOT NULL,
lname varchar2(20) NOT NULL,
age varchar2(3) NOT NULL,
custAreaCode number(5) NOT NULL,
custNumber number(6) NOT NULL,
constraint cust_pk PRIMARY KEY (custAreaCode),
constraint cust_pk2 PRIMARY KEY (custNumber),
constraint age_chk CHECK (age >=18 AND age <150)
CREATE TABLE PHONECALL (
startDateTime smalldatetime NOT NULL,
custAreaCode number(5) NOT NULL, ---Reference PK From Customer Table
custNumber number(6) NOT NULL, ---Reference PK From Customer Table
dialledAreaCode number(5) NOT NULL,
dialledNumber number(6) NOT NULL,
crgPerMinute number number (3,1) NOT NULL,
endDateTime smalldatetime NOT NULL));
I am not sure if i have referenced the primary keys correctly in the PHONECALL table.
Thanks in advance :)

Hi,
You want like this below ? I think that smalltime data type is not a valid type. Other thing, this is not a rule, but I advice you to put the primary key columns as the first columns of your table. One question: There is no PK on the phonecall table ?
SGMS@ORACLE10> create table customer (
  2  custareacode number(5) not null,
  3  custnumber number(6) not null,
  4  fname varchar2(15) not null,
  5  lname varchar2(20) not null,
  6  age varchar2(3) not null,
  7  constraint cust_pk primary key (custareacode),
  8  constraint cust_uk unique (custnumber),
  9  constraint age_chk check (age >=18 and age <150)
10  );
Table created.
SGMS@ORACLE10> create table phonecall (
  2  custareacode number(5) not null constraint fk_phone_cusarecode_customer references customer,
  3  custnumber number(6) not null constraint fk_phone_custnumber_customer references customer,
  4  startdatetime date not null,
  5  dialledareacode number(5) not null,
  6  diallednumber number(6) not null,
  7  crgperminute number (3,1) not null,
  8  enddatetime date not null);
Table created.
SGMS@ORACLE10> select table_name,constraint_name,constraint_type from user_constraints
2 where table_name in ('CUSTOMER','PHONECALL') and constraint_type in ('P','U','R');
TABLE_NAME                     CONSTRAINT_NAME                C
CUSTOMER                       CUST_PK                        P
CUSTOMER                       CUST_UK                        U
PHONECALL                      FK_PHONE_CUSARECODE_CUSTOMER   R
PHONECALL                      FK_PHONE_CUSTNUMBER_CUSTOMER   RCheers

Similar Messages

  • How to specify  tablespace for a primary key inde in create table statement

    How to specify the tablespace for a primary key index in a create table statement?
    Does the following statement is right?
    CREATE TABLE 'GPS'||TO_CHAR(SYSDATE+1,'YYYYMMDD')
                ("ID" NUMBER(10,0) NOT NULL ENABLE,
                "IP_ADDRESS" VARCHAR2(32 BYTE),
                "EQUIPMENT_ID" VARCHAR2(32 BYTE),
                "PACKET_DT" DATE,
                "PACKET" VARCHAR2(255 BYTE),
                "PACKET_FORMAT" VARCHAR2(32 BYTE),
                "SAVED_TIME" DATE DEFAULT CURRENT_TIMESTAMP,
                 CONSTRAINT "UDP_LOG_PK" PRIMARY KEY ("ID") TABLESPACE "INDEX_DATA"
                 TABLESPACE "SBM_DATA";   Thank you
    Edited by: qkc on 09-Nov-2009 13:42

    As orafad indicated, you'll have to use the USING INDEX clause from the documentation, i.e.
    SQL> ed
    Wrote file afiedt.buf
      1  CREATE TABLE GPS
      2              ("ID" NUMBER(10,0) NOT NULL ENABLE,
      3              "IP_ADDRESS" VARCHAR2(32 BYTE),
      4              "EQUIPMENT_ID" VARCHAR2(32 BYTE),
      5              "PACKET_DT" DATE,
      6              "PACKET" VARCHAR2(255 BYTE),
      7              "PACKET_FORMAT" VARCHAR2(32 BYTE),
      8              "SAVED_TIME" DATE DEFAULT CURRENT_TIMESTAMP,
      9               CONSTRAINT "UDP_LOG_PK" PRIMARY KEY ("ID") USING INDEX TABLESP
    ACE "USERS"
    10               )
    11*              TABLESPACE "USERS"
    SQL> /
    Table created.Justin

  • Primary key issue with adf Entry Form.

    Hi All,
    i'm using jdev version 11.1.1.5.0
    use case: i have create simple entry form based on Eo and Vo using database table like student(enrollment no,name address)
    where enrollment no is primary key.
    so when i have create a record i have set enrollment no in entity impl class of this eo create method using some logic based on my need like(20130001)
    for that i have read highest no from database field and assign to enrollment no field when user create record.
    so when user create record second time then enrollment no is 20130002. and other detail like name and address user fill and commit record. and it is fine.
    but problem is that when two user access same form at a time and create record so both have get same primary key like 20130003 because in current time in database maximum value is 20130002.
    so which user  commit record first it record will save on database and second user get error message because of primary key violation.
    so my question is that where we generate primary key value for record so when multiple user access form have get different primary key value. and in my use case i can't use sequence and any autoincrement no
    because i have patter for primary key.
    Thanks in Advance
    Manish

    Hi,
    Dimitar and Frank
    thank for reply.
    How can i apply non-concurrent DB lock can you please explain.(because lock method on entity impl not work when user create new row as per documentation)
    http://docs.oracle.com/cd/B14099_19/web.1012/b14022/oracle/jbo/server/EntityImpl.html#lock__
    i have write following line of code in entity impl class to set primary key value(reqid)-
        @Override
        protected void prepareForDML(int i, TransactionEvent transactionEvent) {
             super.prepareForDML(i, transactionEvent);
            this.setReqid(genReqid());
        public String genReqid() {
            String reqby = this.getReqby();
            String qry =
                "SELECT nvl(MAX(TO_NUMBER(SUBSTR(REQID,7))),0)+1  FROM STM_REQHDR WHERE REQBY=? AND REQTYPE<>'M' and substr(reqid,1,2)<>'MT' AND SUBSTR(REQID,1,3)<>'PAY'";
            PreparedStatement ps = null;
            String no = "";
            try {
                ps = getDBTransaction().createPreparedStatement(qry, 0);
                ps.setString(1, reqby);
                ResultSet rs = ps.executeQuery();
                if (rs.next()) {
                    no = rs.getString(1);
                ps.close();
            } catch (Exception e) {
                System.out.println("Exception in Gen req id ==>" + e);
            String reqno = reqby + String.format("%6s", no).replace(' ', '0');
            return reqno;
    but when i run form in debug mode and two user commit concurrent manner only one time code block is executed who first commit. and second user got error message.
    thanks
    Manish

  • How to find out the primary key of oracle apps tables

    Hii,,
    My Question is How to find the primary key in the apps table...Is there any Query or other way to find out???
    for eg.
    I want to find out the primary key of the AP_INVOICES_ALL table...just tell me how can i get the primary key of this table.
    I am currently using toad for the query..
    Please guide me...

    you can define the primary key when you create table
    or add the primary key after the table creation by 'ALTER TABLE ... ADD constraint pk_nme primary key (col1, col2)';
    you could use below sql to check the detail of primary key:
    1) check out the table definition directly:
    select dbms_metadata.get_ddl('TABLE','EMP') FROM DUAL;
    2) check out the columns of primary key:
    select * from user_constraints where constraint_type='P' AND table_NAME='EMP';
    select * from user_cons_columns where CONSTRAINT_NAME='PK_EMP';And BTW, it is madam, not sir. :)
    Edited by: PhoenixBai on Dec 17, 2010 1:07 PM

  • How can I use the SQL to create a primary key for a existing table?

    create table a(bm number,mc varchar2(20));
    when the table was created,i want to make the column bm as
    the primary key and my SQL is "alter table a enable primary key bm",the system show
    me error,how can I write the right one?

    create table a(bm number,mc varchar2(20));
    when the table was created,i want to make the column bm as
    the primary key and my SQL is "alter table a enable primary key bm",the system show
    me error,how can I write the right one? You do not have any primary key defined on your table yet, so, it does not make sense to enable it (if at all possible) !
    You need to add PRIMARY KEY using something like this:
    SQL> alter table a add constraint pk_a_bm primary key (bm) ;

  • For the Primary Key Field with the combination of other fields

    Hai
    I Have a problem in creating trigger for the following
    the problem goes like this..
    i have a table with the fields
    fld1 (varchar2(6) fld2(varchar2(20)) fld3 (number)
    the fld1 is a primary key.
    here in this table i am inserting the values ..
    At the time of inserting the fld1 should get the part value of fld2
    say if new value for fld2 is "SAM & CO" then
    it should take the first letter and then followed by the sequence..
    i.e., the fld1 is the combination of a
    letter + firt letter of the fld2 + sequence
    fld1 fld2 fld3
    CS001 SSSSS 324234
    CP001 PPPPP 5345435
    CS002 SSSS 53543543
    Here the Sequence should vary depending on the alphabet of the fld1
    if P the the Sequence should be the next number of fld1 to P
    i.e.,
    if i add the value like this
    insert into tname(fld2,fld3) values('PQQQQ',34343)
    then it should be inserted as
    CP002 PQQQQ 34343
    I need the solution for this..
    Thanx
    Gaya3
    thanx
    gaya3

    There are not enough details to be sure since you have not provided the mappings. From just the error, it looks like you are using the tableC.tableA_ID field as the foreign key in the ManyToOne relationship to A, but have marked it as insertable=false, writeable=false, meaning that it cannot be updated or used for inserts.
    Either make it writable (set the settings to true), or add another basic mapping/attribute in the entity for TableC that maps to the field which you can use to set when you insert a new tableC entity. A simple example is available at
    http://wiki.eclipse.org/EclipseLink/Examples/JPA/2.0/DerivedIdentifiers
    Best Regards,
    Chris

  • ADF BC Primary key generation with SQL Server DB

    Hi,
    I am using ADF 11.1.1.6 to develop a small application that will do some very basic CRUD operations on a SQL Server DB.
    I read through http://www.oracle.com/technetwork/developer-tools/jdev/multidatabaseapp-085183.html before I'm starting to implement the Entity objects.
    This document describes a way to do primary key generation using a table created in the DB and instructs to create an application connection to the Database :
    +2. Create a Connection to the Table+
    In your application, create a database connection named ROWIDAM_DB that points to the database containing your S_ROW_ID table. Alternatively, edit your BC project's properties and add the following Java option to the project's run configuration:
    -Djbo.rowid_am_conn_name= appconnection
    where appconnection is the name of a database connection that points to the S_ROW_ID table.
    My question is how do we do this when we mve to a production environment ?
    Also I'll be interested to hear if anyone has any pointers for developing ADF apps with SQL Server. (gotchas, performance pitfalls etc. )
    -Jeevan
    Upadte : This is SQL Server 2005
    Edited by: Jeevan Joseph on May 2, 2012 9:04 AM

    my apologies to everyone ... This should have been very simple. I just need to provide the config in my AM configuration(bc4j.xml)
    jbo.rowid_am_conn_name* should be set to the connection name you create. For production deployments, theres a similar
    jbo.rowid_am_datasource_name* that should work just fine (though I havent tried if it has any hiccups).
    I'd like to point out one thing though, for whoever might stumble upon this thread and find it useful later on...
    After I did the steps above, everything seemed to work when I tested the app from the AM tester. But when I built a UI for it in ADF Faces, I started getting an exception on Create/CreateInsert :
    java.lang.ClassCastException: com.microsoft.sqlserver.jdbc.SQLServerConnection cannot be cast to oracle.jdbc.OracleConnection
         at oracle.jbo.server.OracleSQLBuilderImpl.setSessionTimeZone(OracleSQLBuilderImpl.java:5533)
         at oracle.jbo.server.DBTransactionImpl.refreshConnectionMetadata(DBTransactionImpl.java:5311)
         at oracle.jbo.server.DBTransactionImpl.initTransaction(DBTransactionImpl.java:1194)
         at oracle.jbo.server.DBTransactionImpl.initTxn(DBTransactionImpl.java:6826)
         at oracle.jbo.server.DBTransactionImpl2.connect(DBTransactionImpl2.java:136)
         at oracle.jbo.common.ampool.DefaultConnectionStrategy.connect(DefaultConnectionStrategy.java:213)The trouble is that the ADF Faces adf-config.xml overrides the AM configuration. Oracle is the default, and it overrides the SQL flavor I set when initializing the Model project.
    This was not mentioned in the original document probably because ADF faces was out of scope for that document.
    I also found this thread extremely useful, and its what reminded me of the ADF Faces AM config overrides : Re: Locking mode 'optupdate' with SQL92
    Cheers !
    Jeevan

  • How many primary keys use in one table

    Hi,
    Please help me. Maximum How many primary keys use with in one table
    Regards,
    Sunil Kumar.T

    Hi,
    For my knowledge, It depends on the Database & version u r working.. Right..
    This is a sample description what I seen for a Database...
    Technical Specification of SAP DB Version 7.4
    Description                            Maximum Value
    Database size                    32 TB (with 8 KB page size)
    Number of files/volumes per database64...4096, specified by a configuration parameter
    File/volume size (data)      518 ...8 GB (dependent on operating system limitations)
    File/volume size (log)1      6 TB (dependent on operating system limitations)
    SQL statement length>=  16 KB (Default value 64 KB, specified by a system variable)
    SQL character string lengthSpecified by a system variable
    Identifier length                32 characters
    Numeric precision              38 places
    Number of tables unlimited
    Number of columns per table (with KEY)  1024
    Number of columns per table (without KEY)  1023
    Number of primary key columns per table  512
    Number of columns in an index  16
    Reward Points if useful

  • Hello Gurus..... ISSUE with child Table update

    I have an issue with child table update
    I have created a GTC with one parent table and two child tables. I'm able to update the parent table and the values are found in db, but the ISSUE is the child Table values are not updating the db.
    please give me a solution
    regards
    Srikanth

    If you are keeping referential integrity in the database, not in the application, it is easy to find the child and parent tables. Here is a quick and dirty query. You can join this to dba_cons_columns to find out on which columns the referential constraints are defined. This lists all child-parent table including SYS and SYSTEM users. You can run this for specific users of course.
    select cons1.owner child_owner,cons1.table_name child_table,
    cons2.owner parent_owner,cons2.table_name parent_table
    from dba_constraints cons1,dba_constraints cons2
    where cons1.constraint_type='R'
    and cons1.r_constraint_name=cons2.constraint_name;

  • Altering Primary Key constraint on a table i Oracle 10G

    Hi All,
    Can anyone tell me how to alter a primary key constraint on any table. My concern is that, suppose i have a table called 'Employee' where only 'EmployeeName' is added as a primary ket constaint. Now i want to alter this P.K. constarint to add 'EmployeeName' and 'DateOfBirth' as a primary key. Can anyone suggest me how can i achieve that? Any help will be highly appreciated.

    hi,
    you need to drop the constraint and recreate it.
    SQL> conn scott/tiger@alpspso
    Connected.
    SQL> create table test (id number constraint id_pk primary key,name varchar(30));
    create table test (id number constraint id_pk primary key,name varchar(30))
    ERROR at line 1:
    ORA-00955: name is already used by an existing object
    SQL> create table test_table (id number constraint id_pk primary key,name varchar(30))
    Table created.
    SQL> alter table test_table modify constraint id_pk primary key(id,name);
    alter table test_table modify constraint id_pk primary key(id,name)
    ERROR at line 1:
    ORA-00933: SQL command not properly ended
    SQL> alter table test_table modify constraint id_pk(id,name);
    alter table test_table modify constraint id_pk(id,name)
    ERROR at line 1:
    ORA-00933: SQL command not properly ended
    SQL> alter table test_table modify primary key(id,name);
    alter table test_table modify primary key(id,name)
    ERROR at line 1:
    ORA-00933: SQL command not properly ended
    SQL> alter table drop constraint id_pk;
    alter table drop constraint id_pk
    ERROR at line 1:
    ORA-00903: invalid table name
    SQL> alter table test_table drop constraint id_pk;
    Table altered.
    SQL> alter table test_table add constraint id_pk primary_key(id,name);
    alter table test_table add constraint id_pk primary_key(id,name)
    ERROR at line 1:
    ORA-00902: invalid datatype
    SQL> alter table test_table add constraint id_pk primary key(id,name);
    Table altered.
    Regards.
    Navneet

  • Primary key issue in toplink workbench

    Hi All,
    I am getting No primary keys specified in (xxx is the table name) table error in workbench.
    I my table,don't have any primary keys,i try to do direct to field mapping for all the fields with the table.
    After mapping,was getting this warning.
    Is there any mandatory thing in toplink workbench that we need to have atleast one primary key defined in a table.
    Please let me know!!!!!!!!!!!!
    Appreciate your hekp............

    Yes, it needs to know how to uniquely identify the row to do deletes and updates, etc. If you have no PK, then actually every field is needed to uniquely identify the row, and therefore just slect every field in the MW as being the PK... Note that the PK specified in the MW doesn't have to actually be the PK on the database, it just has to be unique for when TL does update/delete, etc.
    - Don

  • Issues with Advance Table Add Row New Row not work in some scenarios.

    Hi,
    Wondering if there's any issue with Advanced Tables where it does not create any rows. I don't know if anyone tried this or not. I have one OA Page with Advanced Table and a button that when clicked open a new OA Page in a POP-UP Window. The pop-up page conatins one textbox where u enter a data and this gets saved in one of the VO's transient attribute. Now on the ase page if you don't click a button to open a pop-up page you can Add New Rows in the Advanced Table by clicking Add Row Button. But as soon as you open a popup window and close it Add New Rows button doesn't work and is not creating any new rows. Basically page stops working. Both the POP-UP and the base page share the same AM but have different controllers.
    POP-UP page is a custom page that I open giving the Destination URI value in the button item and target frame _blank.
    I even tried creating rows programmatically for Advance Table but this too doesn't work once u open a pop-up. Also I have used pageContext.putTransactionValue in the pop-up page and am checking and removing this in the base page.
    Any help is appreciated.
    Thanks

    anyone

  • .Issue with creating a photo book in Iphoto error message states some frames are missing images, but I've checked each one and from I can see they are all filled in. It won't let my buy the book though

    Having an Issue with Creating a photo Book in Iphoto. I continue to get an error message when clicking on "Buy Book" The error indicates that I'm missing a frame or frames from the book. I've checked this many times and cannot locate the issue. The book appears to be complete from my perspective. What can I do now? I worked very hard on this and would hate to lose it all.

    How did you check the book? Did you preview it?
    See:
    iPhoto, Aperture: Previewing an order in iPhoto or Aperture
    Is that the message?
    "your book seems to have frames on one or more pages that do not contain photos. You must either change the layout of those pages or place photos in those frames before you can buy this book."
    The most common reason for this error is, that you have used a page layout that needs a background photo and you have not yet added the backdrop. Then the book is still showing some default grayscale image.
    Check, if one of your page layouts is using this background:
    Then you need to add a photo to the background of the page.

  • Updation of Primary Key field in HR Tables (PA2001 and PA2002) - Urgent

    Can anyone please tell me how to update a primary key field in HR Tables (PA2001 and PA2002).
    I need to update sprps field in both the tables. I used HR_Infotype_Operation function module, but still it is not updating the field.
    Pls find the following code snippet for the table PA2001 and let me know if any discrepancies :
    TABLES: pa2001.
    DATA: it_pa2001 TYPE TABLE OF pa2001,
    wa_pa2001 LIKE LINE OF it_pa2001,
    DATA: date TYPE d.
    date = sy-datum - 100.
    SELECT pernr sprps begda endda FROM PA2001 INTO CORRESPONDING FIELDS OF TABLE it_pa2001
    WHERE begda BETWEEN date and sy-datum.
    WRITE:/.
    WRITE:/ 'PA 2001 Records'.
    if sy-subrc <> 0.
    WRITE:/ 'No Data Exists'.
    else.
    LOOP AT it_pa2001 INTO wa_pa2001.
    WRITE:/ wa_pa2001-pernr, wa_pa2001-sprps, wa_pa2001-begda, wa_pa2001-endda.
    ENDLOOP.
    endif.
    LOOP AT it_pa2001 INTO wa_pa2001.
    wa_pa2001-sprps = 'X'.
    CALL FUNCTION 'HR_INFOTYPE_OPERATION'
    EXPORTING
    infty = '2001'
    number = wa_pa2001-pernr
    SUBTYPE =
    OBJECTID =
    LOCKINDICATOR =
    VALIDITYEND =
    VALIDITYBEGIN =
    RECORDNUMBER =
    record = wa_pa2001
    operation = 'MOD'
    TCLAS = 'A'
    DIALOG_MODE = '0'
    NOCOMMIT =
    VIEW_IDENTIFIER =
    SECONDARY_RECORD =
    IMPORTING
    RETURN =
    KEY =
    ENDLOOP.
    if sy-subrc = 0.
    write:/ 'SY-subrc is zero'.
    write:/ ' Rows modified = ', sy-dbcnt.
    else.
    write:/ 'No Record(s) updated'.
    endif.
    commit work.
    WRITE: / 'Updated Records in the Internal Table IT_PA2001'.
    SELECT pernr sprps begda endda FROM PA2001 INTO CORRESPONDING FIELDS OF TABLE it_pa2001
    WHERE begda BETWEEN date AND sy-datum..
    LOOP AT it_pa2001 INTO wa_pa2001.
    WRITE:/ wa_pa2001-pernr, wa_pa2001-sprps, wa_pa2001-begda, wa_pa2001-endda.
    ENDLOOP.

    Thanks Kiran.
    But I need to change this field as we need to lock the records, as soon as the employee fills his/her time sheets which will be sent for HR Payroll, later on.
    When I am updating directly the field the database table PA2001, its not updating but for table PA2002, only few records are being updated.
    Is there any other alternative for this problem?
    Pls reply.
    Thanks,
    Harish

  • Issue with Creating Forecast Profiles/Forecasting

    Hello Experts,
    We are facing an issue with creating forecast profiles.
    We have two FYVs defined in our system, one with 52/53 fiscal weeks (Fiscal Variant W1) and the other with 12 fiscal months (FYV M1), Our storage buckets profile (STP) uses W1,  since we have many dataviews that use W1 as the FYV in the attached Planning buckets profile(PBP). We also have dataviews that display in fiscal months (use FYV M1 in the attached PBP), data from Fiscal weeks will be aggregated and shown in Fiscal months . We need to do forecasting using FYV M1, that is forecast in Fiscal Months , but since the storage bucket profile has W1 which is used in the Planning Area config. We are unable to create any Forecast profiles with FYV M1 . Please note that we cannot use M1 in STP because when we used M1 in STP, we could not create  dataviews in fiscal weeks (using W1)
    1. Is there any way we can forecast using M1 while having assinged W1 to the Planning area/STP ?
    2. OR we are willing to assing M1 to PA, provided we can use W1 in some of the dataviews, unfortunately we were unable to do this, though the vice versa is possible i,e. we could have W1 in STP and M1 in some of the related PBPs/ Weekly Dataviews.
    Please let me know if any of these are possible or if there is any alternative way to do forecasting in Fisc Months.
    Thanks
    Tej

    Hi,
    You are correct, the Storage bucket profile always has to be at a detailed level. Time bucket profile can be at higher levels like monthly, quarterly etc.,
    Coming to the root of your problem, which is you are unable to forecast at a level other that what is specified in your Storage bucket profile, unfortunately the answer is no.
    You can do a forecast only at the level at which the data is stored and not at the level at which the data is viewed.
    One work around for this is to create an additional planning area with the same MPOS and this addl PA can contain only those bare minimum KF required for your forecast. After you generate your forecast, you can copy it to your Weeks based PA and then proceed from there. This copy of KF between PA is much faster as it will happen at LC level and should not cause time delays.
    NOTE - You have to exercise caution when you are using 2 periodicities i.e., weeks and months, if you are using standard SAP calendar then you are good to go. If you are creating custom Fiscal variants pls ensure the start and end of a month is same in both the weekly and monthly variants. Failing which, there will be mismatch of data between the two dataviews.
    Hope this helps.
    Thanks
    Mani Suresh

Maybe you are looking for