Creating table in mysql but the error keeps poping up , what am i doing wrong? - create-table

![CREATE TABLE ELEB(
ccode integer primary key,
cname varchar(20),
add varchar(25),
oldmr integer,
newmr integer,
unit integer,
tbill integer);][1]

CREATE TABLE ELEB
(
ccode int primary key,
cname varchar(20),
add varchar(25),
oldmr int,
newmr int,
unit int,
tbill int
)

Related

How can I make a table for operations?

ER Diagram
I attached the image of the ER diagram and here's the SQL code that I came up with. The problem is I don't know if it's right and I don't know how to implement the operations table.
CREATE TABLE catagory(
catagoryId INT,
Name VARCHAR(20),
Brakes VARCHAR(20),
PRIMARY KEY(catagoryId)
);
CREATE TABLE AutoParts(
PartId INT,
PartName VARCHAR(20),
Description VARCHAR(20),
Price FLOAT(10),
PRIMARY KEY(PartId)
);
CREATE TABLE Customer(
Id INT,
Name VARCHAR(20),
Address VARCHAR(50),
Phone INT,
PRIMARY KEY(Id)
);
CREATE TABLE Employee(
Id INT,
Name VARCHAR(20),
Email VARCHAR(20),
Phone INT,
PRIMARY KEY(Id)
);
CREATE TABLE sub-catagory(
Name VARCHAR(20),
Type VARCHAR(20),
PRIMARY EKY(Type)
);

how to output a parameter using a stored procedure in mysql?

I have two tables say
table1:
create table Person(
Id int primary key auto_increment,
name varchar(20),
Gender char(1)
);
table2:
create table Details(
Age int,
Phone int(10) primary key,
Address varchar(100)
Id foreign key(Id) references Person(Id),
Name foreign key(Name) references Person(Name)
);
i will have to create a stored procedure to insert data into table:'Details'
create procedure usp_insert(
IN Name varchar(100),
IN Age int,
IN Phone int(10),
IN Address varchar(100),
OUT Id int
)
begin
//want to output the Id as QW001 on inserting the data into the Details table.
insert into Details(Name,Age,Phone,Address) values(name,age,phone,address)
end
How can i achieve the Id in the following format 'QW001' as output parameter.
Can someone help me out in correcting the above stored procedure,since i'm new to this. Any help is appreciated.
TYIA!
you need to use LAST_INSERT_ID() like this :
create procedure usp_insert(
IN Name varchar(100),
IN Age int,
IN Phone int(10),
IN Address varchar(100),
OUT Id int
)
begin
insert into Details(Name,Age,Phone,Address) values(name,age,phone,address);
set Id =CONCAT("QW00", LAST_INSERT_ID()) AS ConcatenatedString;
end

Why can't I add a constraint when creating the table?

CREATE TABLE `CHARSET`
(
CHARSET_CODE varchar(15),
CHARSET_DESCRIPTION varchar(50),
APP_REGI_DATE date,
APP_UPD_DATE date,
CREATED_BY varchar(20),
CREATION_DATE date,
UPDATED_BY varchar(20),
UPDATE_DATE date,
LOCALE_CODE varchar(20),
ADD constraint PK_80 PRIMARY KEY (CHARSET_CODE)
) ;
Giving me a red pointer on the last line where i am trying to add constraint.
CREATE TABLE CHARSET
(
CHARSET_CODE varchar(15),
CHARSET_DESCRIPTION varchar(50),
APP_REGI_DATE date,
APP_UPD_DATE date,
CREATED_BY varchar(20),
CREATION_DATE date,
UPDATED_BY varchar(20),
UPDATE_DATE date,
LOCALE_CODE varchar(20),
CONSTRAINT PK_80 PRIMARY KEY (CHARSET_CODE)
) ;
Remove ADD in your query and run it. It will work.
Try this code. You should remove the ADD keyword used before the constraint.
CREATE TABLE CHARSET
( CHARSET_CODE varchar(15),
CHARSET_DESCRIPTION varchar(50),
APP_REGI_DATE date,
APP_UPD_DATE date,
CREATED_BY varchar(20),
CREATION_DATE date,
UPDATED_BY varchar(20),
UPDATE_DATE date,
LOCALE_CODE varchar(20),
constraint PK_80 PRIMARY KEY (CHARSET_CODE));
Remove the ADD before contstraint, that should do the trick.
CREATE TABLE `CHARSET` (
CHARSET_CODE varchar(15),
CHARSET_DESCRIPTION varchar(50),
APP_REGI_DATE date,
APP_UPD_DATE date,
CREATED_BY varchar(20),
CREATION_DATE date,
UPDATED_BY varchar(20),
UPDATE_DATE date,
LOCALE_CODE varchar(20),
CONSTRAINT PK_80 PRIMARY KEY (CHARSET_CODE)
);

Error Code: 1215 in mysql cannot add foreign key constraint

