how to check to modify the definition - mysql

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.

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 add a number to a time before ordering (MySQL)

Running a very basic database including details on customers, car parks and parking sessions, I would like to return all parking sessions ordered by the end time in ascending order for a particular car.
However i only have the beginning time and length(in hours, stored as an int) of each session.
Below is my best attempt however this only orders by the start time.
(TimeDateStart in this case is the start time and date of the parking session stored as datetime datatype)
It is the order by part I cannot figure out, the rest works as desired and returns the parking sessions for this car.
SELECT *
FROM ParkingSession
JOIN Customer ON ParkingSession.CustID = Customer.CustID
WHERE Customer.Registration = "BH34 JHN"
ORDER BY (TIME(DateTimeStart) + Hours) ASC;
The Tables this is being run on is as follows (I dont think the customer table is needed but i thought i would include it anyway):
CREATE TABLE Customer (
CustID INT AUTO_INCREMENT PRIMARY KEY,
FName VARCHAR(30) NOT NULL,
LName VARCHAR(30) NOT NULL,
Registration CHAR(8) NOT NULL,
CreditCard BLOB NOT NULL,
CVC BLOB NOT NULL,
Email VARCHAR(45) NOT NULL);
CREATE TABLE ParkingSession (
CarParkID INT,
CustID INT,
DateTimeStart DATETIME,
Hours TINYINT,
PRIMARY KEY(CarParkID,CustID,DateTimeStart),
CONSTRAINT FK_Carpark FOREIGN KEY (CarParkID) REFERENCES CarPark(CarParkID),
CONSTRAINT FK_Customer FOREIGN KEY (CustID) REFERENCES Customer(CustID));
You want to add an interval of some hours to a datetime. That's
ORDER BY DateTimeStart + INTERVAL Hours HOUR

Querying of off a query MYSQL

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

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';

MySQL Stored Procedure Getting distinct count, displaying latest records only

I have the following inside my stored procedure that retrieves unique records from player names that have a faction of Neutral:
SELECT
COUNT(DISTINCT Name) into #neutcount
FROM
dim5players
WHERE
Faction ='Neutral';
UPDATE dim5stats
SET Value = #neutcount
WHERE
Function = 'Neutral';
This works find and dandy. The problem is that I have a field called Date as well.
I want to select the lastest date of the records to be listed in the count instead of a random record from the unique "Name" record.
This is a history table, and it records daily changes of the records, where Name can appear several times. I need to count only the latest records that have a faction of neutral with their latest records only. Some people change factions from time to time. I only care about their latest faction.
This is the structure:
CREATE TABLE `dim5players` (
`id` CHAR(64) NOT NULL,
`name` VARCHAR(45) NOT NULL,
`rank_name` VARCHAR(20) NULL DEFAULT NULL,
`level` INT(11) NOT NULL,
`defender_rank_id` INT(11) NULL DEFAULT NULL,
`Faction` VARCHAR(15) NOT NULL,
`Organization` VARCHAR(100) NULL DEFAULT NULL,
`Date` DATE NOT NULL,
`Updated` BIT(1) NOT NULL,
UNIQUE INDEX `id_UNIQUE` (`id`) USING HASH,
INDEX `name_index` (`name`) USING HASH,
INDEX `date_index` (`Date`) USING HASH,
INDEX `updated_index` (`Updated`) USING HASH,
INDEX `faction_index` (`Faction`) USING HASH
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
After a discussion with Michael i think i figured out what he needs:
"I want the Last Updated record of each name"
SELECT
name ,
MAX(Date) as last_date
FROM
dim5players
WHERE
Faction ='Neutral'
GROUP BY
name
"I just want to count the latest date on each NAME that still holds the faction of Neutral"
SELECT
COUNT(last_date)
FROM (
SELECT
name ,
MAX(Date) as last_date
FROM
dim5players
WHERE
Faction ='Neutral'
GROUP BY
name
) as tmp
#Michael : Let me know if i understood you requirements correctly