create-table

DB2: How to get generated always as statement to work with session user

好久不见. 提交于 2019-12-25 04:20:52
问题 I need to get a userstamp into a table and have not managed to figure out how the GENERATED FOR EACH ROW ON UPDATE AS statement works with the SESSION_USER variable in DB2 10.5 (LUW). Managed to get an implementation working using a function which has a fake variable for forcing the evaluation in update statements: CREATE OR REPLACE FUNCTION XXX.CURRENT_USER( tmp varchar(128)) SPECIFIC xxx.XXX_CURRENT_USER RETURNS VARCHAR(128) CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION BEGIN RETURN session

DB2: How to get generated always as statement to work with session user

天涯浪子 提交于 2019-12-25 04:20:06
问题 I need to get a userstamp into a table and have not managed to figure out how the GENERATED FOR EACH ROW ON UPDATE AS statement works with the SESSION_USER variable in DB2 10.5 (LUW). Managed to get an implementation working using a function which has a fake variable for forcing the evaluation in update statements: CREATE OR REPLACE FUNCTION XXX.CURRENT_USER( tmp varchar(128)) SPECIFIC xxx.XXX_CURRENT_USER RETURNS VARCHAR(128) CONTAINS SQL DETERMINISTIC NO EXTERNAL ACTION BEGIN RETURN session

Change empty string to NULL when column has DATE constraint

回眸只為那壹抹淺笑 提交于 2019-12-25 00:40:05
问题 This might be impossible but I was wondering if someone more experienced knew if this is possible to do in postgresql. I have a column in my create statement CREATE table IF NOT EXISTS (other cols, some_date DATE, other cols); When I extract the json object from the API, there might actually be an empty string '' instead of a empty cell. Which of course gives me the following error psycopg2.errors.InvalidDatetimeFormat: invalid input syntax for type date: "" The solution would simply to

How to create/add columns using a variable in a loop

人走茶凉 提交于 2019-12-24 16:32:52
问题 I am very new to SQL in that I just finished reading Sams Teach Yourself SQL in 10 Minutes and that is my only SQL knowledge. So now that I'm done with the book, I'm trying to create some tables so I can play around with them. I can easily create a table with a known amount of columns and specified header. Where I am having trouble is creating a table with an unknown amount of columns and a date as the header. What I have tried so far is this: DECLARE @start_date AS DATE DECLARE @end_date AS

Confusion with the external tables in hive

与世无争的帅哥 提交于 2019-12-24 14:08:03
问题 I have created the hive external table using below command: use hive2; create external table depTable (depId int comment 'This is the unique id for each dep', depName string,location string) comment 'department table' row format delimited fields terminated by "," stored as textfile location '/dataDir/'; Now, when I view the HDFS I can see the db but there is no depTable inside the warehouse. [cloudera@quickstart ~]$ hadoop fs -ls /user/hive/warehouse/hive2.db [cloudera@quickstart ~]$ Above

create table with select union has no constraints

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 03:15:15
问题 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? 回答1: 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

CREATE TABLE new_table_name LIKE old_table_name with old_table_name's AUTO_INCREMENT values

試著忘記壹切 提交于 2019-12-22 17:42:31
问题 Can I create a new table with an old table's autoincriment status in mysql client? I think, that ALTER TABLE new_table_name AUTO_INCREMENT=@my_autoincr_iment helps me, but this construction must use with a constant value. I don't want to use a difficult script. 回答1: mysql> create table new_table like old_table; mysql> select @my_auto_increment:=auto_increment from information_schema.tables where table_name='old_table'; mysql> set @query = CONCAT("alter table new_table auto_increment = ", @my

Mysql errno 150 trying to create table with foreign key references

狂风中的少年 提交于 2019-12-22 10:48:41
问题 I'm trying to create a table in mysql with a foreign key reference, like this: In database A: CREATE TABLE replication ( id varchar(255) NOT NULL PRIMARY KEY, uid varchar(255) NOT NULL, value int(11) NOT NULL, FOREIGN KEY (uid) REFERENCES databaseB.users(username) ); In database B i have a table named users like this: +-----------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------------+--------------+------+-----+---------+------

Storing the results of a prepared statement as a table in mysql?

江枫思渺然 提交于 2019-12-22 08:25:15
问题 Is it possible to store the result of a prepared table in mysql ? My use case is - : I am creating two variables based on certain conditions of the source table, then fetching the randomized rows, based on this criteria. Since I have 10 of such tables, should I be 1st joining them and then doing this randomization on the "overall" passing/filtering criteria (See also @total below, which is my main criteria, PER table) set @total=(select count(*) from tab_1 where predict_var ="4" or predict

What is the difference between UNIQUE, UNIQUE KEY and CONSTRAINT 'name' UNIQUE?

那年仲夏 提交于 2019-12-22 02:06:54
问题 I have a basic users table I want to create in MySQL. I do not want duplicate emails or duplicate usernames appearing in the database. What is the best way of preventing this upon table creation? And what is the difference between the following: 1. UNIQUE (username), UNIQUE (email), 2. UNIQUE KEY (username), UNIQUE KEY (email), 3. CONSTRAINT ucons_login UNIQUE (username, email), I assume some of these are synonymous, yet I've been reading conflicting information online and was seeking