MySQL about constraint - mysql

Good afternoon,
Can anyone tell me what's wrong with my code on PHP MY ADMIN, I'm trying to write a CONSTRAINT and create values for the car color in the beginning ( with table creation)
CREATE TABLE T_CAR
(CAR_ID INTEGER NOT NULL PRIMARY KEY,
CAR_MARK CHAR(32) NOT NULL,
CAR_MODEL VARCHAR(16),
CAR_NUMBER CHAR(10) NOT NULL,
CAR_COLOR CHAR(16) CHECK (VALUE IN ('white', 'black', 'red', 'green', 'blue')))
The problem is with the last line (error message syntax not known).
Thanks in advance.

MySQL ignores check expression.
Manual: Create Table
The CHECK clause is parsed but ignored by all storage engines.
Try Enum:
CREATE TABLE T_CAR (
CAR_ID INTEGER NOT NULL PRIMARY KEY,
CAR_MARK CHAR(32) NOT NULL,
CAR_MODEL VARCHAR(16),
CAR_NUMBER CHAR(10) NOT NULL,
CAR_COLOR ENUM('white', 'black', 'red', 'green', 'blue') NOT NULL
)

Related

MySQL INSERT INTO statement giving ERROR CODE 1062

I have a F1 database and I'm trying to insert the team principal's name (from Principals table) into the Teams table, but for some reason it won't work. I'm not sure if my insert into statement is wrong, but I can't see why it would be. The code is below
CREATE TABLE Teams (
Team_Name VARCHAR(30) NOT NULL,
Driver_1 INT NULL,
Driver_2 INT NULL,
Nation VARCHAR(30) NOT NULL,
Seasons INT NOT NULL,
No_Titles INT NOT NULL DEFAULT 0,
Principal VARCHAR(30) DEFAULT NULL,
PRIMARY KEY (Team_Name));
CREATE TABLE Principals (
Principal_No INT NOT NULL,
Principal_Name VARCHAR(30) UNIQUE NOT NULL,
Team VARCHAR(30) NULL,
Age INT NOT NULL,
Nationality VARCHAR(30) NOT NULL,
Seasons INT NOT NULL DEFAULT 0,
PRIMARY KEY (Principal_No),
FOREIGN KEY (Team) REFERENCES Teams(Team_Name));
Here's the insert statement. The Principals table has already been populated with the principal names and their corresponding teams
INSERT INTO Teams (Principal)
SELECT Principal_Name
FROM Principals
WHERE Team IN(
SELECT Team_Name
FROM Teams);
In your INSERT statement
INSERT INTO Teams (Principal)
-- ^
you're only inserting a value for the field Principal into the table Teams. But the Teams table has other fields as well... not inserting values into them will default them to NULL. This will lead to a contradiction in the arguments. In your Teams table:
Team_Name VARCHAR(30) NOT NULL
Nation VARCHAR(30) NOT NULL
Seasons INT NOT NULL
these fields, by definition, can't be NULL. But since no values are inserted with the INSERT statement, these will default to NULL, clashing with the definition and causing an error.
You may want to consider setting default values for the above fields or modifying your INSERT statement to accommodate those fields.
INSERT INTO Teams (Principal, Team_Name, Nation, Seasons)
SELECT
-- four columns ...
Your insert statement tries to assign null values to columns that have 'not null' property. Check these columns 'not null' -> false or assign default value or define values in your insert statement. I hope this help

Can anyone help me with date command

