Create a enum table and access it from other table in mysql - mysql

I have a table schema as follows:
[enumeration]
Status{
good, bad, high,low
}
Image{
id :string
name: string
quality : Status
}
I have found that enum Table can be created as:
Create Table Status{
status enum ('good','bad','high','low')
};
My question: Do i need to create a column for a table Status?
How can i refer it in other table while creating a table schema?

You don't need a table for status. You declare a status enum column in your image table. Like so:
create table testo (status enum('foo', 'bar', 'baz'));
insert into testo(status) values('foo');

Related

Converting an INT column to an ENUM column in MySQL

I have following INT column in my MySQL table with two records:
I have tried to change the datatype to ENUM with following:
`BetalingsStatus` ENUM ('BestillingRegistreret','FakturaSendt','Betalt') NOT NULL,
But my existing data goes missing. How do I convert my existing INT values to ENUM ?.
create the new column:
ALTER TABLE XXX ADD `BetalingsStatus_Tmp` ENUM('BestillingRegistreret','FakturaSendt','Betalt') NOT NULL DEFAULT '' AFTER BetalingsStatus;
Update the data:
UPDATE XXXX SET BetalingsStatus_Tmp = CASE BetalingsStatus
WHEN 0 THEN 'BestillingRegistreret'
WHEN 1 THEN 'FakturaSendt'
WHEN 2 THEN 'Betalt'
END
Delete old column:
ALTER TABLE XXX DROP BetalingsStatus;
Rename tmp column:
ALTER TABLE XXX CHANGE BetalingsStatus_Tmp BetalingsStatus ENUM('BestillingRegistreret','FakturaSendt','Betalt') NOT NULL;

Rebuild a Hive table with different definitions

I want to build an automatic system to help me map Hive tables.
I have an SQL table with meta data: tableID, fieldName, field Type, description, lastUpdated.
I want to update automatically my tables -
where lastUpdate=CURDATE() - INTERVAL '1' DAY
But I don't have an indication on what change was made - it can be a new column in the table, and it can be a column name that was changed, or even a description update.
Is there a way to "define" a table all over again when it already exists? That all the changes I want to make will be executed at once (all change types)?
for instance - I have a table that was defined like this:
create external table IF NOT EXISTS tableA (`a` string, `b` int, `c` int) PARTITIONED BY (dt date) ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t' STORED AS TEXTFILE LOCATION 'File/Path';
And the change was that column "b" type is now "string". Is there a (generic) update/alter query that I can write:
*SomeCommand* tableA (`a` string, `b` string, `c` int)
and my column will be updated?
Same if I have a new column - d, type: float.
*SomeCommand* tableA (`a` string, `b` string, `c` int, `d` float)
I need one command that can contain these options, please. Or - if you have another good idea n how to do this, I will really appreciate it...
Thank you!
You can use ALTER TABLE REPLACE COLUMNS. It does exactly what you asked,
It will replace all the columns at once. See https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-Add/ReplaceColumns

Copy table data on same server with field remapping

I need to copy the data of an old table with millions of rows to a newer table, with a slightly different definition. Most importantly, there is one new field with a null-default, and a varchar field became an enum (with directly mapping values).
Old table:
id : integer
type : varchar
New table:
id : integer
type : enum
number : integer, default null
All of the possible string values of type are within the new enumeration.
I tried the following:
insert into new.table select * from old.table
But I obviously get:
Insert value list does not match column list: 1136 Column count doesn't match value count at row 1
You can copy the table data and structure from phpmyadmin window, and then modify the new table and add the new column.
Using the INSERT ... SELECT syntax:
INSERT INTO new.table `id`, `type` SELECT `id`, `type` FROM old.table
Apparently the varchar to enum remapping isn't a problem.

Insert DataFrame into SQL table with AUTO_INCREMENT column

I have a MySQL table which includes a column that is AUTO_INCREMENT:
CREATE TABLE features (
id INT NOT NULL AUTO_INCREMENT,
name CHAR(30),
value DOUBLE PRECISION
);
I created a DataFrame and wanted to insert it into this table.
case class Feature(name: String, value: Double)
val rdd: RDD[Feature]
val df = rdd.toDF()
df.write.mode(SaveMode.Append).jdbc("jdbc:mysql://...", "features", new Properties)
I get the error, Column count doesn’t match value count at row 1. If I delete the id column it works. How could I insert this data into the table without changing the schema?
You have to include an id field in the DataFrame, but its value will be ignored and replaced with the auto-incremented ID. That is:
case class Feature(id: Int, name: String, value: Double)
Then just set id to 0, or any number when you create a Feature.

Dynamic Partitioning + CREATE AS on HIVE

I'm trying to create a new table from another table with CREATE AS and dynamic Partitioning on HiveCLI. I'm learning from Hive official wiki where there is this example:
CREATE TABLE T (key int, value string)
PARTITIONED BY (ds string, hr int) AS
SELECT key, value, ds, hr+1 hr1
FROM srcpart
WHERE ds is not null
And hr>10;
But I received this error:
FAILED: SemanticException [Error 10065]:
CREATE TABLE AS SELECT command cannot specify the list of columns for the target table
Source: https://cwiki.apache.org/confluence/display/Hive/DynamicPartitions#DynamicPartitions-Syntax
Since you already know the full schema of the target table, try creating it first and the populating it with a LOAD DATA command:
SET hive.exec.dynamic.partition.mode=nonstrict;
CREATE TABLE T (key int, value string)
PARTITIONED BY (ds string, hr int);
INSERT OVERWRITE TABLE T PARTITION(ds, hr)
SELECT key, value, ds, hr+1 AS hr
FROM srcpart
WHERE ds is not null
And hr>10;
Note: the set command is needed since you are performing a full dynamic partition insert.
SET hive.exec.dynamic.partition.mode=nonstrict;
CREATE TABLE T (key int, value string)
PARTITIONED BY (ds string, hr int);
INSERT OVERWRITE TABLE T PARTITION(ds, hr)
SELECT key, value, ds, hr+1 AS hr
FROM srcpart
WHERE ds is not null
And hr>10;
In the above code, instead of the Create statement use: CREATE TABLE T like srcpart;
In case the partitioning is similar.