I am getting an issue and I feel like I fixed everything. MySQL workbench is still giving me this error. If you can please explain what is wrong I would be greatly appreciate it. I did check for key constraints in the Account table. It is giving the error in the account table.
CREATE TABLE Account(
AcctNum int AUTO_INCREMENT,
MemberID int,
Balance double,
PIN int,
creationDate date,
InitialBalance double,
CreatedByEmployee int,
type VARCHAR(20),
PRIMARY KEY(AcctNum),
FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);
CREATE TABLE Member(
MemNum int AUTO_INCREMENT,
DOB date,
CreditScore int,
AcctOpened date,
SSN VARCHAR(11),
Address VARCHAR(255),
PRIMARY KEY(MemNum)
);
CREATE TABLE Employee(
EmpId int AUTO_INCREMENT,
DOB date,
SSN VARCHAR(11),
HireDate date,
Salary double,
EmpLevel VARCHAR(50),
PRIMARY KEY(EmpId)
);
You need to create the referencing table first before creating the table which is referring other tables with FOREIGN key.
The order of table creation should be as
CREATE TABLE Member(
MemNum int AUTO_INCREMENT,
DOB date,
CreditScore int,
AcctOpened date,
SSN VARCHAR(11),
Address VARCHAR(255),
PRIMARY KEY(MemNum)
);
CREATE TABLE Employee(
EmpId int AUTO_INCREMENT,
DOB date,
SSN VARCHAR(11),
HireDate date,
Salary double,
EmpLevel VARCHAR(50),
PRIMARY KEY(EmpId)
);
CREATE TABLE Account(
AcctNum int AUTO_INCREMENT,
MemberID int,
Balance double,
PIN int,
creationDate date,
InitialBalance double,
CreatedByEmployee int,
type VARCHAR(20),
PRIMARY KEY(AcctNum),
FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);
You are creating the Foreign key for Member before creating the table so it does not exist yet. Change the order you are creating the tables.
CREATE TABLE Member(
MemNum int AUTO_INCREMENT,
DOB date,
CreditScore int,
AcctOpened date,
SSN VARCHAR(11),
Address VARCHAR(255),
PRIMARY KEY(MemNum)
);
CREATE TABLE Employee(
EmpId int AUTO_INCREMENT,
DOB date,
SSN VARCHAR(11),
HireDate date,
Salary double,
EmpLevel VARCHAR(50),
PRIMARY KEY(EmpId)
);
CREATE TABLE Account(
AcctNum int AUTO_INCREMENT,
MemberID int,
Balance double,
PIN int,
creationDate date,
InitialBalance double,
CreatedByEmployee int,
type VARCHAR(20),
PRIMARY KEY(AcctNum),
FOREIGN KEY(MemberID) REFERENCES Member(MemNum),
FOREIGN KEY(CreatedByEmployee) REFERENCES Employee(EmpId)
);
Ofcourse the order of table creation is important here.
Create the Member and Employee tables first, followed by the Account. It should work fine.
you have wrong order of creating tables
try this
1- Create member table first
2- employer table second
3- account table in last
http://www.sqlfiddle.com/#!2/47d58

Creating a table, SQL. Basic stuff

So I'm having a small problem with creating a table. The problem is in my attempt to create a foreign key to another table. I'm currently using MYSQL2008 Management R2 express, so no designer. Heres my two tables
use teckDB;
CREATE TABLE inventory
(
primId int NOT NULL PRIMARY KEY,
prodName VarChar(255),
quantity int,
prodCost MONEY,
prodDesc VARCHAR(255)
);
CREATE TABLE orderTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT NOT NULL,
created date,
prodId INT,
);
Those two tables ran without a problem. When create the thrid one however causes this error message.
Msg 1769, Level 16, State 1, Line 3 Foreign key 'orderTB' references
invalid column 'orderTB' in referencing table 'CustomerTB'. Msg 1750,
Level 16, State 0, Line 3 Could not create constraint. See previous
errors.
On the thrid table of....
CREATE TABLE CustomerTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT, FOREIGN KEY (orderTB) REFERENCES orderTB(orderId),
fName VARCHAR(50),
lName VARCHAR(50),
addLN1 VARCHAR(255),
addLN2 VARCHAR(255),
addCity VARCHAR(255),
addPro VARCHAR(255),
addPST VARCHAR(7)
);
try this
FOREIGN KEY (iparent_id) REFERENCES innodb_parent (iparent_id)
I think this should help to solve your query
http://blog.sqlauthority.com/2008/09/08/sql-server-%E2%80%93-2008-creating-primary-key-foreign-key-and-default-constraint/
You have an extra comma after "orderId INT", and you have got the foreign key syntax wrong.
This should work:
CREATE TABLE CustomerTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT REFERENCES orderTB(orderId),
fName VARCHAR(50),
lName VARCHAR(50),
addLN1 VARCHAR(255),
addLN2 VARCHAR(255),
addCity VARCHAR(255),
addPro VARCHAR(255),
addPST VARCHAR(7)
);
tested on SQLFiddle here