trying to change a data type in table using a query - sql

I have this query
CREATE TABLE COSTUMER(
COSTUMER_ID INT,
TAXI_ID INT,
COSTUMER_PHONE_NUMBER BIGINT,
COSTUMER_NAME VARCHAR(40),
DESTINATION VARCHAR(40)
);
i'd like to change the DESTINATION data type to DATETIME rather than VARCHAR. or if you can suggest a better data type that can store a full address the please do.
I tried this query
ALTER TABLE COSTUMER ALTER DESTINATION DATETIME
but when executed I get this message :
102 stating expecting column

If it is a mySql database then the syntax should be as follows:
ALTER TABLE table_name
MODIFY COLUMN column_name datatype

ALTER TABLE COSTUMER MODIFY COLUMN DESTINATION DATETIME;

Related

Presto Hive SQL Error: mismatched input 'PARTITIONED'. Expecting: 'COMMENT', 'WITH', <EOF>

I am trying to create a Hive table with partitions but getting the above error. What am I doing wrong?
CREATE TABLE IF NOT EXISTS schema.table_name
(
ID varchar(20),
name varchar(50)
)
PARTITIONED BY (part_dt varchar(8), system varchar(5));
The code works without the partitioning clause. Something gives up during partitioning.
Statement is working in hive. Pls find below screenshot.
Its possible that some of column names are reserved keywords and that is throwing error. if yes, you can use below SQL too.
CREATE TABLE IF NOT EXISTS schema.table_name
(
`ID` varchar(20),
`name` varchar(50)
)
PARTITIONED BY (`part_dt` varchar(8), `system` varchar(5));

Unable to change column size

In my database table, I've column entryId with data type varchar and size=10. Now I want to change size to 100, when I hit following query -
ALTER TABLE ft ALTER COLUMN entryId TYPE varchar(100);
it results into following error -
Incorrect syntax near 'varchar'.
My query syntax looks correct. Help me to understand what I did wrong.
No "Type" is need here:
ALTER TABLE ft ALTER COLUMN entryId varchar(100);
I SQL Server, the keyword type is not needed
ALTER TABLE ft ALTER COLUMN entryId varchar(100);
Remove 'Type' from your query :
ALTER TABLE ft ALTER COLUMN entryId VARCHAR(100);
no need for 'TYPE' keyword
ALTER TABLE ft ALTER COLUMN entryId varchar(100);

Guessing Data Type of columns stored as VarChar in SQLServer

