I have a event in which I am storing a string in a variable. Now I want to use that variable to create a new table. Everytime my event runs it creates table with the name of "mon". What is I am doing wrong ?
BEGIN
DECLARE onlyweek INT;
DECLARE mon VARCHAR(20);
SET #mon = "rehan";
CREATE TABLE mon(
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
capacity INT NOT NULL
);
END
Because you use mon instead of #mon. And even then it wont work because you need dynamic SQL for that.
But what is even more important:
Don't do that!
Don't create a table on the fly. Table designs should be static. That smells like a big design flaw.
This is a design mistake. For example, you need to make report for the year. In your design you have to join 12 tables and where-s how to join. And this is very slow.
Better design is creating 2 tables - "months" and "reporting_periods" with foreign key to table 'months'. This way when you need year report - you join only 2 tables by ID with "where".
Table 'months' can be filled once a year using same mysql events.
Then use mysql "stored procedure" (and mysql event) for periodic insert into reporting_period with month id. Months` names can include year as "bad way" or have the field 'year' = 'better one'.
CREATE TABLE months(
id int auto_increment,
name varchar(10),
number int not null,
year int not null,
constraint monthes_pk
primary key (id)
);
and reporting_period
CREATE TABLE reporting_period(
id INT auto_increment,
period_id INT NOT NOT,
capacity INT NOT NULL,
constraint `reporting_period_pk`
primary key (id),
constraint `reporting_period__fk`
foreign key (period_id) references months (id)
);
More about DB design: normalization
Hi I have this code and I have been asked for this query: Surname of the actors in alphabetical order, with the titles of the films in which they participated, and their age at
time of participation in film production (conventionally dated 30 June);
I have tried in every way the datediff function but it keeps giving me error, from what I understand does not accept the production year written in that way. How do I set the month and day as a query and then use the datediff? thank you very much for those who will help me, I'm wasting my time for nonsense. Excuse me but I'm still in the beginning
create database cinema;
use cinema;
create table participation(film varchar (3) not null ,actor varchar(5) not null);
create table actor (id_actor varchar(5) not null,name varchar(30), surname varchar(30) not null);
create table film (id_film varchar(3) not null,title varchar(30) not null, kind varchar (30) not null, producer varchar (5) not null, production_year year(4));
create table producer( id_producer varchar (5) not null, name varchar (30) ,surname varchar(30) not null);
alter table producer add primary key (id_producer);
alter table film add primary key (id_film);
alter table participation add primary key (film,actor);
alter table actor add primary key (id_actor);
alter table participation add constraint fk_pfilm foreign key (film) references film(id_film);
alter table participation add constraint fk_pactor foreign key (actor) references actor(id_actor);
alter table film add constraint fk_fproducer foreign key (producer) references producer (id_producer);
insert into producer(id_producer,name,surname) values ("0000A","Steven","Spielberg"),("0000B","Stanley","Kubrick"),("0000C","Ridley","Scott");
insert into actor(id_actor,name,surname) values ("000AA","Sylvester","Stallone"),("000AB","Brad","Pitt"),
("000AC","George","Clooney"),("000AD","Demi","Moore"),("000AE","Bruce","Willis"),
("000AF","Monica","Bellucci");
insert into film(id_film,title,kind,production_year,producer) values ("00A","Jurassic Park","avventura",'2000',"0000A"),("00B","Matrix","fantascienza",'2001',"0000B"),
("00C","Star Wars","fantascienza",'2000',"0000A"),("00D","Indiana Jones","avventura",'2002',"0000B"),("00E","Rambo","avventura",'2002',"0000A"),
("00F","Rocky I","sportivo",'2001',"0000A"),("00G","Rocky II","sportivo",'2004',"0000B");
insert into participation (film,actor) values ("00A","000AA"),("00B","000AB"),("00C","000AC"),("00D","000AD"),("00E","000AA"),
("00F","000AA"),("00G","000AB"),("00A","000AC"),("00B","000AA"),("00C","000AB"),("00D","000AB");
select * from participation;
select * from actor;
select * from film;
select * from producer;
alter table actor add column datebirth date;
update actor set datebirth='1946-07-06' where id_actor="000AA";
update actor set datebirth='1963-12-18' where id_actor="000AB";
update actor set datebirth='1961-05-06' where id_actor="000AC";
update actor set datebirth='1962-11-11' where id_actor="000AD";
update actor set datebirth='1955-03-19' where id_actor="000AE";
update actor set datebirth='1964-09-30' where id_actor="000AF";
create view vista as select * from (actor inner join participation on id_actor=participation.actor);
create view vista2 as select * from (vista inner join film on vista.film=film.id_film);
select surname,title,datediff(datebirth,production_year-06-30) as età from vista2;
Try to replace anno_produzione-06-30 with:
concat(anno_produzione, '-06-30')
The engine can cast ISO date representations to date.
And you shouldn't use double quotes around string literals. Use singles quotes instead, that's their job in SQL.
I'be been googling around about nested queries but can't find anything that I can grasp about how to go about this particular operation.
First, I'll show you my DB schema
CREATE TABLE slsemp
( empID char(4) NOT NULL,
empname varchar(50) NOT NULL,
prtime enum('yes','no') NOT NULL, # we can only accept yes or no values to the part-time employee indicator
RegionID char(2) NOT NULL, # enums are often used for boolean values in a BD
PosID char(4) NOT NULL,
PRIMARY KEY (empID),
FOREIGN KEY (regionID) REFERENCES region (RegionID),
FOREIGN KEY (PosID) REFERENCES slspos(PosID));
# create the sales transactions table
CREATE TABLE slstranx
( tranxID int(10) NOT NULL AUTO_INCREMENT, #starts at a certain number, then increments accordingly
empID char(4) NOT NULL,
ProdID char(3) NOT NULL,
Qty int(5) NOT NULL,
Unitprice Decimal(5,2) NOT NULL, # please note we are capturing the unit price at the transactional level in this case
SAmt Float(10,2), #store up to 10 (TOTAL?) with 2 digits past decimal point
SalesDate date, # 'date' data type is organized as follows YYYYMMDD. You need to make sure that raw data contains the DATE in the YYYYMMDD format
# For example 20150915
PRIMARY KEY (tranxID),
FOREIGN KEY (ProdID) REFERENCES product (ProdID),
FOREIGN KEY (empID) REFERENCES slsemp (empID));
Now, I want to find employees that are in the west region that haven't made any sales. I figured I'd do this via a left outer join between the two tables then query the resulting table based off of a null tranx ID. I've got it most of the way there, here's my query:
SELECT e.empID, t.tranxID, e.RegionID
FROM slsemp e LEFT OUTER JOIN slstranx t ON e.empID=t.empID
WHERE e.RegionID='WS'
My question is, how do I query based of the criteria of this resultant table. If I could do that, I simply would need a selection with criteria of slstranxID=null.
You can use left join adding where slstranx.empID is null
select distinct empID, empName
from slsemp
left join slstranx on slsemp.empID = slstranx.empID
where slsemp.RegionID = 'WS'
and slstranx.empID is null
if the column from the table in left join is null mean that don't match .. so don't have sales
Let's say I have tables:
create table people (
human_id bigint auto_increment primary key,
birthday datetime );
create table students (
id bigint auto_increment primary key,
human_id bigint unique key not null,
group_id bigint not null );
create table teachers (
id bigint auto_increment primary key,
human_id bigint unique key not null,
academic_degree varchar(20) );
create table library_access (
access_id bigint auto_increment primary key,
human_id bigint not null,
accessed_on datetime );
Now I want to display information about a library access, along with the information whether it was a student or a teacher (and then the id corresponding to the table) (let's say I want something like SELECT access_id,id,true_if_student_false_if_teacher FROM library_access), in an idiomatic way.
How do I form the query (in case such database was already deployed) and what are better and more idiomatic ways to solve that problem (in case it wasn't deployed so far).
MariaDB 5.5, database accessed by Go and nothing else.
Thanks in advance.
You said you need to know which table the data comes from. You can use union all for this:
select la.access_id, s.id, 'Students' as source_table
from library_access la
join students s on la.human_id = s.human_id
union all
select la.access_id, t.id, 'Teachers' as source_table
from library_access la
join teachers t on la.human_id = t.human_id
Without looking at your tables or any idea as to what you want returned in the select statement:
SELECT *
FROM people a,
students b,
teachers c,
library_access d
WHERE a.human_id = b.human_id
AND a.human_id = c.human_id
AND a.human_id = d.human_id
I have 2 tables city_sessions_1 and city_sessions_2
Structure of both table are similar
CREATE TABLE `city_sessions_1` (
`city_id` int(11),
`session_date` date,
`start_time` varchar(12),
`end_time` varchar(12) ,
`attendance` int(11) ,
KEY `city` (`city_id`),
KEY `session_date` (`session_date`)
) ENGINE=MyISAM;
Note these tables do not have any primary key, but they have their indexes defined. Both tables have same number of rows. But it is expected that some data would be different.
How can I compare these 2 tables' data?
-- We start with the rows in city_session_1, and their fit in city_session_2
SELECT
* -- or whatever fields you are interested in
FROM city_sessions_1
LEFT JOIN city_sessions_2 ON city_sessions_1.city_id=city_sessions_2.city_id
WHERE
-- Chose only those differences you are intersted in
city_sessions_1.session_date<>city_session_2.session_date
OR city_sessions_1.start_time<>city_session_2.start_time
OR city_sessions_1.end_time<>city_session_2.end_time
OR city_sessions_1.attendance<>city_session_2.attendance
UNION
-- We need those rows in city_session_2, that have no fit in city_session_1
SELECT
* -- or whatever fields you are interested in
FROM city_sessions_2
LEFT JOIN city_sessions_1 ON city_sessions_1.city_id=city_sessions_2.city_id
WHERE city_sessions_1.city_id IS NULL