CREATE TABLE Editor (
UsernameID VARCHAR (30),
EditorName VARCHAR (30),
EMail VARCHAR (30),
DateOfBirth DATE,
BlogTitle VARCHAR(30),
PRIMARY KEY (UsernameID));
INSERT INTO Editor VALUES
('Mdbuzzer','Joshua', 'coker#hotmail.com', '1995-03-15', 'Nearly Bound');
INSERT INTO Editor VALUES
('Kally32','Kally', 'kally#hotmail.com', '1993-10-13', 'Tomorrows War');
SELECT * FROM Editor;
CREATE TABLE Post
(UsernameID VARCHAR (30),
PostID INT,
BlogTitle VARCHAR (30),
PostTitle VARCHAR (30),
CategoryID INT,
TimeofPost VARCHAR (20),
PRIMARY KEY(PostID),
FOREIGN KEY (UsernameID) REFERENCES Editor (UsernameID));
INSERT INTO Post VALUES
('Kally32','1','Tomorrows War', 'Mystery', '4', '12:03pm');
INSERT INTO Post VALUES
('Kally32','1','Tomorrows War', 'Let It Shine','2','10:05pm');
INSERT INTO Post VALUES
('Kally32','1','Tomorrows War','Two Can Play That Game','2', '12:00pm');
INSERT INTO Post VALUES
( 'Mdbuzzer','2','Nearly Bound', 'Goal','3', '10:05pm');
INSERT INTO Post VALUES
( 'Mdbuzzer','2','Nearly Bound','Life After Death','4', '12:03pm');
INSERT INTO Post VALUES
('Mdbuzzer','2', 'Nearly Bound','Times Up','1', '14:06pm');
SELECT * FROM Post;
Every time I try and run this script I keep getting an error saying duplicate entry '1' for key 'primary' and I don't really understand how to fix it
The problem is that you are inserting multiple rows into the POST table with a PRIMARY KEY of 1. If you are trying to implement a primary key, it must be unique.
So this:
INSERT INTO Post VALUES
('Kally32','1','Tomorrows War', 'Mystery', '4', '12:03pm');
INSERT INTO Post VALUES
('Kally32','1','Tomorrows War', 'Let It Shine','2','10:05pm');
INSERT INTO Post VALUES
('Kally32','1','Tomorrows War','Two Can Play That Game','2', '12:00pm');
INSERT INTO Post VALUES
( 'Mdbuzzer','2','Nearly Bound', 'Goal','3', '10:05pm');
INSERT INTO Post VALUES
( 'Mdbuzzer','2','Nearly Bound','Life After Death','4', '12:03pm');
INSERT INTO Post VALUES
('Mdbuzzer','2', 'Nearly Bound','Times Up','1', '14:06pm');
Could be changed to something like this:
INSERT INTO Post VALUES
('Kally32','1','Tomorrows War', 'Mystery', '4', '12:03pm');
INSERT INTO Post VALUES
('Kally32','2','Tomorrows War', 'Let It Shine','2','10:05pm');
INSERT INTO Post VALUES
('Kally32','3','Tomorrows War','Two Can Play That Game','2', '12:00pm');
If you make a column a primary key, then every value in the column must be unique. You already have one instance of '1' in the column POST_ID when you add the first tuple:
('Kally32','1','Tomorrows War', 'Mystery', '4', '12:03pm');
and if you try adding another tuple with the instance of '1' in the column POST_ID, such as:
('Kally32','1','Tomorrows War', 'Let It Shine','2','10:05pm');
you will get an error.
You have declared PostId to be the primary key of POST.
When you use insert, you should always include the columns. So, these are the first two inserts into the table:
INSERT INTO Post(UsernameID, PostID, BlogTitle, PostTitle, CategoryID, TimeofPost)
VALUES('Kally32','1','Tomorrows War', 'Let It Shine','2','10:05pm');
INSERT INTO Post(UsernameID, PostID, BlogTitle, PostTitle, CategoryID, TimeofPost)
VALUES('Kally32','1','Tomorrows War','Two Can Play That Game','2', '12:00pm');
You are setting PostId to the same value, 1, in both cases. Hence the error.
I would expect the code to look like:
CREATE TABLE Post (
UsernameID VARCHAR (30),
PostID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
BlogTitle VARCHAR (30),
PostTitle VARCHAR (30),
CategoryID INT,
TimeofPost VARCHAR (20),
FOREIGN KEY (UsernameID) REFERENCES Editor (UsernameID)
);
INSERT INTO Post(UsernameID, BlogTitle, PostTitle, CategoryID, TimeofPost)
VALUES('Kally32', 'Tomorrows War', 'Let It Shine', 2, '10:05pm');
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);
I have a simple table with students in MySQL database. Similar to:
student_id
student_name
teacher_id
1
Adam
100
2
Bob
100
3
Carl
100
4
Dan
200
Teachers can input new students or change existing student's names. Currently I have this set up to update via:
INSERT INTO student_list (name) VALUES (:name) ON DUPLICATE KEY UPDATE student name= values(name)
The problem is that this completely breaks any associated student_ids in other tables (as the ON DUPLICATE KEY UPDATE changes the student_id). I could look up each entry before updating, but it seems messy. Surely there's a better way.
CREATE TABLE `sabrep_db`.`students` ( `student_id` INT NOT NULL AUTO_INCREMENT , `name` TEXT NOT NULL , `teacher_id` INT NOT NULL , PRIMARY KEY (`student_id`)) ENGINE = InnoDB;
INSERT INTO `students` (`student_id`, `name`, `teacher_id`) VALUES (NULL, 'Adam', '100'), (NULL, 'Bob', '100');
Should also pass a key (usually the primary key) as part of the insert so that it knows the key reference to update when there is a duplicate, name is passed twice for the update portion:
INSERT INTO student_list (student_id, name)
VALUES (:student_id, :name) ON DUPLICATE KEY UPDATE student_name = :name;
I am trying to create a table called "Borrower" or "Lånetagare" and want to insert the value from table Lån with the ID from there. The result I get is NULL. How come?
create table Lån
(
Lån_ID INT auto_increment primary key,
Lånedatum DATETIME,
Inlämningsdatum DATETIME,
Omlån DATETIME
);
INSERT INTO Lån (Lånedatum, Inlämningsdatum, Omlån)
VALUES ('2017-09-12', '2017-09-15', '2017-09-15');
CREATE TABLE Lånetagare
(
Lånetagare_ID INT(10) auto_increment primary key,
Lösenord varchar(50),
Förnamn varchar(50),
Efternamn varchar(50),
Adress varchar (50),
Ort varchar(50),
Postnummer int(5),
Email varchar(50),
Telefonnummer int(20),
Lånekort int(50),
Lån_ID int(50),
FOREIGN KEY (Lån_ID) REFERENCES Lån(Lån_ID)
);
INSERT INTO Lånetagare (Lösenord, Förnamn, Efternamn, Adress, Ort, Postnummer, Email, Telefonnummer, Lånekort)
VALUES ('hej135', 'Victor', 'Chi', 'Blekingegatan 28', 'Stockholm', 11856, 'Tim#hotmail.com', 0704582235, 56);
SELECT * FROM Lånetagare;
If you want to associate a row in Lånetagare with the previously-inserted row in Lån, you must set a value for the foreign key.
Here's a common way to do it:
INSERT INTO Lån (Lånedatum, Inlämningsdatum, Omlån)
VALUES ('2017-09-12', '2017-09-15', '2017-09-15');
INSERT INTO Lånetagare (...other columns..., Lån_ID)
VALUES (...other values..., LAST_INSERT_ID());
The LAST_INSERT_ID() function returns the most recent auto-increment id created by an INSERT statement during your session. Read more about this function here: https://dev.mysql.com/doc/refman/5.7/en/information-functions.html#function_last-insert-id
Try
INSERT INTO Lånetagare (Lösenord, Förnamn, Efternamn, Adress, Ort, Postnummer, Email, Telefonnummer, Lånekort, Lån_ID)
VALUES ('hej135', 'Victor', 'Chi', 'Blekingegatan 28', 'Stockholm', 11856, 'Tim#hotmail.com', 0704582235, 56, (SELECT Lån_ID FROM Lån WHERE <put your select criteria here>));
Given this table where both reviewid and prankid are auto increment.
CREATE TABLE Review
(
reviewId INT NOT NULL AUTO_INCREMENT,
email VARCHAR(32) NOT NULL,
prankId INT NOT NULL,
rating INT,
comment VARCHAR(1056) NOT NULL,
PRIMARY KEY(reviewId),
FOREIGN KEY (email) REFERENCES User(email),
FOREIGN KEY (prankId) REFERENCES Prank(prankId)
);
Would this insert statement correctly insert values into all of the attributes in review table.
INSERT INTO Review (email, prankId) SELECT email, prankId from User;
INSERT INTO Review (rating, comment) VALUES(‘5’,’amazing!’);
INSERT INTO Review (rating, comment) VALUES(‘5’,’brilliant!’);
I suspect that you want this:
INSERT INTO Review (email, prankId, rating, comment)
SELECT email, prankId, 'S', 'amazing!'
from User;
INSERT INTO Review (email, prankId, rating, comment)
SELECT email, prankId, 'S', 'brilliant'
from User;
In your version, the last two inserts will fail because email and prankid are NULL. Remember, insert adds new rows into the table. It doesn't modify existing rows.
Hey guys I've searched for answers through the forums but to no avail so I'm using MySql and I'm trying to insert statements for certain tables and they aren't going into the tables and I'm getting errors like "Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated. The statement has been terminated."
These are the statements I'm having problems with.`INSERT INTO Course VALUES
INSERT INTO Course VALUES (12345, 'DatabaseManagement', '2015-2-1', '2014-5-9');
INSERT INTO Course VALUES (12346, 'Calculus', '2015-1-12', '2015-5-9');
INSERT INTO Course VALUES (12347, 'Biology', '2015-1-3', '2015-5-9');
INSERT INTO Course VALUES (12348, 'Chemistry', '2015-1-2', '2015-5-9');
INSERT INTO Grade VALUES (10, 12345, 012, 'A');
INSERT INTO Grade VALUES (11, 12346, 013, 'B');
INSERT INTO Grade VALUES (12, 12347, 014, 'C');
INSERT INTO Grade VALUES (13, 12348, 015, 'D');
INSERT INTO Grade VALUES (14, 12345, 016, 'B');
INSERT INTO Student VALUES (54321, 'Rachel', 'Cotterel', '2013-4-15', '2016-3-4');
INSERT INTO Student VALUES (54320, 'John', 'Smith', '2012-1-23', NULL);
INSERT INTO Student VALUES (54319, 'Johny', 'Depp', '2010-5-12', '2012-10-10');
INSERT INTO Student VALUES (54318, 'Orlando', 'Bloom', '2014-6-24', NULL);
INSERT INTO Student VALUES (54317, 'Linda', 'Jacob', '2015-4-4', '2019-8-6');
I didn't get any error for insert into Course statements. I got error for INSERT INTO Grade statements. Its because there is no reference available for StudentID 012,013 etc in Student table. And you are trying to add them in grade table.
Try using this:
INSERT INTO table1 (column1,column2,column3,...)
VALUES (value1,value2,value3,...);
These are the field types:
CREATE TABLE Course
(
CourseID int,
Description varchar(20) NOT NULL,
StartDate DATE NOT NULL,
EndDate DATE NOT NULL,
CONSTRAINT [PK_CourseID] PRIMARY KEY (CourseID)
);
CREATE TABLE Grade
(
GradeID integer(10) NOT NULL,
CourseID integer(10) NOT NULL,
StudentID integer(10) NOT NULL,
Grade varchar (10) NULL,
CONSTRAINT [PK_GradeID] PRIMARY KEY (GradeID),
CONSTRAINT [FK_CourseID] FOREIGN KEY (CourseID) REFERENCES Course(CourseID),
CONSTRAINT [FK_StudentID] FOREIGN KEY (StudentID) REFERENCES Student(StudentID)
);
CREATE TABLE Student
(
StudentID integer(10) NOT NULL,
FirstName varchar(45) NOT NULL,
LastName varchar(45) NOT NULL,
RegistrationDate varchar (45) NOT NULL,
GraduationDate DATE NULL,
CONSTRAINT [PK_StudentlID] PRIMARY KEY (StudentID)
);
String or binary data would be truncated
The reason that you get this message should be that you are trying to insert some value to some field to which you haven't assigned enough size to hold the value.
Can you send what the exact error message you get?
I tried to do it myself.But the error I got was from you insertion query to Grade table foreign key fails which refer Student table because you are trying to insert Student_IDs which are not there in you Student table