create table with select union has no constraints - oracle

I created a table using select with a union, as follows:
create table tableC as
select column1, column2 from tableA
union all
select column1, column2 from tableB
The resulting table (tableC) has inherited none of the constraints from tableA or tableB. Why weren't the constraints copied to the new table?

Using select ... as ... to create a table never copies constraints. If you want the new table to inherit constraints from the original tables, you must create the new constraints manually.
As #Davek points out, not null constraints will get copied from a single table select ... as .... I imagine that's because they are both column attributes and constraints. However, once the column has more than one source, it is reasonable that Oracle would not try to apply that constraint.
In response to the follow-up question "would it be possible to give tableC the same constraints either from tableA or tableB, after a CTAs?":
Of course it's possible, but there's no single command to do it. You could write a procedure that used dynamic SQL to copy the constraints. However, unless you're looking to automate this behavior, it'll generally be easier to extract the DDL using an IDE and change the table name.

Related

How to make insert statement re-runnable?

Need to add two following insert statements:
insert into table1(schema, table_name, table_alias)
values ('ref_owner','test_table_1','tb1');
insert into table1(schema, table_name, table_alias)
values ('dba_owner','test_table_2','tb2');
Question is how can I make those two insert statements re-runnable meaning, if those two insert statement are compiled again, it should throw row exists error or something along those lines...?
Additional notes:
1. I've seen examples of Merge in Oracle however, thats only when you're using two tables to match records. In this case im only using a single table.
2. The table does not have any primary, unique or foreign keys - only check constraints on one of the columns.
Any help is highly appreciated.
You can use a MERGE statement, as follows:
MERGE into table1 t1
USING (SELECT 'ref_owner' AS SCHEMA_NAME, 'test_table_1' AS TABLE_NAME, 'tb1' AS ALIAS_NAME FROM DUAL
UNION ALL
SELECT 'dba_owner', 'test_table_2', 'tb2' FROM DUAL) d
ON (t1.SCHEMA = d.SCHEMA_NAME AND
t1.TABLE_NAME = d.TABLE_NAME)
WHEN NOT MATCHED THEN
INSERT (SCHEMA, TABLE_NAME, TABLE_ALIAS)
VALUES (d.SCHEMA_NAME, d.TABLE_NAME, d.ALIAS_NAME)
Best of luck.
You should have a primary key, especially when you want to check for duplicate records and data integrity.
Provide a primary key for your table, or, if you somehow do not want to do that, create a unique constraint for all of the columns in the table, so no duplicate rows are possible.

View the contents and constraints of a table

I am working on this assignment question and it is asking me:
To create a table called (TEMP_CUST) from an existing table Customers
View the content and constraints of TEMP_CUST table
What I have done so far is I have created my table, didn't add any constraints to the table TEMP_CUST and viewed the table using the DESC command.
Here is the code for table creation
CREATE TABLE TEMP_CUST
AS
(SELECT
CUSTOMER#, LASTNAME,
FIRSTNAME, ADDRESS, CITY,
STATE, ZIP, REFERRED,
REGION, EMAIL
FROM
CUSTOMERS);
DESC TEMP_CUST;
Now that I have done that I want to view the constraints of the table. I have used this command but am not sure if it is correct.
SELECT *
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'TEMP_CUST';
i have used this command, but not sure if it is correct.
You haven't said why you don't think it's correct so we have to guess the reason for your doubt. Perhaps it's because the set of constraints you get is smaller than the set of constraints for the original CUSTOMERS table?
That is correct. When we use CREATE TABLE ... AS SELECT the statement creates a new table with the projection, column names and datatypes of the original tables (assuming a vanilla SELECT clause) and the data (determined by the WHERE clause, if any). However, the only constraints which are created are NOT NULL constraints on the primary key column(s) and any other mandatory columns. The new table does not have primary key, foreign key or check constraints. We have to create these explicitly.
Hence, this query ...
SELECT * FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'TEMP_CUST';
... might return fewer constraints than you were expecting.

updating a table in VFP

There are two tables in my database. I am trying to update a column in table2 by setting it equal to one of the columns in table1. I've already looked at this answer visual foxpro - need to update table from another table
And tried to do this on my code, however, I kept having a syntax error on UPDATE table2. Why?
Here is what I have.
ALTER TABLE table2;
ADD COLUMN base2 B(8,2);
UPDATE table2
WHERE table2.itemid=table1.itemid from table1;
SET table2.base2=table1.base;
The simplest syntax is:
update table2 from table1 where table2.itemid = table1.itemid ;
set table2.base2 = table1.base
You could also add more fields to update separated by commas, i.e.
... set table2.base2 = table1.base, table2.this = table1.that
Using 'standard' VFP language syntax and RELATED Tables, you could quite easily do the following:
USE Table1 IN 0 EXCLUSIVE
SELECT Table1
INDEX ON ID TAG ID && Create Index on ID field
USE Table2 IN 0
SELECT Table2
SET RELATION TO ID INTO Table1
REPLACE ALL Table2.ID WITH Table1.ID FOR !EMPTY(Table2.ID)
You might want to spend some time looking over the free, on-line tutorial videos at: Learn Visual Foxpro # garfieldhudson.com
The videos named:
* Building a Simple Application - Pt. 5
and
* Q&A: Using Related Tables In A Report
Both discuss using VFP's language to work with Related Tables
Good Luck
Use join
Update table2 b
Join table1 a on b. Itemid=a.itemid
Set b. Base2=a.base

sybase insert from another database in same server

i am trying to get all extra data from one data base and trying to insert into another.
But i want to omit the column name and am trying to make only the table name as hard coded to achieve this. But we have some fields which are system generated in a table like an id which is not that necessary a data but still will create a integrity issue. How can i do a insert of just the wanna be details omitting those above columns, the names of the columns to omit also changes.. I can't do a total insert, just the addition of some extra data.
so far i have come to this.
while 1=1
begin
if exists(select 1 from db1.table1 not in (select * from db2.table1)
begin
insert into db2.table1 (columns) select (columns) from db1.table1
end
if(rowCount=0)
break
end
please advise how i can optimize this to get the least possible hard coding
Have left the pk part intentionally, as
the query being big.
If you want to something like:
insert into TAB
select * from TAB2
or
insert into TAB
select col1,col2 from TAB2
or
insert into TAB (col1,col2)
select * from TAB2
where TAB1 and TAB2 have different count or type of columns it's not possible, because it will generate an error.

Updating a SQL table where items to change are identified in another table that is linked

Everywhere I look I can find how to update a table from data in another table but I am not looking for that. I have two tables TABLE1 and TABLE2. TABLE1 has a column PULLDATE and a column JOBNMBR. TABLE2 has a column JOBNMBR and a column PROJECT. The two tables link at the JOBNMBR column. I need to do a bulk update to TABLE1.PULLDATE per a project number, but that project number is stored in TABLE2.PROJECT.
Using VisualStudio 2005 and in VB code not C+, does anyone know the code (if there is any) that links the tables and allows me to update all TABLE1.PULLDATE records grouped by TABLE2.PROJECT? I will be providing the trigger to update using a textbox [TxtBox_Pulldate] and a nearby button [Button_UpdatePulldate].
Thanks a bunch
Chuck Vensel
I think I understand that you want to update Table1 given a matching column in Table2?
You write the SQL update just as you would the SELECT except replace the SELECT clause with the UPDATE clause.
UPDATE Table1
SET
[PULLDATE] = your_value
FROM
Table1
JOIN Table2
ON Table2.[JOBNMBR] = Table1.[JOBNMBR]
WHERE
Table2.[PROJECT] = your_project_ID

Resources