Sql inserting information into several items in a table - mysql

I got this example to work with some help on here:
http://sqlfiddle.com/#!2/92e87/1
However, if I want to try and insert information for every child of the table, I cant seem to get it to work (using this code):
CREATE TABLE country (
id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,
name varchar(255) NOT NULL
)
;
CREATE TABLE location (
id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,
name varchar(255) NOT NULL,
coordinate varchar(255) NOT NULL,
country_id integer NOT NULL REFERENCES country (id)
)
;
CREATE TABLE item (
id integer NOT NULL PRIMARY KEY AUTO_INCREMENT,
title varchar(60) NOT NULL,
description varchar(900) NOT NULL,
date datetime NOT NULL,
source varchar(255) NOT NULL,
link varchar(255) NOT NULL,
location_id integer NOT NULL REFERENCES location (id)
)
;
Insert Into item (title) values ('Title');
Insert Into item (description) values ('Description');
Insert Into item (date) values ('1995-12-31T23:59:59Z');
Insert Into item (source) values ('Source');
Insert Into item (link) values ('Link');
Insert Into item (location_id) values ('1');
Is this the correct way of doing this? Secondly, it tells me "description" doesnt have a default value, but does it need one if I will always be putting information into it?
Thank you for any help you can give

Insert Into item (title, description, date, source, link, location_id)
values ('Title', 'Description','1995-12-31T23:59:59Z','source','Link',1);
Each insert acts as inserting a new record. hence you need to combine all data into one insert statement as given above.
The SQL asks for default value, because you have mentioned NOT NULL in the table definition which you have created.

For every insert, you will end up with a new row of data in the item table. I do not think this is what you want. Instead, do:
INSERT INTO item VALUES (NULL, [title], [description], [date], [source], [link], [location_id]);
Replace the items in [ and ] with the appropriate values.
You are being told that Description does not have a default value because in your INSERT statements that don't specify a value for that column, there is no default value that the database can fill in for that field.

Related

MySQL: Created Table won't show values?

I'm new to MySQL and I created a table called students with some attributes but when I run Select * From Students it appears as null for each category I'm not sure why
use practice;
create table Students(
sid integer default 1,
sname varchar(50) default 'Joe',
GPA real default 1.7,
dateOfBirth date default (2000-12-12),
primary key(sid)
);
select *
from Students
You can try to insert a value in your table (Insert into Students ('sid', 'sname', 'GPA', 'dateOfBirth') Values ('NULL', 'joe', '1.7', '200-12-12'); this will solve your problem with the SELECT *
I would also add an auto increment on your sid so you can putt the value of sid to null and it will automatically ALTER TABLE Students MODIFY 'sid' INT(10) NOT NULL AUTO_INCREMENT PRIMARY KEY;

How to use properly IN clause getting the value from another column in MySQL?

In one table I have a list of cities and in another a list of clients. On clients I have a varchar column identifiend a list of cities separated by commas (ex: "2,3,4").
When I tried to list the cities of a client it is shown just the first city of the list.
It seem that is adding some quotation marks on the value like:
select GROUP_CONCAT(city.name)
from city where city.id_city in ('2,3,4')
¿How can avoid this situacion?
https://dbfiddle.uk/?rdbms=mysql_5.6&fiddle=a70c667e820c3208053b324075b0462c
CREATE TABLE `city` (
`id_city` int(11) NOT NULL,
`name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id_city`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `client` (
`id_client` int(11) NOT NULL,
`name` varchar(45) DEFAULT NULL,
`cities` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id_client`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO city (id_city,name) VALUES ('1','New York');
INSERT INTO city (id_city,name) VALUES ('2','Boston');
INSERT INTO city (id_city,name) VALUES ('3','San Diego');
INSERT INTO city (id_city,name) VALUES ('4','Seatle');
INSERT INTO city (id_city,name) VALUES ('5','Chicago');
INSERT INTO client (id_client,name,cities) VALUES ('1','Client_1','2,3,4');
select client.id_client, client.name, (select GROUP_CONCAT(city.name)
from city where city.id_city in (client.cities)) as cities from client;
You cannot directly pass the list to your queries. You need to change your code to -
SELECT client.id_client,
client.name,
(SELECT GROUP_CONCAT(city.name)
FROM city
WHERE FIND_IN_SET(city.id_city, client.cities) <> 0) AS cities
FROM client;
DB Fiddle.
Though this solves your purpose, I think you must consider visiting this link which clearly says that storing the comma-separated values is really a very bad idea.

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

I unable to Insert a value from a char that has been CAST as Integer and added by 1

I convert an id which is in a char column datatype. after that, I want to add it by 1 (plus 1).
Could you help me? why my query is not working?
query:
INSERT INTO `countries` (`id`, `country_name`) VALUES ((SELECT MAX(CAST(`id` as INTEGER)) AS `max_id` FROM `countries`) + 1, 'India');
The following would run:
INSERT INTO `countries` (`id`, `country_name`)
SELECT MAX(CAST(`id` as INTEGER)) + 1, 'India'
FROM `countries`;
But I think it would be easier if you just make the id column an AUTO_INCREMENT.
This is not how you should be doing identifiers.
If you want incrementing id values, you want to use the AUTO_INCREMENT feature when creating your table.
Your way is dangerous, there's always a possibility of two transactions running at the same time picking the same "next ID".
Just create a table with the flag on:
CREATE TABLE countries (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO countries (`name`) VALUES ('India');

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..