MySQL creationg table error with Positional message - mysql

Hello I am new to MySQL and having trouble creating a table. It says that the types im using are not valid at their position with ')'.
Can someone tell me what is wrong with this code snippet?
create table CUSTOMER(
CustomerID int AUTO_INCREMENT,
LastName varchar,
FirstName varchar,
Address varchar,
City varchar,
State varchar,
ZIP number,
Phone number,
EmailAddress varchar,
constraint pk_cID primary key(CustomerID));

Your table should look like:
create table CUSTOMER(
CustomerID int not null AUTO_INCREMENT,
LastName varchar(100),
FirstName varchar(100),
Address varchar(100),
City varchar(100),
State varchar(100),
ZIP int,
Phone varchar(50),
EmailAddress varchar(100),
primary key(CustomerID)
);
number is not a datatype in MySQL
When you assign auto_increment it should be not null auto_increment
You don't need a constraint for primary key, just add primary key.
I don't think the best way of storing Phone numbers is int, you might have different formatting the best way is to treat numbers as addresses , so varchar would be better

Related

Trying to change the name of a foreign key column

I have these two tables:
CREATE TABLE Collaborators (
CustomerID INT NOT NULL,
FirstName VARCHAR(25),
LastName VARCHAR(25),
Street VARCHAR(50),
City VARCHAR(50),
State VARCHAR(25),
ZipCode INT,
Telephone VARCHAR(15),
PRIMARY KEY(CustomerID));
CREATE TABLE Orders (
OrderID INT NOT NULL,
CustomerID INT NOT NULL,
SKU VARCHAR(20),
Description VARCHAR(50),
PRIMARY KEY(OrderID),
FOREIGN KEY(CustomerID) REFERENCES Customers(CustomerID));
My goal is to change any instance of "Customer" to "Collaborator". I tried to go one at a time and use an alter table statement.
ALTER TABLE Collaborators CHANGE CustomerID CollaboratorID INT;
ALTER TABLE Orders CHANGE CustomerID CollaboratorID INT;
Here is the error code MySQL spits out at me:
ERROR 1025 (HY000): Error on rename of './QuantigrationUpdates/#sql-668_24' to './QuantigrationUpdates/Collaborators' (errno: 150)
Any help would be greatly appreciated. I've figured that I can't simply change the column names because they have key constraints, but I don't know how to work around that. Thanks.
Update: Am I able to use CREATE VIEW to work around the key constraints?

How to make the primary key as varchar

Hi guys so i dont know how to make it as varchar because is want to input BSIT as my primary key and i cant figure it out how to make it as characters please help me this is my code btw
create table CourseTBL
(
CourseID int primary key,
Course varchar(50),
Description varchar(50),
)
It's better to create a different CODE column to use WHERE condition on it, because key is something meaningless and is intended for referential integrity only. For PK add a constraint as an another element of CREATE TABLE statement. Like this:
create table CourseTBL
(
CourseID varchar(50),
Course varchar(50),
Description varchar(50),
primary key( CourseID )
)
My preference is to add course code column
Create table CourseTBL
(
CourseID int primary key,
CourseName varchar(50),
CourseCode varchar(10),
CourseDescription varchar(50),
)

Is GUID a good reference key for addresses in this case?

I am currently working at a construction management software and I stepped into a problem (described below) when designing the schema for my database which contains four tables, for now, i.e employee, address, user and license.
The tables that created the confusion are employee and address; code provided down below.
In order to maintain the Physical integrity of my data, I did not store anything lexically derived from address, such as Address1, Address2, City, State, County etc in the table employee; since I subjectively considered that it would be better for attributes that do not uniquely identify a specific employee to make part of another table. Now my question arises:
Is it a good choice to use a GUID as the primary key for the address table?
If yes, is there any possibility that it may be a factor which prevents fast querying?
The reason why I have chosen to use a GUID as PK-indexing is that I was left with no options. EmployeeId from address does not offer me a solution since I can not have a field being PK as well as FK: see this.
CREATE TABLE employee(
employeeId INT
NOT NULL
CHECK(employeeId > 0),
firstname VARCHAR(20)
NOT NULL,
lastname VARCHAR(20)
NOT NULL,
sex VARCHAR(1)
NOT NULL,
birthdate DATE
NOT NULL,
addressId VARCHAR(30),
PRIMARY KEY(employeeId)
);
CREATE TABLE address(
addressId VARCHAR(30)
NOT NULL,
employeeId INT,
Address1 VARCHAR(120)
NOT NULL,
Address2 VARCHAR(120),
Address3 VARCHAR(120),
City VARCHAR(100)
NOT NULL,
State CHAR(2)
NOT NULL,
Country CHAR(2)
NOT NULL,
PostalCode VARCHAR(16)
NOT NULL,
PRIMARY KEY(addressId),
FOREIGN KEY(employeeId) REFERENCES employee(employeeId) ON DELETE SET NULL
);
ALTER TABLE employee
ADD FOREIGN KEY(addressId)
REFERENCES address(addressId)
ON DELETE SET NULL;
I would love to know if there are any other ways to create an appropriate relationship between employee and address without using GUID. Another way would be a specified (int) value, but in that case a disadvantage would be:
Introducing by mistake a FK != PK which will lead to a poor relationship between the tables.
EDIT:
Some of you suggested in the comments to change "UUID" indexing to AUTO_INCREMENT but the problem for me arises when I have to insert employees from my WPF-application.
The PK addressId from address will keep increasing on its own. What am I supposed to pass into the FK then to keep the relationship tight and correct then?
Should I create a variable of type int, let's say var i = 0 and every time I insert one employee -> increase that variable by one -> assign it to the FK or?
You'll want something like this in order to do a many-to-many association between employees and addresses (Address types will be like home, work, billing, travel, etc.). You can then create other entity tables to track associations between those entities and addresses as well. In the table employee_address, those columns will be foreign keys back to their relative tables.
As for using GUIDs vs INT for primary keys, numeric values read faster than string values on a JOIN. Depending on how large your data gets, you may need to switch from INT to BIGINT years down the road.
Also, give these people some space to enter their names. 20 characters is way too short, especially for last names.
employee
--------
employee_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(255) NOT NULL,
lastname VARCAHR(255) NOT NULL,
gender BIT NOT NULL,
birthdate DATE NOT NULL
address
---------
address_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
address1 VARCHAR(255),
address2 VARCHAR(255),
address3 VARCHAR(255),
city VARCHAR(255),
state CHAR(2),
country CHAR(2)
address_type
--------------
address_type_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
address_type VARCHAR(255)
employee_address
-----------------
employee_address_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
employee_id INT,
address_id INT,
address_type_id INT

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

mysql foreign key declaration confusion

I have two tables
CREATE TABLE members(
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(50),
password char(128),
salt char(128),
status VARCHAR(20),
profile VARCHAR(15),
unlock_code INT,
username VARCHAR(20),
privilege VARCHAR(15)
);
CREATE TABLE member_details(
detail_id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50),
middle_name VARCHAR(50),
last_name VARCHAR(50),
contact VARCHAR(12),
dob VARCHAR(10),
nic VARCHAR(15),
mobile VARCHAR(12),
userid INT,
FOREIGN KEY (userid) REFERENCES members(id)
);
How the thing is that when I DESCRIBE TABLE it shows MUL.
Engine is InnoDB.
Also, is it okay to not declare foreign keys and just use JOINS in query and make it act like foreign key?
Keep the database without constraints! Make the columns 'act' like foreign keys but don't but constraints.
Make sure you have sufficient verification in server side scripting to handle those constraints there.