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)
);
....
Related
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;
So basically I want to create a trigger for mysql database that has same/similar function with the assertion code below (since mysql does not support assertion)
Here is the assertion code that I want to replicate using trigger
CREATE ASSERTION assert
CHECK NOT EXISTS(SELECT * FROM paper P WHERE 3 <>(SELECT COUNT(*)
FROM review R
WHERE R.paperid = P.paperid)
);
CREATE ASSERTION atmostfivepapers
CHECK NOT EXISTS(SELECT * FROM pcmember P WHERE 5 <
( SELECT *
FROM review R
WHERE R.email = P.email
)
);
And here is my table
CREATE TABLE paper(
paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
abstract VARCHAR(250),
pdf VARCHAR(100),
PRIMARY KEY(paperid)
);
CREATE TABLE author(
email VARCHAR(100) NOT NULL,
name VARCHAR(50),
affiliation VARCHAR(100),
PRIMARY KEY(email)
);
CREATE TABLE writePaper(
paperid INT UNSIGNED NOT NULL AUTO_INCREMENT,
email VARCHAR(100),
paper_order INT,
PRIMARY KEY(paperid, email),
FOREIGN KEY(paperid) REFERENCES paper(paperid),
FOREIGN KEY(email) REFERENCES author(email)
);
CREATE TABLE pcmember(
email VARCHAR(100) NOT NULL,
name VARCHAR(20),
PRIMARY KEY(email)
);
CREATE TABLE review(
reportid INT UNSIGNED,
sdate DATE,
comment VARCHAR(250),
recommendation CHAR(1),
paperid INT UNSIGNED,
email VARCHAR(100),
PRIMARY KEY(paperid, email),
FOREIGN KEY(paperid) REFERENCES paper(paperid),
FOREIGN KEY(email) REFERENCES pcmember(email)
);
Thanks in advance
First create another table replicating the columns and their features. Thereafter create a trigger.
CREATE TRIGGER afterInsert_trigger on yourMainTableName
FOR INSERT
AS
INSERT INTO yourTableCopy(column1ofYourReplicateTable, column2OFyourReplicateTable, ... etc)
SELECT column1ofYourMainTable, column2ofYourMainTable, ..., etc
FROM INSERTED
That's what I use in Microsoft SQL server
CREATE TABLE CU_ORDER
( cordernumber INT PRIMARY KEY,
fname_lname VARCHAR(50) NOT NULL,
product_name VARCHAR(100) NOT NULL,
);
CREATE TABLE TRACKING_NUMBER
( trnumber INT PRIMARY KEY
);
INSERT INTO CU_ORDER VALUES(456, 'John Doe' , Table);
INSERT INTO TRACKING_NUMBER(276734673);
I am trying to created a table called Package and in the table it will have all the items from cu_order and all the items from tracking_number. How will I add all of the attributes of this table to one table. What am I doing wrong?
CREATE TABLE PACKAGE
( orderno INT PRIMARY KEY,
fname VARCHAR(50) NOT NULL,
name VARCHAR(100) NOT NULL,
trno INT PRIMARY KEY);
INSERT INTO PACKAGE (........
The two tables do not seem to have a relation, so, presumably, you want a cartesian product of both tables. If so, you can use the insert ... select ... syntax with a cross join:
insert into package(orderno, fname, name, trno)
select
co.cordernumber,
co.fname_lname,
co.product_name,
tn.trnumber
from cu_order co
cross join tracking_number tn
This inserts all possible combinations of rows from both source tables in the target table.
You should also fix the declaration of the package table: yours has two primary keys, which is not allowed. Instead, you probably want a compound primary key made of both columns:
create table package (
orderno int,
fname varchar(50) not null,
name varchar(100) not null,
trno int,
primary key(orderno, trno)
);
You can create a new table from the data of another table (or several tables) by appending a SELECT statement to the CREATE TABLE statement.
However, your two source tables are missing the 1:1 relation allowing this to work, which I assume is the cordernumber of CU_ORDER. It appears the table TRACKING_NUMBER is missing a 'cordernumber' column.
CREATE TABLE TRACKING_NUMBER (
trnumber INT PRIMARY KEY,
cordernumber INT
);
After you added the column 'cordernumber' to TRACKING_NUMBER, you can create the new table PACKAGE with:
CREATE TABLE PACKAGE (
orderno INT PRIMARY KEY,
fname VARCHAR(50) NOT NULL,
name VARCHAR(100) NOT NULL,
trno INT PRIMARY KEY
)
SELECT
CU_ORDER.cordernumber AS orderno,
CU_ORDER.fname_lname AS fname,
CU_ORDER.product_name AS name,
TRACKING_NUMBER.trnumber AS trno
FROM CU_ORDER, TRACKING_NUMBER
WHERE CU_ORDER.cordernumber=TRACKING_NUMBER.cordernumber;
I want to select values from basic2 and insert into basic3 and basic4 using stored procedure.
These are the table definitions:
create table basic2(
id int AUTO_INCREMENT,
name varchar(50),
address varchar(50),
PRIMARY KEY (id)
);
create table basic3(
id int AUTO_INCREMENT,
name varchar(50),
address varchar(50),
PRIMARY KEY (id)
);
create table basic4(
id int AUTO_INCREMENT,
name varchar(50),
address varchar(50),
PRIMARY KEY (id)
);
this is the new_person store procedure
drop procedure if exists new_person;
DELIMITER //
CREATE PROCEDURE new_person
select (id, name,address)
from basic2;
BEGIN
START TRANSACTION;
INSERT INTO basic3 (id,name,address)
VALUES(LAST_INSERT_ID(),bname,baddress);
INSERT INTO basic4 (id,name,address)
VALUES(LAST_INSERT_ID(),bname,baddress);
COMMIT;
END//
DELIMITER;
We can do it by two way one for using cursor and another is using SELECT with insert i thing for you SELECT is better
Like this
INSERT INTO basic3 (name,address)
SELECT name, address FROM basic2;
I tried to create a new Database and some Tables and executed Query successfully, but my tables are not visible in Object Explorer, but my Database is created. Refreshed the Object Explorer as well!
Could you please help me sort this out?
Create DATABASE CustomerOrders
GO
USE CustomerOrders;
GO
CREATE TABLE dbo.Customerdetails
(
Customer_ID int IDENTITY(1,1) PRIMARY KEY,
Name varchar(50) NOT NULL,
PhoneNumber int NOT NULL,
Address nvarchar(100)
);
GO
INSERT dbo.Customerdetails
VALUES ('ABC', +91-123456789, 'SF'), ('DEF', +1-123432145, 'California');
GO
CREATE TABLE dbo.Productdetails
(
Product_ID int IDENTITY(2015160,1) PRIMARY KEY,
Product_Name nvarchar(100) NOT NULL,
UnitPrice money NOT NULL,
ISSN_No varchar(100) NOT NULL
);
GO
INSERT dbo.Productdetails
VALUES ('QWERR', 15000, 'ASD123//456'), ('ERRT', 45000, 'APP4352223//234');
GO
CREATE TABLE dbo.Orderdetails
(
Order_ID int IDENTITY(00001,1) PRIMARY KEY,
Cust_ID int NOT NULL,
Prod_ID int NOT NULL,
Quantity int NOT NULL,
ProductName nvarchar(100) NOT NULL,
CONSTRAINT FK_Customer_ID FOREIGN KEY (Cust_ID)
REFERENCES CustomerDetails(Customer_ID),
CONSTRAINT FK_Product_ID FOREIGN KEY (Prod_ID)
REFERENCES ProductDetails(Product_ID)
);
GO
INSERT dbo.Productdetails
VALUES ('Sony_Xperia', 5, 'SWER2525//35'), ('Apple-I series', 2, 'SWER2525//35');
GO
i tried out in my machine.but its working fine.its shows all tables in object explorer.
Just disconnect server and connect again and check it out.