So I´ve looked trough the web in search for basic date help in sql and nobody seems to be able to help my codes goes like this
create table Hotel
(
id int not null primary key auto_increment,
Name varchar(255)
);
create table Gestur
(
id int not null primary key auto_increment,
nafn varchar(255),
heimili varchar(255),
simi char(7),
netfang varchar(255)
);
create table Bokun
(
id int not null primary key auto_increment,
ID_hotel_fk int references Hotel(id),
ID_gestur_fk int references Gestur(id),
dags_inn date null,
dags_ut date null,
tegund_herbergis char(1)
);
and I can´t seem to get this part right
insert into Bokun
(ID_gestur_fk,ID_hotel_fk,dags_inn,dags_ut,tegund_herbergis)
values
(1,3, 2015-10-25,2016-12-26,"1"),
(2,5, 2015-04-01, 2016-8-24,"3"),
(3,4, 2014-02-24, 2016-12-08,"1"),
(4,2, 2015-04-26, 2016-12-24,"2"),
(5,4, 2015-07-14, 2016-04-23,"1"),
(6,2, 2015-12-12, 2016-09-12,"3"),
(7,3, 2015-12-26, 2016-05-03,"2"),
(8,2, 2013-09-12, 2014-06-10,"1"),
(9,1, 2015-05-26, 2016-12-28,"1"),
(10,5, 2015-03-30, 2016-06-07,"4");
I only get the error
1292 - Incorrect date value: '1980' for column 'dags_inn' at row 1
You need to quote dates with ' and qoute "1" as '1':
insert into Bokun(ID_gestur_fk,ID_hotel_fk,dags_inn,dags_ut,tegund_herbergis)
values(1,3, '2015-10-25','2016-12-26','1');
2015-10-25 is treated as 1980 (aritmetic operation substraction)
SqlFiddleDemo
insert into Bokun
(ID_gestur_fk,ID_hotel_fk,dags_inn,dags_ut,tegund_herbergis)
values
(1,3, '2015-10-25','2016-12-26',"1"),
(2,5, '2015-04-01', '2016-8-24',"3"),
(3,4, '2014-02-24', '2016-12-08',"1"),
(4,2, '2015-04-26', '2016-12-24',"2"),
(5,4, '2015-07-14', '2016-04-23',"1"),
(6,2, '2015-12-12', '2016-09-12',"3"),
(7,3, '2015-12-26', '2016-05-03',"2"),
(8,2, '2013-09-12', '2014-06-10',"1"),
(9,1, '2015-05-26', '2016-12-28',"1"),
(10,5, '2015-03-30', '2016-06-07',"4");
you missed '(quotes).Thanks this helps..

Auto_increment trigger

I need to auto_increment the primary key in a mysql database using a trigger. Unfortunately, I am not quite sure how to do this. In the sample code I have provided, I need the employee table primary key to auto_increment beginning with an empty table and a starting value of 200. Then, I need each new insert to increment by 1.Thanks for looking and I hope you are able to help me.
CREATE TABLE department (
dept_name VARCHAR(50) NOT NULL Primary Key
);
CREATE TABLE employee (
emp_id INT(6) unsigned Default 0 Not NULL
, last_name VARCHAR(25) NOT NULL
, first_name VARCHAR(40) NOT NULL
, dept_name VARCHAR(50) NOT NULL
, PRIMARY KEY(emp_id, dept_name)
, FOREIGN KEY(dept_name) REFERENCES department (dept_name)
);
There are several things you need to do:
Declare the emp_id column as AUTO_INCREMENT;
Set the value of AUTO_INCREMENT property of the table to 200;
Do not provide any value for column emp_id when you INSERT rows in table employee.
Change the table creation as below:
CREATE TABLE employee (
emp_id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
last_name VARCHAR(25) NOT NULL,
first_name VARCHAR(40) NOT NULL,
dept_name varchar(50) NOT NULL
PRIMARY KEY(emp_id),
FOREIGN KEY(dept_name) REFERENCES department_tbl(dept_name)
) AUTO_INCREMENT=200;
If the table has an AUTO_INCREMENT column then it must be the PRIMARY KEY of the table. I removed dept_name from the definition of the PK above. I also removed the default value 0 from the emp_id column. It's default value is generated by MySQL using the AUTO_INCREMENT policy.
When you INSERT a new record into the employee table all you have to do is to not provide any value for the emp_id column:
INSERT INTO employee (last_name, first_name, dept_name)
VALUES ('Doe', 'John', 'accounting');
Then use the LAST_INSERT_ID() MySQL function to retrieve the value of the emp_id generated on insertion.
The language or the library you use to develop the client application probably has a function that wraps LAST_INSERT_ID() and returns its value.

Use a trigger like assertion

