not accepting null value in mysql - mysql

`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)'

Related

Nothing shows when I use select *

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;

sql how to use select query to get invoke foreign key

i was wondering how do i use the select query to Retrieve all offers of a listing.
Any tips or help to improve my codes is appreciated to! Thank you and have a nice day!
CREATE DATABASE IF NOT EXISTS `assignment_db`;
USE `assignment_db`;
CREATE TABLE USER_LIST(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
userName VARCHAR(50) NOT NULL,
email varchar(100) NOT NULL,
registeredDate timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);
create table listing_list(
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
itemName VARCHAR(50) NOT NULL,
itemDescription VARCHAR(254) NOT NULL,
price DECIMAL(4,2) NOT NULL,
fk_poster_id int references USER_LIST(id),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);
create table offer_list(
id int(6) Unsigned auto_increment Primary key,
offer int,
fk_listing_id int references listing_list(id),
fk_offeror_id int references user_list(id),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
);
insert into user_list (userName, email) values ('John','johnnyboi#123.com');
insert into user_list (userName, email) values ('Tom','Tommyboi#123.com');
insert into listing_list (itemName,itemDescription, price) values ( 'Pen', 'A long delicate pen.',' 1.50 ');
insert into listing_list (itemName,itemDescription, price) values ( 'Pencil', 'A long delicate pencil.',' 0.50 ');
insert into offer_list (offer,fk_listing_id,fk_offeror_id) values ('200','2','3');
insert into offer_list (offer,fk_listing_id,fk_offeror_id) values ('200','1','1');
All offer listing for existing user :
select a.*,b.*,c.* from offer_list a
INNER JOIN (listing_list as b)
on a.fk_listing_id=b.Id
INNER JOIN (user_list as c)
on a.fk_offeror_id=c.Id
just all offer listing :
select a.*,b.* from listing_list b
INNER JOIN (offer_list as a)
on a.fk_listing_id=b.Id
just all offer listing 2nd way
select a.*,b.* from offer_list a
INNER JOIN (listing_list as b)
on a.fk_listing_id=b.Id
Check it here , i tested them in this FIDDLE

I've two tables a trigger that's auto filling the 2nd table. How can I fetch data so that null values for an attribute will be avoided automatically?

I've two tables a trigger that's auto filling the 2nd table.
How can I fetch data so that null values for an attribute will be avoided automatically?
CREATE TABLE `departments` (
department_id INT(2) NOT NULL AUTO_INCREMENT,
department_name VARCHAR(30) NOT NULL,
PRIMARY KEY (department_id),
UNIQUE (department_name));
CREATE TABLE `designations_of_departments` (
department_id INT(2) NOT NULL AUTO_INCREMENT,
designation_1 VARCHAR(30),
designation_2 VARCHAR(30),
designation_3 VARCHAR(30),
designation_4 VARCHAR(30),
designation_5 VARCHAR(30),
FOREIGN KEY (department_id)
REFERENCES departments (department_id)
ON DELETE CASCADE);
delimiter $$
create trigger trigger_1 after insert on departments
for each row begin
insert into designations_of_departments(department_id, designation_1, designation_2, designation_3, designation_4, designation_5)
values (new.department_id, null, null, null, null, null);
end$$ delimiter ;
I tried with this query, but it still shows null values.
select * from designations_of_departments
where not (designation_1 <=> null and designation_2 <=> null and
designation_3 <=> null and designation_4 <=> null and designation_5 <=>
null) and department_id=1;
Example :
INSERT INTO `departments` VALUES (1,'HRM'), (2,'Accounting')(3,'Engineering');
UPDATE designations_of_departments SET
designation_1 = 'Senior HR',
designation_2 = 'HR',
designation_3 = 'Junior HR' WHERE department_id = 1;
This is what I tried according to suggestion in comments :
select * from designations_of_departments
where designation_1 is not null
and designation_2 is not null
and designation_3 is not null
and designation_4 is not null
and designation_5 is not null
and department_id=1;

Auto_increment trigger

I need to auto_increment the primary key in a mysql database using a trigger. Unfortunately, I am not quite sure how to do this. In the sample code I have provided, I need the employee table primary key to auto_increment beginning with an empty table and a starting value of 200. Then, I need each new insert to increment by 1.Thanks for looking and I hope you are able to help me.
CREATE TABLE department (
dept_name VARCHAR(50) NOT NULL Primary Key
);
CREATE TABLE employee (
emp_id INT(6) unsigned Default 0 Not NULL
, last_name VARCHAR(25) NOT NULL
, first_name VARCHAR(40) NOT NULL
, dept_name VARCHAR(50) NOT NULL
, PRIMARY KEY(emp_id, dept_name)
, FOREIGN KEY(dept_name) REFERENCES department (dept_name)
);
There are several things you need to do:
Declare the emp_id column as AUTO_INCREMENT;
Set the value of AUTO_INCREMENT property of the table to 200;
Do not provide any value for column emp_id when you INSERT rows in table employee.
Change the table creation as below:
CREATE TABLE employee (
emp_id INT(6) UNSIGNED NOT NULL AUTO_INCREMENT,
last_name VARCHAR(25) NOT NULL,
first_name VARCHAR(40) NOT NULL,
dept_name varchar(50) NOT NULL
PRIMARY KEY(emp_id),
FOREIGN KEY(dept_name) REFERENCES department_tbl(dept_name)
) AUTO_INCREMENT=200;
If the table has an AUTO_INCREMENT column then it must be the PRIMARY KEY of the table. I removed dept_name from the definition of the PK above. I also removed the default value 0 from the emp_id column. It's default value is generated by MySQL using the AUTO_INCREMENT policy.
When you INSERT a new record into the employee table all you have to do is to not provide any value for the emp_id column:
INSERT INTO employee (last_name, first_name, dept_name)
VALUES ('Doe', 'John', 'accounting');
Then use the LAST_INSERT_ID() MySQL function to retrieve the value of the emp_id generated on insertion.
The language or the library you use to develop the client application probably has a function that wraps LAST_INSERT_ID() and returns its value.

inner join gives in mysql gives error code 1052

I created a database with following tables :
CREATE SCHEMA IF NOT EXISTS `facturatiedatabase` ;
USE `facturatiedatabase` ;
DROP TABLE IF EXISTS tblAddress ;
DROP TABLE IF EXISTS tblContact ;
DROP TABLE IF EXISTS tblCustomers ;
CREATE TABLE tblCustomers (
customerID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
vat VARCHAR(30) NOT NULL,
customerVisible varchar(1) NOT NULL DEFAULT 'T'
) ENGINE=InnoDB;
CREATE TABLE tblContact (
contactID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(100),
phone VARCHAR(100),
customerID int,
CONSTRAINT FK_customerID_Contact FOREIGN KEY (customerID) REFERENCES tblCustomers(customerID)
) ENGINE=InnoDB;
CREATE TABLE tblAddress (
addressID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
street VARCHAR(100),
houseNumber VARCHAR(15),
city VARCHAR (100),
country VARCHAR (100),
customerID int,
CONSTRAINT FK_customerID_Adress FOREIGN KEY (customerID) REFERENCES tblCustomers(customerID)
) ENGINE=InnoDB;
INSERT INTO tblCustomers (firstname, lastname,vat)
VALUES ("John","Doe","UV45856855");
INSERT INTO tblContact (customerID,phone, email)
VALUES ((SELECT DISTINCT LAST_INSERT_ID() FROM tblCustomers), "0000001","Johndoe#gmail.com");
INSERT INTO tblAddress (customerID,street,housenumber,city,country)
VALUES ((SELECT DISTINCT LAST_INSERT_ID() FROM tblCustomers), "berkenlaan","1a","Harelbeke","Belgie");
But when i try following inner join it gives me the following error :
LIMIT 0, 1000 Error Code: 1052. Column 'customerID' in field list is ambiguous 0.000 sec.
SELECT customerID, firstname, lastname, vat,email
FROM tblCustomers
INNER JOIN tblContact on tblCustomers.customerID = tblContact.contactID
The error message says it all: the column name customerID is contained in both tables. So which value should mysql select? That is ambiguous, therefore the error.
Have a try with this variant:
SELECT tblCustomers.customerID AS customerID, firstname, lastname, vat,email
FROM tblCustomers
INNER JOIN tblContact on tblCustomers.customerID = tblContact.contactID
(or pick that other tables's column with the same if you need that one...)
And, frankly, I doubt your ON clause is the one you want to use. Shouldn't that be ON tblCustomers.customerID = tblContact.customerID instead?