can anybody help me with this please?
I want to insert data in table in Teradata SQL Assistant, but insert only that doesn't exist in the table.
I have an ideal how to do it in Microsoft SQL Server but not in teradata as i'm new to it.
Any help would be highly appreciated.
Thank you
This very much depends on your situation. Can you elaborate a little more by answering the following questions? I can then give you my best idea of what to do.
Are you inserting into an existing table using a select statement from other Teradata tables, or from a file?
Does the table you are inserting into have a UPI (unique primary index)? You can check this by running the following code:
show table database.table;
where "table" is the name of your table and "database" is the database where your table is stored.
The end of the resultset should say either "unique primary index" or simply "primary index."
No matter what the answers are, you will most likely need to use an insert statement, insert-select statement, or a merge statement in the end. Teradata SQL Assistant has an example of all of these in the Query Builder. You can bring it up by pressing F2.
If I understood your question, You want to insert some data to a table. A the issue is you only need to insert those rows in the table for-example table A, which does not exist is this table A. The solution depends on your source.
If source is another Teradata table that is table B, then you could use a join between Table A and Table B so detect which are not already there.
If source is some other database or a file, than I would suggest to insert data to a temporary table and then use the same logic as given in point-1.
Br,
Hassam
Related
I'm trying to work with YEAR function on one column in the DB and then add the results to a different table in the DWH.
What am I doing wrong?
INSERT INTO example_dwh1.dim_time (date_year)
SELECT YEAR(time_taken)
FROM exampledb.photos;
When removing the INSERT INTO line, I get the results I want, but I'm not able to insert them into the dwh table.
Thanks for your help!
The following select works, but I don't see the data in the table after the insert:
INSERT INTO example_dwh1.dim_time (date_year)
SELECT YEAR(time_taken)
FROM exampledb.photos;
There is rather broad. Assuming you have no errors in the insert, you might have:
You are incorrectly querying dim_time, so the data is there but your check is wrong.
You are inserting into dim_time in one database but querying it in another.
Assuming you have errors but are missing them, here are some possibilities:
The database does not exist.
The table does not exist.
The column is misnamed.
Other columns are declared NOT NULL.
Triggers defined on the table are preventing the insert.
Unique constraints/indexes on the table are preventing the insert.
Your question does not provide enough information to be more specific. However, it seems highly suspicious to be inserting a bunch of years -- which might include many duplicates -- into a dimension table.
I have two MySQL database k_db1 and k_db2 on a single server.
In k_db1, I have k_db1.table1 and k_db1.table2.
In k_db2, I have k_db2.table3 and k_db2.table4.
I want to create a third database k_db3 where I copy/paste tables of others databases.
It will result in k_db3.db1-table1, k_db3.db1-table2, k_db3.db2-table3, k_db3.db2-table4. I want to transfer data, indexes etc... and I don't want to delete k_db1 and k_db2 tables in the process. It must duplicate datas.
Do you know a way to do this just with SQL command?
Thanks in advance for your help.
You can try something like this:
DROP TABLE IF EXISTS k_db3.db1_table_1;
CREATE TABLE k_db3.db1_table_1 AS
SELECT * FROM db1.table_1;
Then you can recreate the indexes on the new table via ALTER TABLE statements.
Also I would avoid using - in table names.
For example, I have a table named movies. It has the fields/columns title VARCHAR(100) and runtime INT(5). It's loaded with 10,000 rows of data.
I want to create another table, let's call it movies_custom, that has all of the same columns, but with none of the data.
Is there a quick SQL statement to do this?
EDIT: Sorry, I noticed the SQL Server tag on this and assumed that was the technology you were using until I saw MySQL in the question title.
You can use the syntax CREATE movies_custom LIKE movies in MySQL
Sure!
In SQL Server you can do this query:
select top 0 * into movies_custom from movies
That creates the table structure without inserting any rows.
Very easy in MySQL:
CREATE newtable LIKE oldtable;
The other answers led me in the right direction, but I had to add the keyword TABLE in order to make it work with MySQL Workbench 6.
CREATE TABLE movies_custom LIKE movies;
Is there a way to do this?
In case the DBMS command history got cleaned or, in my case, when many ALTER TABLE were used in the course of time.
I'm using MySQL.
Yes, it is as simple as
SHOW CREATE TABLE yourtable;
This will include all the subsequent ALTER TABLE statements. You cannot retrieve the table's original state.
Here is the relevant documentation
Is there a way to get the insert statements for a table via some query in MySql?
for ex: if the table name is Cards,which has 5 rows, i need to get the insert statements for that particular table.
Thanks.
You can do a "mysqldump" which will backup the database to a file. That file will include all off the insert statements with data.
Enjoy!
Doug's right. You can use mysqldump. But you can refine it a bit and only write out the scripts for the tables that you want. This explains how.