CREATE TABLE Movies (
ID int PRIMARY KEY,
Title varchar(50),
Director varchar(50),
Year int ,
Length_minutes decimal(5,2)
);
Here is the insert statement:
INSERT INTO Movies VALUES (1,'Toy Story','John Lasseter',1995,81) ;
INSERT INTO Movies VALUES (2,'A Bug\'s life','John Lasseter',1998,95) ;
INSERT INTO Movies VALUES (3,'Toy','John Lasseter',1999,93) ;
INSERT INTO Movies VALUES (4,'Monsters,Inc','Pete Docter',2001,92) ;
INSERT INTO Movies VALUES (5,'Finding Nemo','Andrew Stanton',2003,107) ;
Schema Error: Error: ER_PARSE_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 'INSERT INTO Movies VALUES (3,'Toy Story 2 ','John Lasseter',1999,93) ; INSERT I' at line 4
The insert statements after id2 don't work.
Primary Keys cannot be repeated. If you are inserting the same one again that is an issue.
MySQL table creation
CREATE TABLE movies(
id INT UNSIGNED AUTO_INCREMENT, PRIMARY KEY(id),
title VARCHAR(250),
director VARCHAR(250),
year TINYINT,
minutes TINYINT
)ENGINE=InnoDB;
Since the id is a PRIMARY KEY that AUTO_INCREMENTs, you will have to label all of your fields.
MySQL insertion (only use on first insert)
INSERT movies (title, director, year, minutes) VALUES('Toy Story 2', 'John Lasseter', 1999, 93);
MySQL update
UPDATE movies SET title='Toy Story 2', director='John Lasseter', year=1999, minutes=93 WHERE id=3;
I see the issue from the INSERT statement is here:
INSERT INTO Movies VALUES (2,'A Bug\'s life','John Lasseter',1998,95) ;
There are two ways you can do this either escaping the single quote with another single quote like '' or replace the opening and closing single quotes to double quotes. Look at this two example:
INSERT INTO Movies VALUES (2,'A Bug''s life','John Lasseter',1998,95) ;
INSERT INTO Movies VALUES (6,"A Bug's life","John Lasseter",1998,95) ;
Fiddle: https://www.db-fiddle.com/f/DfwTYpTr8Xj3McLi9KZNZ/2
Try :
INSERT INTO Movies (ID, Title, Director, Year, Length_minutes) VALUES (3,'Toy Story 2','John Lasseter',1999,93) ;
Related
I'm new to SQL and learning its basic functions, i've been instructed to create a table and add it to my database, whilst then adding the appropriate values to each field in the table. But I keep getting a syntax error in the line:
INSERT into Weather_db.client_data
Here is my code so far. Let me know what I should change:
Q1.) Using the CREATE TABLE statement, create a table called client_data with fields
CREATE TABLE Weather_db.client_data (
ID int PRIMARY KEY,
First_name varchar(40) NOT NULL,
Last_name varchar(40),
Nationality varchar(40),
Age Float Check (age>18))
Q2.) Insert the following records in the database using the INSERT statement
INSERT into Weather_db.client_data
VALUES ('John', 'S',
'British', 'null'),
('Peter', 'Jackson', 'null', '20'),
('Tom', 'W', 'null', '20'),
('Jack', 'Patrick', 'American', '30');
ID int PRIMARY KEY,
This column definition is legal, but it doesn't have any automatic behavior to generate unique values. So you must supply a value yourself in your INSERT statement, and it's up to you to make sure the value is unique, i.e. it is not already used on any existing row in this table.
MySQL provides an option for integer primary keys to generate new unique values automatically:
ID int AUTO_INCREMENT PRIMARY KEY,
By using this, you don't have to specify a value in your INSERT statement.
You may like to read this manual page for more information: https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html
Your statement has a few syntax issues.
Firstly you are not providing values for all the defined columns.
It's also good practice to always specifically list the columns in the insert statement.
You should not be quoting numeric values, or null values.
You should probably define the ID as auto_increment which means the database will supply a default value.
See a working example
CREATE TABLE client_data (
ID int PRIMARY key auto_increment,
First_name varchar(40) NOT NULL,
Last_name varchar(40),
Nationality varchar(40),
Age Float Check (age>18));
INSERT into client_data (first_name, last_name, Nationality, Age)
VALUES ('John', 'S','British', null),
('Peter', 'Jackson', null, 20),
('Tom', 'W', null, 20),
('Jack', 'Patrick', 'American', 30);
There are two tables:
create table author (id int primary key auto_increment, name varchar(255));
insert into author (name) values ('tingwei');
insert into author (name) values ('jiahui');
insert into author (name) values ('naidan');
insert into author (name) values ('weizhi');
insert into author (name) values ('siyao');
create table book (author_id int primary key auto_increment, book varchar(255));
insert into book (book) values ('I love you');
insert into book (book) values ('I hate you');
insert into book (book) values ('I miss you');
But I don't know what does this statement mean by using number 1 behind "select":
select *
from author
where exists (select 1 from book where book.author_id = author.id)
I've already searched some information online but I got nothing.
SELECT 1 does just that: it selects the value 1. When used with WHERE EXISTS it does not matter what data the subquery returns, as long as it returns any data (in other words: returns at least one row).
I have a question for an assignment with 5 tables as shown below. I need to write a query with the minimum cost for each sport:
2nd column is equipment_name:
I think I need to do a bunch of joins in subqueries with the primary keys being the id columns and the foreign keys the name_id columns. Is this the right approach?
You don't need a bunch of joins; minimally this question can be solved by one join between the store_equipment_price and the sports_equipment tables - if these two are joined on equipment id then you'll effectively get rows that can give the cost of starting up in each sport per store. You'll need to group by the sport id and the store id; don't forget that it might be cheaper to start soccer by getting all the gear from store A but it might be cheaper to start golf by going to tore B - tho I how I read the question. If however you're prepared to get your gloves from store A and your bat from store B etc then we don't even group by the store when summing, instead we work out which store is cheapest for each component rather than which store is cheapest for each sport overall.
If you're after producing named stores/sports on your result rows then you'll need more joins but try getting the results right based on the fewest number of joins possible to start with
Both these queries will ultimately be made a lot easier by the use of an analytic/windowing function but these are database dependent; never post an sql question up without stating what your db vendor is, as there are few questions that are pure ISO SQL
You question is not completely clear, I assume you need to find stores from which to buy each equipment for all sports so as to incur minimum expense. Following query will achieve this
select s.sports, e.equipment_name, min(sep.price),
(select store_name from stores st where st.id = sep.store_id) store_name
from sports s
join sports_equipment se on s.id = se.sport_id
join equipment e on e.id = se.equipment_id
join sports_equipment_prices sep on sep.equipment_id = se.equipment_id
group by s.sports, e.equipment_name
order by s.sports, e.equipment_name
;
Following 'create table' and 'insert data' script are based on your screen images
create table sports (
id INTEGER PRIMARY KEY AUTOINCREMENT,
sports varchar(50)
);
insert into sports(sports) values('golf');
insert into sports(sports) values('baseball');
insert into sports(sports) values('soccer');
create table stores (
id INTEGER PRIMARY KEY AUTOINCREMENT,
store_name varchar(50)
);
insert into stores(store_name) values('A');
insert into stores(store_name) values('B');
insert into stores(store_name) values('C');
create table equipment (
id INTEGER PRIMARY KEY AUTOINCREMENT,
equipment_name varchar(50)
);
insert into equipment(equipment_name) values('shoes');
insert into equipment(equipment_name) values('ball');
insert into equipment(equipment_name) values('clubs');
insert into equipment(equipment_name) values('glove');
insert into equipment(equipment_name) values('bat');
create table sports_equipment (
sport_id INTEGER not null,
equipment_id INTEGER not null,
FOREIGN KEY(sport_id) REFERENCES sports(id),
FOREIGN KEY(equipment_id) REFERENCES equipment(id)
);
insert into sports_equipment values(1, 1);
insert into sports_equipment values(1, 2);
insert into sports_equipment values(1, 3);
insert into sports_equipment values(2, 2);
insert into sports_equipment values(2, 4);
insert into sports_equipment values(2, 5);
insert into sports_equipment values(3, 1);
insert into sports_equipment values(3, 2);
create table sports_equipment_prices (
id INTEGER PRIMARY KEY AUTOINCREMENT,
store_id INTEGER not null,
equipment_id INTEGER not null,
price INTEGER not null,
FOREIGN KEY(store_id) REFERENCES stores(id),
FOREIGN KEY(equipment_id) REFERENCES equipment(id)
);
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');
I am trying to add the employee first name but I keep getting the error. I have tried changes based on similar issue and videos I have watched but nothing seems to get rid of the error.
create table `Employee Information`.`Employee`(
`EmployeeID` int not null,
`EmployeeFirstName` varchar(255) not null,
`EmployeeLastName` varchar(255) not null,
`SupervisorID` int not null,
primary key (`EmployeeID`),
foreign key (`SupervisorID`) references employee (`EmployeeID`)
on delete no action
on update no action
);
insert into `Employee` (EmployeeID, EmployeeFirstName, EmployeeLastName, SupervisorID) values (1, `John`, `Smith`, 52);
Any help?
You are using backticks (\``) for your values rather than regular quotes ('`). The backticks are used around table names and column names. Single quotes are used for strings like the strings "John" and "Smith" in your value pair. The following SQL statement would work assuming that "52" is in fact an existing supervisor:
INSERT INTO `Employee`
(`EmployeeID`, `EmployeeFirstName`, `EmployeeLastName`, `SupervisorID`)
VALUES (1, 'John', 'Smith', 52)