Can't Create MYSQL DataBase Tables [closed] - mysql

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
I am trying to create MYSQL tables for my DataBase but I keep getting an error
Here is my code
CREATE DATABASE Hospitals;
CREATE TABLE hospital (
hospitalId INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
postcode VARCHAR(10),
phoneNumber VARCHAR(20),
emailAddress VARCHAR(255),
PRIMARY KEY (hospitalId),
) ENGINE = INNODB;
CREATE TABLE wards (
wardID INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
specialisation VARCHAR(100),
PRIMARY KEY (wardID),
) ENGINE = INNODB;
CREATE TABLE staff (
staffId INT NOT NULL AUTO_INCREMENT,
foreName VARCHAR(50),
surName VARCHAR(50),
position VARCHAR(50),
salary DOUBLE,
PRIMARY KEY (staffId)) ENGINE = INNODB;
CREATE TABLE patients (
niNumber VARCHAR(50),
name VARCHAR(100),
dob DATE,
sex VARCHAR(10),
nokName VARCHAR(100),
nokPhone VARCHAR(50),
PRIMARY KEY niNumber) ENGINE = INNODB;
CREATE TABLE medications (
medicationId INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
contraIndications VARCHAR(255),
PRIMARY KEY (medicationId)) ENGINE = INNODB;
INSERT INTO hospital (hospitalId, name, postcode, phoneNumber, emailAddress)
VALUES(1, 'Bangor', 'LL72GW', '01276541789', 'Bangor.Hospital#gmail.com'),
VALUES(2, 'london', 'SW1A 0AA', '01673531789', 'London.Hospital#gmail.com');
INSERT INTO wards (wardId, name, specialisation)
VALUES(1, 'Ward1', 'Brain Surgery'),
VALUES(2, 'Ward2', 'Heart Surgery');
INSERT INTO staff (staffId, foreName, surName, position, salary)
VALUES(1, 'Malcolm', 'Gross', 'Manager', '50000.0'),
VALUES(1, 'Lewis', 'Castle', 'Surgeon', '30000.0');
INSERT INTO patients (niNumber, name, dob, sex, nokName, nokPhone)
VALUES('001AFCY6', 'Sam', '20-02-2002', 'male', 'David', '09876543268'),
VALUES('0SSHD2J3', 'Jim', '19-09-1977', 'male', 'Terry', '06789347382');
INSERT INTO medications (medicationId, name, contraIndications)
VALUES(1, 'Heart Medicine', 'Dont Use if heart is ok'),
VALUES(2, 'Antibiotics', 'Dont use if patient is feeling well');
But the error I keep getting is Query 1 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 'VALUES(2, 'Antibiotics', 'Dont use if patient is feeling well')' at line 3
I have no idea why I am getting such an error
What am I doing wrong and how can I fix my error please
Many Thanks

You dont need to use 2 VALUES keywords in your INSERT statements. So you updated INSERT statement should look alike -
INSERT INTO hospital (hospitalId, name, postcode, phoneNumber, emailAddress)
VALUES(1, 'Bangor', 'LL72GW', '01276541789', 'Bangor.Hospital#gmail.com'),
(2, 'london', 'SW1A 0AA', '01673531789', 'London.Hospital#gmail.com');
And you have to get rid of last commas in first 2 tables also -
CREATE TABLE hospital (
hospitalId INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
postcode VARCHAR(10),
phoneNumber VARCHAR(20),
emailAddress VARCHAR(255),
PRIMARY KEY (hospitalId) --, This should be removed.
) ENGINE = INNODB;
CREATE TABLE wards (
wardID INT NOT NULL AUTO_INCREMENT,
name VARCHAR(100),
specialisation VARCHAR(100),
PRIMARY KEY (wardID) --, This should be removed.
) ENGINE = INNODB;

Related

SQL Error Code: 1049. Unknown database 'dbo'

