Related
I created some tables but when I want to check if my data is inside it doesn't show anything when
I use select * from Project it just appears null and I don't have null values I'm not sure what I'm doing wrong it just appears that the foreign key is restrict
`create table Department
(
`did integer not null auto_increment,
Dname varchar(50) default 'HR',
location varchar(50) default 'Chicago',
primary key(did)`
);
`create table Employee`
(
Eid integer not null auto_increment,
DepartmentID integer default 5,
Ename varchar(50) default 'Josh',
Erank integer default 2,
Salary real default 5000.00,
primary key(Eid),
foreign key(DepartmentID) references Department(did)
);
/*drop table Project;*/
create table Project
(
Pid integer not null auto_increment,
DepartmentID integer default 5,
Pname varchar(50) default 'Sorting',
budget real default 5000.00,
StartYear integer default 2000,
primary key(Pid),
foreign key(DepartmentID) references Department(did)
);
insert
into Project(DepartmentID, Pname, budget, StartYear)
values(1, 'OS', 5000.00, 2018),
(2, 'Net', 6000.00, 2020);
select *
from Project;
Check this fiddle. There are no rows in Department, so the restraint keeps the insert into Projects from happening. Also, I removed the backticks:
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=64f8f0e9cccce2eb2ab77b37fcce54fd
Add COMMIT statements:
create table Project...
commit;
insert into Department values('HR','Chicago');
insert into Department values('Admin','New York');
insert into Project...
commit;
select * from Project;
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');
`CREATE TABLE DEPT
(
DEPT_NO INT PRIMARY KEY,
D_NAME VARCHAR(255) NOT NULL,
LOC VARCHAR(255) NOT NULL
);
CREATE TABLE EMP
(
EMP_NO INT primary key,
E_NAME VARCHAR(255) NOT NULL,
JOB VARCHAR(255) NOT NULL,
MGR INT,
HIRE_DATE DATE NOT NULL,
SAL INT NOT NULL,
COMM INT,
DEPT_NO INT NOT NULL,
foreign key(dept_no) references dept(dept_no)
);
INSERT INTO DEPT (DEPT_NO,D_NAME,LOC)
VALUES ('10','ACCOUNTING','NEW YORK');
INSERT INTO DEPT (DEPT_NO,D_NAME,LOC)
VALUES ('20','RESEARCH','DALLAS');
INSERT INTO DEPT (DEPT_NO,D_NAME,LOC)
VALUES ('30','SALES','CHICAGO');
INSERT INTO DEPT (DEPT_NO,D_NAME,LOC)
VALUES ('40','OPERATIONS','BOSTON');
insert into emp values(7369,"SMITH","CLERK",7902,"1980-12-17",800," ",20);'
`
You declared in the 'emp' table the column 'comm' as INT and in the insert you are inserting a string, the right thing would be:
insert into emp values(7369,"SMITH","CLERK",7902,"1980-12-17",800,0,20);
or
insert into emp values(7369,"SMITH","CLERK",7902,"1980-12-17",800,null,20);'
here is your error insert into emp values(7369,"SMITH","CLERK",7902,"1980-12-17",800," ",20)'
just before the value 20 from last
its " " which just means that there's nothing and is a String datatype with space.
So to solve it make it as
use default keyword to insert default value in the table.
where here default value is NULL
insert into emp values(7369,"SMITH","CLERK",7902,"1980-12-17",800,default,20)'
OR
insert into emp values(7369,"SMITH","CLERK",7902,"1980-12-17",800,0,20)'
OR
Just use NULL keyword to insert null value in the table
insert into emp values(7369,"SMITH","CLERK",7902,"1980-12-17",800,NULL,20)'
tables are given below.
CREATE TABLE `departments` (
department_id INT(2) NOT NULL AUTO_INCREMENT,
department_name VARCHAR(30) NOT NULL,
total_employees INT(4),
PRIMARY KEY (department_id),
UNIQUE (department_name));
CREATE TABLE `employees` (
employee_id INT(4) NOT NULL AUTO_INCREMENT,
employee_email VARCHAR(30) NOT NULL,
employee_first_name VARCHAR(30) NOT NULL,
employee_last_name VARCHAR(30) NOT NULL,
department_name VARCHAR(30) NOT NULL,
PRIMARY KEY (employee_id),
UNIQUE (employee_email),
FOREIGN KEY (department_name)
REFERENCES departments (department_name)
ON DELETE CASCADE);
this is the trigger, I want it to be showing the sum of total employees in each department.
delimiter $$
create trigger department_wise_total_employee_counting
after insert on employees
for each row begin update departments set total_employees=total_employees+1
where department_id=department_id; end$$ delimiter ;
INSERT INTO `departments`
VALUES
(1,'HRM',0),(2,'Accounting',0);
INSERT INTO `employees`
VALUES
(1,'bh#gmail.com','A','B','HRM'),
(2,'ak#gmail.com','C','D','HRM'),
(3,'mr#gmail.com','E','F','HRM'),
(4,'pr#gmail.com','G','H','Accounting');
On running the following query :
select * from departments;
I'm getting this output, which just gives the total employee count rather than the total for each department.
I am trying to get total_employees=3 for HRM and total_employees=1 for Accounting.
Would appreciate any sort of suggestions.
As was pointed out by #P.Salmon, in general you shouldn't store data you can easily calculate. For this application, a VIEW (as suggested by #TamilSelvanC) is a good solution. For example:
CREATE VIEW departments_view AS
SELECT d.department_id, d.department_name, COUNT(e.employee_id) AS total_employees
FROM departments d
LEFT JOIN employees e ON e.department_name = d.department_name
GROUP BY d.department_id;
SELECT * FROM departments_view
Output:
department_id department_name total_employees
1 HRM 3
2 Accounting 1
3 Engineering 0
Demo on dbfiddle
I want to create a database have two tables. I want to create a update cascade relationship between two tables. If the SNAME was changed in EMPLOYEE, then WORKREPORT will change his SNAME too.
EMPLOYEE:
SNO SNAME SPASSWORD SEX BDATE HEIGHT BTITLE
2014 boss 12345 male 1987-06-02 180 Manager
2015 Tom 1234567 male 1987-06-05 180 Employee
WORKREPORT
SNO SNAME SDATA SCHECKLIST SIMAGE
2014 boss 1987-06-02 abc afafafaf
2015 Tom 1987-06-05 affafa afafafaf
My code works very well. My problems is that : I don't know how to only query the employee's information(rather than manager) from the "WORKREPORT", suppose there are many employees. What should I do? Am I right about about the designing "WORKREPORT"?
This is my code:
CREATE database if not exists cm360_cm360_1;
use cm360_cm360_1;
CREATE TABLE IF NOT EXISTS EMPLOYEE(SNO VARCHAR(7) NOT NULL, SNAME VARCHAR(8) NOT NULL, SPASSWORD VARCHAR(11) NOT NULL,SEX VARCHAR(8) NOT NULL, BDATE DATETIME NOT NULL, HEIGHT DEC(5,2) DEFAULT 000.00,BTitle VARCHAR(15) NOT NULL, PRIMARY KEY(SNO), UNIQUE KEY (SNAME))ENGINE=InnoDB ;
SET SQL_SAFE_UPDATES=0;
INSERT INTO EMPLOYEE VALUES (2014,'boss','12345','male','2014-6-10 11:00:00',160.00,'Manager');
INSERT INTO EMPLOYEE VALUES (2015,'Tom','1234567','male','2014-6-10 12:00:00',160.00,'Employee');
SELECT * FROM EMPLOYEE;
CREATE TABLE IF NOT EXISTS WORKREPORT(SNO VARCHAR(7) NOT NULL, SNAME VARCHAR(8) NOT NULL,SDATA DATETIME , SCHECKLIST VARCHAR(150),SIMAGE VARCHAR(20),FOREIGN KEY (SNO) REFERENCES EMPLOYEE (SNO) ON UPDATE CASCADE,FOREIGN KEY(SNAME) REFERENCES EMPLOYEE (SNAME) ON UPDATE CASCADE ) ENGINE=InnoDB;
INSERT INTO WORKREPORT VALUES (2014,'boss',' 2014-6-10 14:38:59','abc','afdsfdfds');
INSERT INTO WORKREPORT VALUES (2015,'Tom',' 2014-6-10 15:38:59','abc','afdsfdfds');
SELECT * FROM WORKREPORT order by SDATA ASC;
UPDATE WORKREPORT SET SCHECKLIST='elevator;floor' WHERE SNAME='hanlu2';
delete from workreport WHERE SIMAGE='N/A' AND SNAME='enlan';
If you want to get Employee WORKREPORT information, query below should work..
SELECT WORKREPORT.*
FROM WORKREPORT, EMPLOYEE
WHERE EMPLOYEE.SNO = WORKREPORT.SNO
AND EMPLOYEE.BTITLE = "Employee"
If you just want to get all employee information, you can JOIN the two tables on SNO and/or SNAME since you have a FK relationship on those column and filter by the condition that BTITLE shouldn't be MANAGER
select e.*
from employee e
join workreport w on e.SNO = w.SNO
and e.SNAME = w.SNAME
and e.BTITLE <> 'Manager'