I am trying to create three tables using MySQL Workbench, where two columns need to be auto incremented with a fixed starting value. I have checked some online resources and figured it out what statements to use.
create database test;
use test;
/*table Project */
create table Project(
Pnumber INT NOT NULL AUTO_INCREMENT,
Pname varchar(30) NOT NULL,
Plocation enum ('QLD', 'VIC', 'NSW', 'SA') NOT NULL,
primary key(Pnumber)
);
ALTER TABLE Project AUTO_INCREMENT = 7777770;
/*table Department*/
Create table Department(
Dcode varchar(5) NOT NULL,
Dname varchar(30),
Dmg_ssn varchar(30),
primary key(Dcode)
);
/* Table Employee*/
create table Employee(
Ssn INT NOT NULL AUTO_INCREMENT,
Ename varchar(30) NOT NULL,
Bdate DATE,
Address varchar(30),
Dcode varchar(5) NOT NULL,
Driver_License varchar(30),
primary key(Ssn),
foreign key(Dcode) references Department(Dcode)
);
ALTER TABLE Employee AUTO_INCREMENT = 1000000;
/* Insert into Project*/
Insert into Project values ('7777770','Star', 'QLD');
Insert into Project values ('7777771','Innova', 'NSW');
Insert into Project values ('7777772','Andra', "QLD");
/* Insert into Department */
insert into Department values ('ABC12', 'Finances', 'RA12');
insert into Department values ('WXY10', 'Human Resources', 'RA12');
insert into Department values ('PBC32', 'S2', 'RB13');
/*Insert into Employee */
insert into Employee values ('0000001','Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456');
insert into Employee values ('0000002','Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947');
insert into Employee values ('0000003','Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655');
However, after I created the tables, only the column Pnumber in table Project follows the required format, while the column Ssn in Employee does not.
This is a SELECT over the Employee table:
Ssn,Ename,Bdate,Address,Dcode,Driver_License
1,"Vladimir Rostov",2008-07-04,"19 Wilson St",ABC12,1023456
2,"Rory Reid",2002-02-10,"10 Mary St",WXY10,2365947
3,"Andy Murray",2001-05-11,"1280 Albert St",WXY10,5891655
Any idea what I am doing wrong??
Your sql script has an employee table, not a student, I assume these two names refer to the same table. I will use the employee name.
So, you set the start value auto increment of the employee table to 1000000:
ALTER TABLE Employee AUTO_INCREMENT = 1000000;
But then you explicitly insert 1, 2, and 3 into this column with your insert statements because '0000001' translates into 1. If you explicitly insert a value into auto increment and it is higher than the maximum value in the given field, then mysql will insert that value as is into the auto increment field.
If you are using auto increment, then you should let it work and do not specify an explicit value:
/*Insert into Employee */
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Vladimir Rostov', '2008-7-04', '19 Wilson St', 'ABC12', '1023456');
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Rory Reid', '2002-2-10', '10 Mary St', 'WXY10', '2365947');
insert into Employee (Ename, Bdate, Address, Dcode, Driver_License) values ('Andy Murray', '2001-5-11', '1280 Albert St', 'WXY10', '5891655');
Related
enter image description hereI MADE A TABLE THEN ADDED A COLUMN TO IT NOW ALL THE COLUMN VALUES ARE NULL IN ALL ROWS
insert into students
value
('05','0005','fname','2000','m','fyit','1234567894','king5','332','500','624','650','CS','st','walk','email');
email is where the null value is supposed to be.
1)
create database siws;
use siws;
create table students
(
student_id int(2) AUTO_INCREMENT primary key,
roll_no int(4),
fullname char(10),
dob int(4),
gender char(1),
class char(4),
phone_no bigint(10),
address varchar(20),
ssc int(3),
totssc int(3),
hsc int(3),
tothsc int(3),
department char(2),
caste char(4),
travelling char(6)
);
2)
Insert into students
value ('01','0001','aname','2000','m','syit','1234567890','king1','499','500','649','650','IT','open','bike');
insert into students
value ('02','0002','bname','2001','f','syit','1234567891','king2','497','500','639','650','CS','obc','cycle');
insert into students
value ('03','0003','cname','2002','m','fyit','1234567892','king3','437','500','629','650','IT','sc','car');
insert into students
value ('04','0004','dname','2002','f','syit','1234567893','king4','344','500','619','650','IT','open','plane');
insert into students
value ('05','0005','fname','2000','m','fyit','1234567894','king5','332','500','624','650','CS','st','walk');
3)
ALTER TABLE students
ADD Email varchar(255); //add column
sorry for the long codes im new here :)
for add a value to an existint row you need update
Update students
set email = 'youreamil#yexample.com'
where student_id =5
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>));
I have 2 tables, one for the Employees and another for the Departments. A department can have multiple/different employees but one employee may only work in one Department. Their relationship is one to many.
Table Creations:
CREATE TABLE IF NOT EXISTS department(
id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
street VARCHAR(255) NOT NULL,
employees_count INT(20) DEFAULT '0')
ENGINE=INNODB;
CREATE TABLE IF NOT EXISTS employee(
id INT(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
born INT(20) NOT NULL,
country VARCHAR(255) NOT NULL,
department_name VARCHAR(255) NOT NULL,
FOREIGN KEY (department_name) REFERENCES department(name)
ON UPDATE CASCADE ON DELETE CASCADE)
ENGINE=INNODB;
Table Insertions:
INSERT INTO department(name,street) VALUES ('Alexandroupoli', 'Leoforos Dimokratias21');
INSERT INTO department(name,street) VALUES ('Athens','Basilisis Sofias 111');
INSERT INTO department(name,street) VALUES ('Patras','Smurnis 34');
INSERT INTO department(name,street) VALUES ('Kalamata','Leoforos Fountas 241');
INSERT INTO department(name,street) VALUES ('Heraklion','Leoforos Enetwn 132');
INSERT INTO department(name,street) VALUES ('Thessaloniki','Karolou 45');
INSERT INTO department(name,street) VALUES ('Xanthi','Agia Barbasa 68');
INSERT INTO department(name,street) VALUES ('Larisa','Hroon Polutexneiou 12');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Vaggelis','Michos','vagg7#gmail.com','1995','Greece','Athens');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('James','Gunn','james8#gmail.com','1970','USA','Athens');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('George','McMahon','george95#gmail.com','1978','Usa','Patras');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('John','Jones','john13#gmail.com','1992','England','Patras');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Marinos','Kuriakopoulos','marin_kur#gmail.com','1986','Greece','Alexandroupoli');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Dimitris','Nikolaou','dimitis8#yahoo.gr','1984','Greece','Larisa');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Soufiane','El Kaddouri','sofiane#yahoo.com','1974','France','Xanthi');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Maria','Apostolou','mariamaria1#gmail.com','1997','Greece','Larisa');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Ioannis','Marinou','ioannis_ap#yahoo.gr','1982','Greece','Kalamata');
INSERT INTO employee(first_name,last_name,email,born,country,department_name) VALUES('Thanasis','Athanasiou','thanos89#gmail.com','1989','Cyprus','Heraklion');
UPDATE department SET employees_count = (SELECT COUNT(*) FROM employee WHERE department.name = employee.department_name);
Upon creating the 2 tables and creating a foreign key constraint between them, when I delete a Department from the department table, every assosiated employee get deleted too, just as I need.
BUT, when I delete an employee from the employee table, the employees_count row in department does not change. For example, if I have 2 employees with a department_name = Athens, then upon deleting one of them, when I go to departments table, the employees_count remains equal to 2 instead of 1.
Same thing happens upon updating the department_name in employees table. If for one employee, I update his department_name from "Athens" to lets say "Patras", the department table remains unchanged.
So I was thinking, after I delete an employee, maybe also execute this command
UPDATE department SET employees_count = (SELECT COUNT(*) FROM employee WHERE department.name = employee.department_name);
and that would certainly solve my issue.
BUT, is there a way for the employees_count column in department table to be AUTOMATICALLY assinged to the number of employees that work there instead of just being an integer field?
Is there a more practical way to solve this issue, instead of just adding the UPDATE command after each Deletion I make?
I'd say you don't need to store the department count in the table as a separate attribute; it's a value you can always find out through a query.
Also, deleting records can be very absolute. This is by no means best practice but I prefer to mark records as deleted (maybe with a time stamp). This is information, meta data I guess, that might be of interest. In this way all the employee information is retained. Food for thought.
Hope that was helpful.
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
I've just started on SQL and so far I've made this and it works
CREATE TABLE employees(employee_ID int NOT NULL, name varchar(20) NOT NULL UNIQUE,
PRIMARY KEY (employee_ID)
);
INSERT INTO employees VALUES(1, 'Adam Jones');
INSERT INTO employees VALUES(2, 'Amy Smith');
INSERT INTO employees VALUES(3, 'Anthony Wright');
CREATE TABLE department(department_ID varchar(20) NOT NULL,
department_name varchar(20) NOT NULL, head_of_dep varchar(20),
num_of_employees_in_dep int
);
INSERT INTO department VALUES('Bad At SQL Ltd', 'Need Help HQ', 'No One Yet', 3);
But I cant understand why this wont work
UPDATE department SET head_of_dep = name FROM employees WHERE employee_ID = 1
What am I doing wrong?
Using SQLfiddle and MySQL 5.5.32
You need to rewrite it to
UPDATE department SET head_of_dep = (SELECT name FROM employees WHERE employee_ID = 1)
because you actually have to SELECT the value