I am new to Hive . I want to create the table in hive with the same columns as that of existing table plus some additional columns. I Know we can use something like this.
CREATE TABLE new_table_name
AS
SELECT *
FROM old_table_name
This will create the table with same columns as that of old_table_name.
But How do I specify additional columns in new_table_name ?
Here is how you can achieve it:
Old table:
hive> describe departments;
OK
department_id int from deserializer
department_name string from deserializer
Create table:
create table ctas as
select department_id, department_name,
cast(null as int) as col_null
from departments;
Displaying Structure of new table:
hive> describe ctas;
OK
department_id int
department_name string
col_null int
Time taken: 0.106 seconds, Fetched: 3 row(s)
Results from new table:
hive> select * from ctas;
OK
2 Fitness NULL
3 Footwear NULL
4 Apparel NULL
5 Golf NULL
6 Outdoors NULL
7 Fan Shop NULL
8 TESTING NULL
8000 TESTING NULL
9000 testing export NULL
Simple way is to issue ALTER TABLE command to add more(additional) columns after the above CREATE statement.
first create a new table like the first one
and after that alter this new table and add the columns that you want.
CREATE TABLE new_table LIKE old_table;
ALTER TABLE new_table ADD COLUMNS (newCol1 int,newCol2 int);
if you wish to avoid data copy, make your table external
I wish that it helps you :)
Related
So I'm trying to create a copy of one of my tables into an index organized table, However I get an error, here is the code.
create table clients2 as
select *
from clients
organization index;
ORA-00933:"SQL command not properly ended"
Your command is wrong.
SQL> create table testiot ( object_id primary key,object_name )
organization index
as select object_id,object_name from dba_objects
where object_id is not null ;
Table created.
SQL> select count(*) from testiot ;
COUNT(*)
----------
208730
Organization index must be in the definition of the table and before as select. On the other hand, you need to define the columns in the table and which one is the primary key for IOT.
I have insert 10,000 rows into table which contains 3 columns.
Now i need to add the new column into table.And also need to store the value to updated column which is to be same for all 10,000 rows.
For example:
my table like below..,
No Name ID
1 raj 1000
2 ravi 1001
3 git 1002
..
.
.
10000 dat 10,000
Now i need to add the new column "date"
Then data like this..,
No Name ID Date
1 raj 1001
2 ravi 1002
3 git 1003
I have using below query to add new column
ALTER TABLE table_name
ADD Date date
but i need to know how to store the same data into all rows in table like below.
No Name ID Date
1 raj 1001 10.12.2020
2 ravi 1002 10.12.2020
3 git 1003 10.12.2020
.
.
.
.
10,000 dat 10000 10.12.2020
How can i achieve above requirement?
I am little bit know about sql server.
Can anyone please help me to solve this?
ALTER TABLE table_name
ADD CONSTRAINT DF_date DEFAULT N'10.10.2020' FOR [date];
or
ALTER TABLE [dbo].table_name
ADD CONSTRAINT DF_table_name_column_name DEFAULT ('10.10.2020') FOR column_name
iam giving one example
CREATE TABLE #TEST(PART VARCHAR(10),LASTTIME DATETIME)
GO
ALTER TABLE [DBO].#TEST
ADD CONSTRAINT DF_#TEST_LASTTIME DEFAULT ('10.10.2020') FOR LASTTIME
INSERT INTO #TEST (PART )
VALUES('A')
INSERT INTO #TEST (PART )
VALUES('B')
INSERT INTO #TEST (PART )
VALUES('AA')
INSERT INTO #TEST (PART )
VALUES('BA')
GO
First only add new column to your table .
ALTER TABLE Protocols
ADD Date Date
After , For your past data ,you can use update query .
update Protocols set Date='10.12.2020' .
In last ,
ALTER TABLE Protocols
ALTER COLUMN Date SET DEFAULT '10.12.2020'
It will be updating all your pass date with '10.12.2020' and in future also value for Date will be '10.12.2020' .
Thanks .
I want to create a table using CTAS of partitioned table.
New table must have all the data and partitions, subpartitions of old table.
How to do this?
You need to first create the new table with all the partitions, there is no way you can add partition definitions to a CTAS. Once the table is created you can populate it using insert into .. select.
You can use dbms_metadata.get_ddl to get the definition of the old table.
select dbms_metadata.get_ddl('TABLE', 'NAME_OF_EXISTING_TABLE')
from dual;
Save the output of that into a script, do a search and replace to adjust the table name, then run the create table and then run the insert into ... select ...
i want to create a table similar to other table and also want to declare few more new columns at same time.
I tried like
create table emp2 as ((select * from emp)),age varchar2(3))
But its not working.
If you want to add additional columns in your CTAS (Create Table As Select), you'll have to provide data for them. If you want to leave the column empty, you can use CAST(null as <your datatype>):
create table dual_copy as
select
d.*,
cast(null as varchar2(3)) as age
from dual d;
A word of advice, though: Explicitly storing the age of people is never a good idea (people tend to get older over time :-) ). Just store the birth date and compute the age on-the-fly. Alternatively (if you're running 11g or later), you could add a virtual column for the age.
And if you insist on explicitly storing the age, you should at least use a numeric datatype instead of a varchar2.
You could add the age column in another statement:
CREATE TABLE emp2 AS SELECT * FROM emp;
ALTER TABLE emp2 ADD age VARCHAR2(32);
I want to create table so that when the query runs, it asks for the table name. I am sure this is a duplicate post but would like to know if this is possible with Oracle?
If you are using SQL*Plus, you can use substitution variables (&variable_name)
SQL> create table &name (
2 col1 number,
3 col2 date
4 );
Enter value for name: fuzzy_bunny
old 1: create table &name (
new 1: create table fuzzy_bunny (
Table created.
SQL> select * from fuzzy_bunny;
no rows selected