I am trying to create a database use mysql workbench and I keep getting this error and my code looks fine. Not sure how I can resolve this. This is the error I am getting: Error Code: 1049. Unknown database 'dbo'.
I have tried so many things but only the first two lines of creating and using the database works. [screenshot attached]
use employeedb;
create table dbo.Department(
DeprartmentId int AUTO_INCREMENT,
DepartmentName nvarchar(500),
PRIMARY KEY(DepartmentId)
);
insert into dbo.Department(DepartmentName) values ('IT');
insert into dbo.Department(DepartmentName) values ('Support');
create table dbo.Employee(
EmployeeId int AUTO_INCREMENT,
EmployeeName nvarchar(500),
Department nvarchar(500),
DateOfJoining datetime,
PhotoFileName nvarchar(500),
PRIMARY KEY(EmployeeId)
);
insert into dbo.Employee(EmployeeName,Department,DateOfJoining,PhotoFileName)
values ('John','IT','2022-11-27','anonymous.png');
select * from dbo.Employee;
You seem to be using SQL Server syntax, which won't work on MySQL. Here is your script updated for MySQL:
USE employeedb;
CREATE TABLE Department (
DeprartmentId int AUTO_INCREMENT,
DepartmentName varchar(500),
PRIMARY KEY(DepartmentId)
);
INSERT INTO Department(DepartmentName) VALUES ('IT');
INSERT INTO Department(DepartmentName) VALUES ('Support');
CREATE TABLE Employee (
EmployeeId int AUTO_INCREMENT,
EmployeeName varchar(500),
Department varchar(500),
DateOfJoining datetime,
PhotoFileName varchar(500),
PRIMARY KEY(EmployeeId)
);
INSERT INTO Employee (EmployeeName, Department, DateOfJoining, PhotoFileName)
VALUES ('John', 'IT', '2022-11-27', 'anonymous.png');
SELECT * FROM Employee;
Seems you are changing from MSSQL to MySQL, for MySQl do not need "dbo", just:
use employeedb;
create table Department(
DeprartmentId int AUTO_INCREMENT,
DepartmentName nvarchar(500),
PRIMARY KEY(DepartmentId)
);
....

HOW TO FILL THE NULL VALUES IN ROW?

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

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?

MySQL UPDATE value in SQLfiddle

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

MySQL query returning data with detailed / duplicated information

I am working on MySQL database, and I need to select some data with procedure. So I have something like:
CREATE TABLE pet (id INT, name VARCHAR(20), own_id INT);
insert into pet values (1,"Rufus", 1);
insert into pet values (2,"Bali", 1);
insert into pet values (3,"Lolo", 2);
ref pet.own_id = own.id
CREATE TABLE own (id INT, own_name VARCHAR(20), own_color VARCHAR(20));
insert into own values (1,"Me", "Red");
insert into own values (2,"Other" ,"Green");
And now I wonder how to select / join data to get something like that (as results):
own_name own_color name
Me Red Rufus
Me Red Bali
Other Green Lolo
SELECT own_name, own_color, name
from pet
JOIN own on (pet.own_id = own.id)
;
You have only to join both tables:
Select own_name,own_color, name from own join pet on
pet.own_id = own.id
select o.own_name, o.own_color, p.name from own o, pet p
where p.own_id=o.id
It's a JOIN.
While a JOIN is a valid answer, some people find subqueries easier to read and write:
SELECT own_name, own_color, (SELECT name
FROM pet
WHERE pet.own_id = own.id) AS name
FROM own
This is basically the same as the JOIN method, but then as a subquery
I have a suggestion for your tables: own_id is a foreign key, so instead of
CREATE TABLE pet (id INT, name VARCHAR(20), own_id INT);
CREATE TABLE own (id INT, own_name VARCHAR(20), own_color VARCHAR(20));
I'd do:
CREATE TABLE own (
id INT NOT NULL AUTO_INCREMENT,
own_name VARCHAR(20) NOT NULL,
own_color VARCHAR(20) NOT NULL,
PRIMARY KEY (id)
);
CREATE TABLE pet (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(20) NOT NULL,
own_id INT NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (own_id) REFERENCES own (id)
);
I added some other things as well.
Improvements/changes:
I assumed none of the fields should be NULL. I added NOT NULL to all the fields making it impossible for a field to have a value of NULL. (If you want some fields to be allowed to have a value of NULL just remove the NOT NULL from the CREATE TABLE statements.)
I made both id fields increment automatically. This means the insert statements can now be just insert into own (own_name, own_color) values ("Me", "Red"); and the database will automatically keep track of the ids for you.
I added primary keys so the database knows that the rows can be identified by the id fields
I added a foreign key constraint, meaning that every own_id in pet must exist in own. If you try to insert or alter a pet row in a way that breaks this constraint mysql will throw an error at you.