I'm just wondering if anyone has created or heard of a function to guess the appropriate data type of data stored in a SQLServer table as VarChar.
I'm working on an SSIS package that you can point at a directory, and it will loop through and create tables / import data for every CSV that exists it. I'm having trouble with specifying the data types before import, so as a work around I would like to import all the data as VarChar(50) into a temporary table and then run some sort of function to analyze each column for the appropriate data type (int, decimal, float, etc) so I can use that to script the create table and insert statements.
So for example I'd like to be able to point a function or query at this temp table
CREATE TABLE [#Data]
(
[ProductCode] varchar(50),
[ProductName] varchar(50),
[Year] varchar(50),
[Total_volume] varchar(50),
[Total_Quantity] varchar(50),
[PercentSold] varchar(50)
)
to read through the data and determine what data type / length is most appropriate - much like the 'Suggest Data Type' tool in Excel Connection Manager does, only something I can tie into a variable to be done dynamically. It should end up looking something like
CREATE TABLE [Data]
(
[ProductCode] varchar(6),
[ProductName] varchar(11),
[Year] int,
[Total_volume] int,
[Total_Quantity] int,
[PercentSold] decimal(3,2)
)
Any thoughts?
Thanks!

why can't I insert data into tables I created in sql server managment studio 2012? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I created the tables with this:
CREATE TABLE COSTUMER(
COSTUMER_ID INT,
TAXI_ID INT,
COSTUMER_PHONE_NUMBER INT,
COSTUMER_NAME VARCHAR(40)
DESTINATION VARCHAR(40)
);
CREATE TABLE TAXI(
TAXI_ID INT,
COSTUMER_ID INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_PHONE_NUMBER INT,
DRIVER_NAME INT,
DESTINATION
);
CREATE TABLE BOOKING(
DESTINATION VARCHAR(40),
TAXI_ID INT,
COSTUMER_ID,
COSTUMER_PHONE_NUMBER INT,
DRIVER_PHONE_NUMBER INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40)
COSTUMER_NAME VARCHAR(40)
TIME_OF_ARRIVAL DATETIME,
);
After this my table appeared with all the attributes. Then I set my PKs and FKs manually.
now am trying to insert some data to each of the tables but I keep getting error 213:
Column name or number of supplied values does not match table definition
I searched and searched and still don't understand.
also when I insert the code for such as SELECT * FROM Costumer (that's the table name) it won't show in the small suggestion box as if it doesn't exist???
The below code should work for your requirement unless you have specified an identity column manually as you said that you made some changes manually.
In case you specified an identity column manually then you should not insert the value for the Identity column (say Customer_ID) or you should set SET IDENTITY_INSERT COSTUMER ON before inserting the ID column
CREATE TABLE COSTUMER(
COSTUMER_ID INT,
TAXI_ID INT,
COSTUMER_PHONE_NUMBER INT,
COSTUMER_NAME VARCHAR(40),
DESTINATION VARCHAR(40)
);
SELECT * FROM COSTUMER
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID,COSTUMER_PHONE_NUMBER,COSTUMER_NAME,DESTINATION) VALUES (1, 33,123211241, 'John','Mexico' )
I think you are trying to insert more/less input to table
INSERT INTO [TableName] (Column1, Column2, Column3)
VALUES (Value1, Value2, Value3)
Number of columns should be equal to Number of Values. Please check your INSERT statement.
If you not included (Column1, Column2, Column3) in your statement, include it.
Post the insert statement you used. I see a few problems with the current code you posted.
You don't provide datatypes for a few of your attributes, for instance in the Booking table you need to provide some type for the CUSTOMER_ID attribute. Also you need the attributes and datatypes to be comma separated, except the last one doesn't need a trailing comma. For instance you have:
...
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40)
COSTUMER_NAME VARCHAR(40)
TIME_OF_ARRIVAL DATETIME,
...
This will throw an error probably saying incorrect syntax near COSTUMER_NAME.
You need something like:
CREATE TABLE BOOKING(
DESTINATION VARCHAR(40),
TAXI_ID INT,
COSTUMER_ID INT,
COSTUMER_PHONE_NUMBER INT,
DRIVER_PHONE_NUMBER INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40),
COSTUMER_NAME VARCHAR(40),
TIME_OF_ARRIVAL DATETIME
);
For an insert try something along the lines of:
INSERT INTO <table_name> (<column1>,<column2>,<column3>)
VALUES (<col1Value>,<col2Value>,<col3Value>)
If you aren't getting auto completion you might try and refresh the DB in the management studio by right click > refresh, but I wouldn't rely on auto completion at all in SQL Server nor IntelliSense unless you use someting like this.
If you post the insertion script I might be able to help more.
Update: So, the problem you got was with arithmetic overflow, specifically trying to put 07854625781 into a int. All datatypes have maximum/minimums or other "constraints", an int can be in the range of -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647) (4 bytes). The value you enter exceeds that, it is being interpreted as 07,854,625,781 (7.8 billion) while the maximum for an int is only 2.147 billion. If you change the datatype of phone number to bigint that can hold -2^63 (-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807) (8 bytes) it will work, However I would advise you not to store a phone number in that way, rather, use char(length) if you know that the size and format will always be the same, if you think the length could vary (maybe from extensions or international numbers) I would use something along the lines of varchar(40).
Through this datatype change you will just have to surround input in 'single quotes' when you insert.
Cheers
The Problem that you are facing in the below insert query is the value 07854625781 has become too large to handle for int type. Int range 2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647). So change the datatype of COSTUMER_PHONE_NUMBER to Bigint.
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID, COSTUMER_PHONE_NUMBER, COSTUMER_NAME, DESTINATION) values (1, 32, 07854625781, 'Denzel Washington', 'Heathrow Airport')
use the below query to change the datatype to bigint and then try insert(above command)
ALTER TABLE COSTUMER ALTER COSTUMER_PHONE_NUMBER bigint;
Steps to Follow
ALTER TABLE COSTUMER ALTER COSTUMER_PHONE_NUMBER bigint;
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID, COSTUMER_PHONE_NUMBER, COSTUMER_NAME, DESTINATION) values (1, 32, 07854625781, 'Denzel Washington', 'Heathrow Airport');
Now do
Select * from COSTUMER
you will find the record inserted

Hive - Renaming Fields With Same Name As A Datatype

I have a Hive table like such:
CREATE TABLE mytest (name int, timestamp bigint, donation int);
I am using Hive 0.12. Note the field "timestamp". Incidentally, Hive 0.12+ introduced a new data type called timestamp. Say I want to rename this field to time_stamp
I tried these:
ALTER TABLE mytest CHANGE timestamp time_stamp BIGINT;
ALTER TABLE mytest CHANGE COLUMN timestamp time_stamp BIGINT;
ALTER TABLE mytest CHANGE [timestamp] time_stamp BIGINT;
ALTER TABLE mytest CHANGE `timestamp` time_stamp BIGINT;
However, all of them give me the following error:
FAILED: ParseException line 1:38 mismatched input 'CHANGE' expecting KW_EXCHANGE near 'mytest' in alter exchange partition
I am dead sure this is because of the fact that my field name is the same as a data type name. How can I alter the schema for mytest without having to do the following?
CREATE mytest_cpy AS SELECT mytest.name, mytest.timestamp AS time_stamp,
mytest.donation FROM mytest;
DROP TABLE mytest;
ALTER TABLE mytest_cpy RENAME TO mytest;
Thanks! Any/all help is appreciated!
Use backticks for any unusal column names, either containing weird symbols or the same as data types. This works for Hive 0.14:
ALTER TABLE mytest CHANGE COLUMN `timestamp` time_stamp BIGINT;
in MS SQL use:
EXECUTE sp_rename 'dbo.mytest.timestamp', 'time_stamp', 'COLUMN'
in MYSQL try:
USE database_name;
ALTER TABLE mytest CHANGE timestamp time_stamp BIGINT;
or:
ALTER TABLE database_name.mytest CHANGE timestamp time_stamp BIGINT;
Although this post have answers but still:
If you try to give column name which is Reserved Keywords for HIVE then error will occur.
For overriding the reserved keywords use can use backtick(`) this is the character below your tilde key or left to 1 key.