SQL & PL/SQL :: Updating Sequence With Order Of Old Composite PK
Sep 5, 2011
I am replacing a composite PK with a new single PK as business changed. I am creating sequence to fill the new field that should present the PK but I need to fill it according to the values of the old composite PK.
I need to update ABC and set ABC_SERIAL = SEQ_ABC.nextval but i need this to be done according to the order of the old composite primary key... i.e. rows with COMP_PK1 = 1 are filled before rows with COMP_PK1 = 2 and so on..
I have migrated database from postgresql to oracle...All sequences are migrated with their default values...(Start with 1) I already have 213 entries in a table and I want to begin using this for 214th entry ( replace with "start with 214")
How can I automate the process of updating "Start with" value of sequence with the max no of entry on my table every time I migrate data....
I have created a trigger that will automatically insert the next number from the sequence into the id column.
create trigger test_trigger before insert on test for each row begin select test_seq.nextval into :new.id from dual; end; /
Order OrderID Status OrderItems OrderID EAN Amount Store EAN Amount
now,I need trigger that will on updating table Order and changing status to "GoodsReceived" increase amounts in Store according to values in OrderItems.
create or replace trigger order_received_trigger before update on Order for each row begin if (:old.status=4 and :new.status=1) then /* select ean, sum(amount) from OrderItems where OrderID=:old.OrderID group by ean; */ end if; end;
but now i dont know how to apply that select on table Store.
I have to find the next EMP whose MAX_SQUENCE needs to be updated with a sequence. In this case the next would be '010' I fetched the EMP who falls after the max value of the column max_sequence in order of EMPCODE.
select emp_next from ( SELECT emp,max_sequence, LEAD(emp, 1, 0) OVER (ORDER BY emp) AS emp_next from test_emp_wip where wip > 0 ) where max_sequence=(select max(max_sequence) from test_emp_wip where wip > 0) EMP_NEXT 010 After sometime, the record would look like :-
Name Null Type --------------------------- -------- ------------- RPTNO NOT NULL NUMBER RPTDATE NOT NULL DATE RPTD_BY NOT NULL VARCHAR2(25) PRODUCT_ID NOT NULL NUMBER
describe rptbody
Name Null Type ------------- -------- ------------- RPTNO NOT NULL NUMBER LINENO NOT NULL NUMBER COMMENTS VARCHAR2(240) UPD_DATE DATE
The fact is that we store some header in RPTHEAD and store real data in RPTBODY, the question is that if I use below SQL to query all data for a 'PRODUCT_ID'.
SELECT t0.LINENO, t0.COMMENTS, t0.RPTNO, t0.UPD_DATE FROM RPTBODY t0 , RPTHEAD rpthead WHERE ( t0.RPTNO = rpthead.RPTNO AND t0.UPD_DATE>=to_date('1970/01/01 00:00:00','YYYY/MM/DD hh24:mi:ss') AND rpthead.PRODUCT_ID IN ('4647') )
I do not want to have 'ORDER by' clause since data set is too large, the sorting takes long time, is there any way to get the result rows in the order sorted by RPTNO? We have the index for RPTNO on RPTBODY.
concept of composite and candidate keys. i have gone through all the definition part but did not understand the concept of creating candidate key for example:-
I have the following 3 columns in my VERSIONS table.
MAJOR MINOR MICRO
The MAJOR,MINOR and MICRO columns create a unique composite primary key. I need to query out the last version by doing the following 3 queries and combine them.
select max(major) as max_major from versions select max(minor) as max_minor from versions where major = :max_major select max(micro) as max_micro from versions where minor = max_minor and major = max_major
Is there any way I can query out max(major),max(minor),max(micro) in a single query.
I have a lot of queries on a table with WHERE clause:
WHERE CR_DATE > :y AND CR_VALUE = :x
Actually, there is an index on (CR_DATE) but much more selective index is on (CR_DATE, CR_VALUE). If I do not UPDATE any records on this table, is there any difference in INSERT operation (or another problem) when I replace actual index with multi-column index?
I am following Lynda Tutorial for Normalization. I have understand normalization.The core concept of primary key is that it uniquely identifies each record in the table, but here in the given below image the 'COURSE' field is repeating 'SQL101' value again and again but still the teacher in the video is calling it primary key and combination of date & Course column Composite key.how can a field which vales are repeating can be called as Primary Key?
I have to create six tables as part of my assignment. I have created 5 tables. I am having trouble with the 6th. How can I reference a foreign key to a composite primary key? I have colored red the areas that I think are giving me trouble which is in the 6th table, the Registration Table.
Create Table Student ( StudentID number (6,0) NOT NULL, Student_Name varchar2 (20) NOT NULL, Constraint Student_PK Primary Key (StudentID) );
Create Table Faculty ( FacultyID number (5,0) NOT NULL, Faculty_Name varchar2 (20) NOT NULL, Constraint Faculty_PK Primary Key (FacultyID) );
Create Table Course ( CourseID varchar2 (10) NOT NULL, Course_Name varchar2 (20) NOT NULL, Constraint Course_PK Primary Key (CourseID) );
Error starting at line 1 in command: Create Table Registration ( StudentID number (6,0) NOT NULL, Section_No number (5,0) NOT NULL, Semester varchar2 (7) NOT NULL, Constraint Registration_PK Primary Key (StudentID, Section_No, Semester), Constraint Registration_FK1 Foreign Key (StudentID) references Student (StudentID), Constraint Registration_FK2 Foreign Key (Section_No) references Section (Section_No, Semester, CourseID), Constraint Registration_FK3 Foreign Key (Semester) references Section (Section_No, Semester, CourseID) )
Error at Command Line:8 Column:104 Error report: SQL Error: ORA-02256: number of referencing columns must match referenced columns 02256. 00000 - "number of referencing columns must match referenced columns" *Cause: The number of columns in the foreign-key referencing list is not equal to the number of columns in the referenced list. *Action: Make sure that the referencing columns match the referenced columns.
I have an employee table which has a primary key and a self referencing foreign key, as shown here
create table employee ( id not null, name not null, department not null, supervisor_id not null ,constraint constraint_1 primary key (id) ,constraint constraint_2 foreign key (supervisor_id) references employee (id));
Now if i make the primary key composite, as shown below -
create table employee ( id not null, name not null, department not null, supervisor_id not null ,constraint constraint_1 primary key (id, name) ,constraint constraint_2 foreign key (supervisor_id) references employee (id));
Oracle is throwing the following error -
ORA-02270: no matching unique or primary key for this column-list
How can this error be fixed without changing the composite primary key?
emp_id number, name varchar2(30), from_dt date, remarks varchar2(60)
insert into MY_TAB values (1,'TOM','01-JAN-13', 'some remark'); insert into MY_TAB values (1,'TOM','02-JAN-13', 'some remark'); insert into MY_TAB values (2,'TOM','01-JAN-13', 'some remark'); insert into MY_TAB values (3,'TOM','01-JAN-13', 'some remark'); insert into MY_TAB values (4,'TOM','01-JAN-13', 'some remark'); insert into MY_TAB values (4,'TOM','02-JAN-13', 'some remark');
How do I ensure that when a user tries to insert record with emp_id as 1, then he should only be allowed to enter another from_dt but the value in the name column have to be the same as in the previous row of emp_id 1.
insert into MY_TAB values (1,'TOOM','03-JAN-13') --shld not be allowed.
I need to create a composite unique index on varchar2, number and CLOB column. I haven't used such index before that have the CLOB column indexing. I found the below link related to CLOB indexing...
[URL]......
Links from where I can get related info. Also I would like to know the impact of such index on performance. I have to store and process around 50 million records in such a way, will it be beneficial to use this index?
I Want to tune the attached query. I have tried by creating the normal indexes and composite indexes on the fields . I feel that , Only normal index is required for this instead of composite index?
I have created an non unique index lk_fein on lookup_fein( code,map_id,trash). When I check the explain plan it does a full table scan on lookup_fein. if I force it to use index by it does and the cost also decreases.
I am using oracle 9i, and having trouble with updating a table.
I get ORA-00001(unique constraint) Error on executing the sql below; I know sql below is little strange( which use unique key in 'SET' statement) . but It worked on My Oracle Server. but it didn't on Client's.
why this error occurs or why this error did not occur on my PC,
[Update sql](key is CD and SDATE) Update TBL1 set CD = 'A',
i was just working on one of my SQL assignments from my database management course, and thus far, this is the first that I just can't figure out. The question is:
Quote: Increase the credit limit of any customer who has any order that exceeds their credit limit. The new credit limit should be set to their maximim order amount plus $1,000. This must be done in 1 SQL statement
The bolded part is what I'm having trouble with.
What I have thus far:
UPDATE Customers SET CreditLimit = 1000 + (SELECT MAX(Amount) FROM Orders, Customers WHERE Cust = CustNum) WHERE CustNum IN ( SELECT Cust FROM Orders WHERE Cust = CustNum AND CreditLimit < Amount);
So there's two tables that I'll be working with, Customers (the table I'm updating), and Orders (the table where the order amount is found). With the code I have so far, it does seem to be updating the correct tables at the very least, but not with the correct values. It's essentially updating the CreditLimit column with the new value of 1000 + the maximum amount in the order table, which is very close to what I want it to do, but I want it to be 1000 + the maximum amount for that specific customer.
CustNum is the primary key for the Customers table, and Cust is the foreign key that links each together.
(about the formatting, it looked much prettier in SQL Worksheet Plus)
insert into topdUIDXML select '<filter name="test" topologyUID="1">' from dual; insert into topdUIDXML select '<filter name="test2" topologyUID="2">' from dual; insert into topdUIDXML select '<filter name="ftest" topologyUID="3">' from dual; insert into topdUIDXML select '<filter name="qtest" topologyUID="4">' from dual;
I am trying to find a way to update all of the rows in a table for a column EXCEPT for the very first row. I am not sure if this can be done while I enter my SET parameter or not. I have also thought about using an EXCEPTION in a stored procedure. For example, say I have the table listed below:
SQL> select * from example1;
CODE1 I_ID CODE2 J_ID NAME1 DATE1 ----- -------------------- ----- -------------------- -------------------------------- --------------- A 100 A 200 John 20111225 A 100 A 300 John 20111225 A 100 A 500 John 20111225 A 100 A 400 John 20111225 A 100 A 250 John 20111225 A 100 A 700 John 20111225 A 100 A 800 John 20111225 A 100 A 900 John 20111225 A 100 A 1000 John 20111225 A 100 A 1150 John 20111225 A 100 A 1275 John 20111225 A 100 A 3000 John 20111225
12 rows selected
I am wanting to update the table so that if there were more than 3 J_id's on the table for the same I_id then it will set all of the code1's and code2's to a C except for the very first one. Such as:
SQL> select * from example2;
CODE1 I_ID CODE2 J_ID NAME1 DATE1 ----- -------------------- ----- -------------------- -------------------------------- ---------------- A 100 A 200 John 20111225 C 100 C 300 John 20111225 C 100 C 500 John 20111225 C 100 C 400 John 20111225 C 100 C 250 John 20111225 C 100 C 700 John 20111225 C 100 C 800 John 20111225 C 100 C 900 John 20111225 C 100 C 1000 John 20111225 C 100 C 1150 John 20111225 C 100 C 1275 John 20111225 C 100 C 3000 John 20111225
12 rows selected
I have done some searches and haven't seen any results.
I have two tables, CREATE TABLE repos ( rep_key VARCHAR(10) NOT NULL, base_term VARCHAR(100) NOT NULL, blt_key INTEGER NOT NULL
[code]...
gloss table has the unique set of base_term as in repos. BLT_KEY will be primary_key in gloss and foreign key in repos.
Data in gloss table BLT_KEY BASE_TERM
1 base1 2 base2 3 base3
Now, I need to update the BLT_KEY in gloss to matching entries in repos. Can I do that in a update on select statement? like, UPDATE repos SET blt_key = (SELECT gloss.blt_key FROM repos, gloss WHERE repos.base_term = gloss.abase_term) This throws subquery returns more than one row.
And the end of update the repos table should look like, REP_KEY BASE_TERM BLT_KEY
Also I need a single query which can update on select as the no of records to be updated are more than 90000 in repos. So two step process would slow down the process
and I would like insert the same gross and net column values of ids 7 to 16 into columns with the ids 40 to 49 in the same order. therefore I would like to obtain the result that I describe below:
I am trying to write a control file that will read information from two INFILES and update two tables with the different information via SQL Loader. I am using Oracle 11g on Linux. i am not sure how to take the result from the first insert query and use it as input to the second insert query. Currently I have the following control file:
LOAD DATA INFILE 'table1.dat' INFILE 'table2.dat' APPEND INTO TABLE table1 WHEN tid='1' FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '"' (pid "pid.nextval", p_fname, p_lname, tid) [code]....
The pid in the second insert query should be taken from the pid generated in the first insert query. However, I do not know how to do this. Does SQL Loader run the first insert query for all entries in the table1.dat file and then the second insert query or will it do one record at a time? Here are my INFILES:
table1.dat ,John,Doe,1 ,Joe,Smith,1
table2.dat 10,,abc,1, 11,,xyz,1,
The second field in table2.dat should be taken from the result of creating a record in the table1.dat. Is this something that can be achieved using SQL Loader? The first part of the control file is successful, I can see the table being updated with the contents on table1.dat, but the second query fails.