I know that in MySQL we can't use assertion. I Have these tables:
CREATE TABLE `soggiorno` (
`idSoggiorno` varchar(16) NOT NULL,
`price` int(11) NOT NULL,
PRIMARY KEY (`idSoggiorno`))
CREATE TABLE `prenotazione` (
`idPrenotazione` varchar(16) NOT NULL,
`soggiorno` varchar(16) NOT NULL,
`paymentType` varchar(45) NOT NULL,
PRIMARY KEY (`idPrenotazione`))
CONSTRAINT `guest_ibfk_1` FOREIGN KEY (`soggiorno`) REFERENCES `soggiorno` (`idSoggiorno`)
I have to ensure that, if 'price' > 1500, you can't pay with "cash". How can I do that without assertion? I'm thinking for a trigger...Thanks to all
Try
CREATE TRIGGER tg_prenotazione_before_insert
BEFORE INSERT ON prenotazione
FOR EACH ROW
SET NEW.soggiorno = IF((SELECT price
FROM soggiorno
WHERE idSoggiorno = NEW.soggiorno) > 1500
AND NEW.paymentType = 'Cash', NULL, NEW.soggiorno);
What is does it checks for your desired conditions (if price > 1500 and paymentType is 'Cash') and if it's a match it violates NOT NULL constraint effectively preventing a row from being inserted.
Here is SQLFiddle demo. Try to uncomment the last insert statement in the demo and you'll see that it won't let you insert it.

What's wrong with this SELECT Statement?

