Create a table and insert data in one statement MySQL - mysql

I'm trying to create a table and insert data into it in one statement but I can't figure out just how to do it.
The statement has to create a table that has 3 columns: stock symbol, price, and quote date.
The symbol data is supposed to be SELECT'ed from another table, my_stocks. The price and quote date need to be inserted manually.
I tried the following statement, but I keep getting a syntax error:
CREATE TABLE stock_prices (
symbol VARCHAR(20) NOT NULL,
quote_date DATE NOT NULL,
price DECIMAL(5,3) NOT NULL,
) SELECT symbol FROM my_stocks;
I'm running MySQL Community Server 5.5.28, btw.
Edit: I got it, the appropriate statement is:
CREATE TABLE stock_prices (
quote_date DATE NOT NULL,
price DECIMAL(5,5) NOT NULL
) SELECT `symbol`, SYSDATE() AS quote_date, 0 AS price FROM my_stocks;

CREATE TABLE stock_prices (
symbol VARCHAR(20) NOT NULL,
quote_date DATE NOT NULL,
price DECIMAL(5,5) NOT NULL,
) SELECT symbol, now(), 0 FROM my_stocks;

Related

How to convert UTC to EST or EDT in my sql

I have created a table, which is given below,
CREATE TABLE `user` (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
department VARCHAR(100) NOT NULL,
submission_date DATE,
PRIMARY KEY ( id )
);
I have inserted data from user details table to user table. user details table contains utc date format in column submission_date.
Insertion query is given below,
INSERT INTO user
(name,department,submission_date)
SELECT name,department,submission_date FROM user_details;
How to convert utc to est/edt in my sql
Use CONVERT_TZ to achieve this:
Note that -05:00 is for EST. You can modify this as per your need.
INSERT INTO user
(name,department,submission_date)
SELECT name,department,convert_tz(submission_date , '+00:00', '-05:00')
FROM user_details;
db<>fiddle

How to design Cassandra Scheme for User Actions Log?

I have a table like this in MYSQL to log user actions :
CREATE TABLE `actions` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`module` VARCHAR(32) NOT NULL,
`controller` VARCHAR(64) NOT NULL,
`action` VARCHAR(64) NOT NULL,
`date` Timestamp NOT NULL,
`userid` BIGINT(20) NOT NULL,
`ip` VARCHAR(32) NOT NULL,
`duration` DOUBLE NOT NULL,
PRIMARY KEY (`id`),
)
COLLATE='utf8mb4_general_ci'
ENGINE=MyISAM
AUTO_INCREMENT=1
I have a MYSQL Query Like this to find out count of specific actions per day :
SELECT COUNT(*) FROM actions WHERE actions.action = "join" AND
YEAR(date)=2017 AND MONTH(date)=06 GROUP BY YEAR(date), MONTH(date),
DAY(date)
this takes 50 - 60 second to me to have a list of days with count of "join" action with only 5 million rows and index in date and action.
So, I want to log actions using Cassandra, so How can I design Cassandra scheme and How to query to get such request less than 1 second.
CREATE TABLE actions (
id timeuuid,
module varchar,
controller varchar,
action varchar,
date_time timestamp,
userid bigint,
ip varchar,
duration double,
year int,
month int,
dt date,
PRIMARY KEY ((action,year,month),dt,id)
);
Explanation:
With abobe table Defination
SELECT COUNT(*) FROM actions WHERE actions.action = "join" AND yaer=2017 AND month=06 GROUP BY action,year,month,dt
will hit single partition.
In dt column only date will be there... may be you can change it to only day number with int as datatype and since id is timeuuid.. it will be unique.
Note: GROUP BY is supported by cassandra 3.10 and above

how to check to modify the definition

CREATE TABLE Hotel
(roomNum INTEGER NOT NULL,
arrD DATE NOT NULL,
depD DATE NOT NULL,
guestName CHAR(30) NOT NULL,
PRIMARY KEY (roomNum, arrDate));
how can I modify the definition to keep a check so that no room is booked before the departure date of previous allocation.
You can do an INSERT with a NOT EXISTS clause to check the validity of the date:
INSERT INTO Hotel (roomNum, arrD, depD, guestName)
SELECT 1, '2017-02-08', '2017-02-11', 'Tim Biegeleisen'
FROM dual
WHERE NOT EXISTS (SELECT * FROM Hotel WHERE roomNum = 1 AND depD > '2017-02-08')
I don't think that modifying the table definition will make it possible to enforce your logic.

Explicitly specifying value for an auto-incremented column in MySql

MySql forces me to specify value for an auto-incremented column. I do not understand why i need to do that.
I have created a table with the following columns
CREATE TABLE IF NOT EXISTS Aask
( task_id INT(11) AUTO_INCREMENT,
SUBJECT VARCHAR(45) DEFAULT NULL,
start_date DATE DEFAULT NULL,
end_date DATE DEFAULT NULL,
description VARCHAR(200) DEFAULT NULL,
PRIMARY KEY (task_id)
);
After creating the above table, when i try to insert rows using
INSERT INTO flask
VALUES ('Subject1','1892-12-27','1994-11-29','detailed description'),
('Subject2','1992-01-17','1694-11-31','HTML view');
I get an error message which says
Query: INSERT INTO flask VALUES ('Subject1','1892-12-27','1994-11-29','detailed description'), ('Subject2','1992-01-17','1694-11-31','H...
Error Code: 1136
Column count doesn't match value count at row 1
I know there are 5 columns in the table and i have given only 4 values in value list but why am i forced to mention value for auto increment column?
This may sound basic to most of you guys but i am just getting started with MySql so any help here would be greatly appreciated.
Use this insert
INSERT INTO flask (SUBJECT, start_date, end_date, description ) values 'Subject1','1892-12-27','1994-11-29','detailed description');

Auto add days to datetime as each days passes in mysql

I want to make a column in mysql database that when user login first time in system, it stores that datetime in mysql table. And since that day in other column days will add according to his register date. Like 1, 2, 3,....and so on. So, is there any way I can achieve the results? Please guide me soon.
You can do this with just one column (to hold the registration / first login date) and the DATEDIFF function:
CREATE TABLE users (
ID int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) NOT NULL,
registered_at datetime NOT NULL,
PRIMARY KEY (ID)
);
INSERT INTO users SET
name = 'myname',
registered_at = NOW();
SELECT registered_at, DATEDIFF(NOW(), registered_at) AS days_since
FROM users
WHERE name = 'myname';