I'm getting this error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '' JOIN `shirts_link` ON `shirts_link`.`shirt_id`=`shirts`.`id`
JOIN `shirt_sizes' at line 1
I've been combing over it for nearly a half an hour now and can't figure it out. Can someone please point it out to me? Here's the code.
SELECT
`shirts`.`shirt_name`,
`shirts`.`men` AS `main_photo`,
GROUP_CONCAT ( `shirt_sizes`.`size_name` ) AS `sizes`
FROM
`shirts`
JOIN
`shirts_link` ON `shirts_link`.`shirt_id`=`shirts`.`id`
JOIN
`shirt_sizes` ON `shirt_sizes`.`id`=`shirts_link`.`size_id`
JOIN
`shirt_prices` ON `shirt_prices`.`id`=`shirts_link`.`price_id`
WHERE `men`!=''
GROUP BY
`shirt_prices`.`price_cat`
In case the problem goes deeper than this script, here's the other tables I'm trying to tie together.
Shirts Table
CREATE TABLE shirts (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
shirt_name VARCHAR(20) NOT NULL,
men VARCHAR(10) NULL,
women VARCHAR(10) NULL,
boys VARCHAR(10) NULL,
girls VARCHAR(10) NULL,
babies VARCHAR(10) NULL,
)ENGINE=INNODB;
INSERT INTO shirts(shirt_name,men,women,boys,girls,babies) VALUES
('Crewneck Tee','me_crn','wo_crn','bo_crn','gi_crn','ba_crn'),
('V-Neck Tee','me_vnc','wo_vnc','','',''),
('Scoop Neck Tee','','wo_sco','','',''),
('Raglan Tee','me_rag','wo_rag','bo_rag','gi_rag',''),
('Ringer Tee','me_rin','wo_rin','bo_rin','gi_rin',''),
('Cap Sleeve Tee','','wo_cap','','gi_cap',''),
('Tank Top','me_tan','wo_tan','bo_tan','gi_tan',''),
('Spaghetti Strap','','wo_spa','','',''),
('Hoodie','me_hod','wo_hod','bo_hod','gi_hod','ba_hod'),
('Onsie','','','','','ba_ons');
Size Table
CREATE TABLE shirt_sizes (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
size_name VARCHAR(10) NOT NULL
)ENGINE=INNODB;
INSERT INTO shirt_sizes(size_name) VALUES
('new born'),
('6 months'),
('12 months'),
('18 months'),
('2T'),
('3T'),
('4T'),
('5T'),
('x-small'),
('small'),
('medium'),
('large'),
('1x-large'),
('2x-large'),
('3x-large'),
('4x-large'),
('5x-large');
Price Table
CREATE TABLE shirt_prices (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
price_cat VARCHAR(10) NOT NULL,
price NUMERIC(6,2) NOT NULL
)ENGINE=INNODB;
INSERT INTO shirt_prices(price_cat,price) VALUES
('crn_01','25.40'),('crn_02','26.30'),('crn_03','27.20'),('crn_04','28.10'),
('crn_05','29.11'),
('vnc_01','26.21'),('vnc_02','27.11'),('vnc_03','28.01'),('vnc_04','29.02'),
('vnc_05','30.80'),
('sco_01','28.10'),
('rag_01','29.22'),('rag_02','30.44'),('rag_03','31.70'),('rin_01','29.22'),
('rin_02','30.44'),('rin_03','31.70'),
('cap_01','29.04'),('cap_02','30.26'),
('tan_01','25.31'),('tan_02','26.21'),
('spa_01','26.30'),('spa_02','27.11'),
('hod_01','35.21'),('hod_02','36.11'),('hod_03','37.10'),('hod_04','38.11');
Shirt Link Table
CREATE TABLE shirts_link (
adult VARCHAR(1) NOT NULL,
kids VARCHAR(1) NOT NULL,
babies VARCHAR(1) NOT NULL,
shirt_id INT UNSIGNED NOT NULL,
size_id INT UNSIGNED NOT NULL,
price_id INT UNSIGNED NOT NULL,
PRIMARY KEY (shirt_id,size_id,price_id),
FOREIGN KEY (`shirt_id`) REFERENCES `shirts`(`id`),
FOREIGN KEY (`size_id`) REFERENCES shirt_sizes(`id`),
FOREIGN KEY (`price_id`) REFERENCES shirt_prices(`id`)
)ENGINE=INNODB;
INSERT INTO shirts_link (adult, kids, babies, shirt_id, size_id, price_id) VALUES
('n','n','y','1','1','1'),('n','n','y','1','2','1'),('n','n','y','1','3','1'),('n','n','y','1','4','1'),
('n','n','y','1','5','1'),('n','n','y','1','6','1'),('n','n','y','1','7','1'),('n','n','y','1','8','1'),
('n','y','n','1','9','1'),('y','y','n','1','10','1'),('y','y','n','1','11','1'),('y','y','n','1','12','1'),
('y','y','n','1','13','1'),('y','n','n','1','14','2'),('y','n','n','1','15','3'),('y','n','n','1','16','4'),
('y','n','n','1','17','5'),
('y','n','n','2','10','6'),('y','n','n','2','11','6'),('y','n','n','2','12','6'),('y','n','n','2','13','6'),
('y','n','n','2','14','7'),('y','n','n','2','15','8'),('y','n','n','2','16','9'),('y','n','n','2','17','10'),
('y','n','n','3','10','11'),('y','n','n','3','11','11'),('y','n','n','3','12','11'),('y','n','n','3','13','11'),
('y','n','n','3','14','11'),
('y','y','n','4','10','12'),('y','y','n','4','11','12'),('y','y','n','4','12','12'),('y','y','n','4','13','12'),
('y','n','n','4','14','13'),('y','n','n','4','15','14'),
('y','y','n','5','10','15'),('y','y','n','5','11','15'),('y','y','n','5','12','15'),('y','y','n','5','13','15'),
('y','n','n','5','14','16'),('y','n','y','5','15','17'),
('y','y','n','6','10','18'),('y','y','n','6','11','18'),('y','y','n','6','12','18'),('y','y','n','6','13','18'),
('y','n','n','6','14','19'),
('y','y','n','7','10','20'),('y','y','n','7','11','20'),('y','y','n','7','12','20'),('y','y','n','7','13','20'),
('y','n','n','7','14','21'),
('y','n','n','8','10','22'),('y','n','n','8','11','22'),('y','n','n','8','12','22'),('y','n','n','8','13','22'),
('y','n','n','8','14','23'),
('n','n','y','9','5','24'),('n','n','y','9','6','24'),('n','n','y','9','7','24'),('n','n','y','9','8','24'),
('n','n','y','9','9','24'),('y','y','n','9','10','24'),('y','y','n','9','11','24'),('y','y','n','9','12','24'),
('y','y','n','9','13','24'),('y','y','n','9','14','25'),('y','n','n','9','15','26'),('y','n','n','9','16','27'),
('y','n','n','9','16','25'),
('n','n','y','10','1','1'),('n','n','y','10','2','1'),('n','n','y','10','3','1'),('n','n','y','10','4','1');
Where clause belongs after the entire from clause, meaning after all the joins.
You have this WHERE clause in the wrong place:
WHERE `men`!=''
Move it in front of your GROUP BY clause.
UPDATE: Here is a reformatted version of your query:
SELECT `shirts`.`shirt_name`
, `shirts`.`men` AS `main_photo`
, GROUP_CONCAT ( `shirt_sizes`.`size_name` ) AS `sizes`
FROM `shirts`
JOIN `shirts_link`
ON `shirts_link`.`shirt_id`=`shirts`.`id`
JOIN `shirt_sizes`
ON `shirt_sizes`.`id`=`shirts_link`.`size_id`
JOIN `shirt_prices`
ON `shirt_prices`.`id`=`shirts_link`.`price_id'
WHERE `men` != ''
GROUP BY `shirt_prices`.`price_cat`