Creating tables with foreign keys - mysql

I am creating tables where I have foreign keys. This is part of the statements. For some reason I cannot get it to work.
What am i doing wrong?
CREATE TABLE Doctor (
NPI NUMBER PRIMARY KEY NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
PHONE NUMBER NOT NULL,
ADDRESS VARCHAR(20) NOT NULL
CITY VARCHAR(20) NOT NULL );
CREATE TABLE Patient (
SSN NUMBER PRIMARY KEY NOT NULL,
INSURANCE_POLICY_ID NUMBER NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
DOB DATE NOT NULL,
PHONE NUMBER NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID) REFERENCES INSURANCE (INSURANCE_POLICY_ID));

You have tagged your question with SQL Server and MySql both, in both of these RDBMS there is no data type called Number but there is a data type to store numbers it is called INT or INTEGER.
Therefore your table definitions should be as follow:
CREATE TABLE Doctor (
NPI INT NOT NULL PRIMARY KEY ,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
PHONE INT NOT NULL, --<-- should be a varchar since most phone numbers have a leading zero
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL );
CREATE TABLE Patient (
SSN INT NOT NULL PRIMARY KEY,
INSURANCE_POLICY_ID INT NOT NULL,
LNAME VARCHAR(20) NOT NULL,
FNAME VARCHAR(20) NOT NULL,
DOB DATE NOT NULL,
PHONE INT NOT NULL,
ADDRESS VARCHAR(20) NOT NULL,
CITY VARCHAR(20) NOT NULL,
FOREIGN KEY (INSURANCE_POLICY_ID)
REFERENCES INSURANCE (INSURANCE_POLICY_ID));

Related

")" is not valid at this position, expecting an identifier CREATE TABLE

I'm keep getting this error ")" is not valid at this position, expecting an identifier
although I had created table similar way but getting this error for any new table I'm trying to create.
CREATE TABLE IF NOT EXISTS company.employee(
fname VARCHAR(15) NOT NULL,
mname CHAR NULL,
laname VARCHAR(15) NOT NULL,
ssn CHAR(9) NOT NULL,
Bdate DATE,
Adress VARCHAR(35) NOT NULL,
Sex CHAR NULL,
Salary DECIMAL (10,2) NULL,
Supper_ssn CHAR(9) NULL,
Dno INT NOT NULL,
PRIMARY KEY(ssn)
);
CREATE TABLE IF NOT EXISTS company.Department(
Dname VARCHAR(15) NOT NULL,
Dnumber INT NOT NULL,
Mgr_ssn CHAR(9) NOT NULL,
Mgr_start_date DATE NULL,
PRIMARY KEY (Dnumber)
);
USE compnay;
CREATE TABLE IF NOT EXISTS projects(
Pname VARCHAR(15) NOT NULL,
Pnumber INT NOT NULL,
Plocation VARCHAR(15),
Dnum INT NOT NULL,
PRIMARY KEY(Pnumber),
UNIQUE(Pname)
);
CREATE TABLE IF NOT EXISTS Dept_location(
Dnumber INT NOT NULL,
Dlocation VARCHAR(15) NOT NULL,
PRIMARY KEY(Dnumber,Dlocation)
);
CREATE TABLE IF NOT EXISTS Works_on(
Essn CHAR(9) NOT NULL,[![enter image description here][1]][1]
);

I cannot create the database GradeBook with the foreign key. When I try to add it I get error1046: choose database

CREATE SCHEMA GRADEBOOK;
CREATE TABLE GRADEBOOK.PERSON
(PERSON_ID INT NOT NULL,
Fname varchar(15) NOT NULL,
Minit varchar (1),
Lname varchar(15) NOT NULL,
B_date date NOT NULL,
SEX varchar(1) NOT NULL,
ADDRESS varchar(50) NOT NULL,
TELEPHONE VARCHAR(12),
STATUS varchar(10) NOT NULL,
PRIMARY KEY (PERSON_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.STUDENT
(STUDENT_ID INT NOT NULL,
SUBJECT_ID VARCHAR (30) NOT NULL,
PRIMARY KEY (STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.ALUMNUS
(STUDENT_ID INT NOT NULL,
GRAD_YEAR date NOT NULL,
PRIMARY KEY(STUDENT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.SUBJECT
(SUBJECT_ID VARCHAR (30) NOT NULL,
SUBJECT_NAME VARCHAR (20) NOT NULL,
CLASS_ID VARCHAR (20) NOT NULL,
PRIMARY KEY (SUBJECT_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOUSE
(HOUSE_ID INT NOT NULL,
HOUSE_NAME VARCHAR (20) NOT NULL,
HOUSE_COLOR VARCHAR (20) NOT NULL,
PRIMARY KEY (HOUSE_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLUB
(CLUB_ID INT auto_increment NOT NULL,
CLUB_name varchar(20) NOT NULL,
PRIMARY KEY (CLUB_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.HOMEROOM
(HOMEROOM_ID VARCHAR(20) NOT NULL,
CLASS_ID VARCHAR(20) NOT NULL,
PRIMARY KEY( HOMEROOM_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.CLASS
(CLASS_ID VARCHAR(20) NOT NULL,
CLASS_name VARCHAR(20) NOT NULL,
HOMEROOM_ID VARCHAR(20) NOT NULL,
PRIMARY KEY (CLASS_ID))
ENGINE=INNODB;
CREATE TABLE GRADEBOOK.TEACHER
(TEACHER_ID INT NOT NULL,
HOOMROOM VARCHAR(20) NOT NULL,
SUBJECT_ID VARCHAR(20) NOT NULL,
YEAR INT NOT NULL,
TERM VARCHAR(20) NOT NULL,
PRIMARY KEY(TEACHER_ID))
ENGINE=INNODB;

Foreign Keys in MySQL Workbench not working?

I am reverse engineering a database in MySQL Workbench but it doesn't seem to be importing or recognising the foreign keys. Which means I cant get it to draw the relationships.
While I am trying to get this to work I am using a snippet of a couple of tables, so its nothing complex.
Here are the 2 demo tables that I am trying to get to work:
CREATE TABLE users (
UserID varchar(32) NOT NULL,
OrgID varchar(6) NOT NULL,
RegionID int NULL,
LocationID int NULL,
Name nvarchar(60) NULL,
Crypt varchar(32) NULL,
Security int NULL,
Status int NULL,
PRIMARY KEY ( UserID ),
CONSTRAINT fk_OrgID_Users FOREIGN KEY (OrgID) REFERENCES Organisations(OrgID)
);
CREATE TABLE Organisations(
OrgID varchar(6) NOT NULL,
Name nvarchar(70) NOT NULL,
Address1 varchar(50) NULL,
Address2 varchar(50) NULL,
Address3 varchar(50) NULL,
Town varchar(20) NULL,
County varchar(20) NULL,
PostCode varchar(10) NULL,
NotificationEmail varchar(500) NOT NULL,
PRIMARY KEY ( OrgID )
)
But in MySQL Workbench, there are no relationships or foreign keys being created. If I look at the table data, and the foreign keys tab, theres nothing there?
I think I'm creating the foreign keys correctly, so is there a reason its not importing them?
On further research there were several things that I needed to do for this to work.
I had to make sure that the tables reverse engineered from MySQL were defined as InnoDB.
And I also had to remove the foreign key constraint from the create table command and create the foreign keys after the tables were created, like so:
CREATE TABLE Users (
UserID varchar(32) NOT NULL,
OrgID varchar(6) NOT NULL,
RegionID int NULL,
LocationID int NULL,
Name nvarchar(60) NULL,
Crypt varchar(32) NULL,
Security int NULL,
Status int NULL,
PRIMARY KEY ( UserID )
) ENGINE=InnoDB;
CREATE TABLE Organisations(
OrgID varchar(6) NOT NULL,
Name nvarchar(70) NOT NULL,
Address1 varchar(50) NULL,
Address2 varchar(50) NULL,
Address3 varchar(50) NULL,
Town varchar(20) NULL,
County varchar(20) NULL,
PostCode varchar(10) NULL,
NotificationEmail varchar(500) NOT NULL,
PRIMARY KEY ( OrgID )
) ENGINE=InnoDB;
ALTER TABLE Users ADD CONSTRAINT fk_OrgID_Users FOREIGN KEY (OrgID) REFERENCES Organisations(OrgID);
This then worked fine and the relationships were visible and connected in MySQL Workbench

Duplicate data is inserted with unique index

I have a table called users with 4 unique columns and when I insert data in email it doesn't give me any error and inserts the data even when when the same value already exists in that column.
Here is my database structure:
$user = "CREATE TABLE IF NOT EXISTS users(
id INT UNSIGNED AUTO_INCREMENT,
fb_id BIGINT UNSIGNED NULL,
google_id BIGINT UNSIGNED NULL,
fname VARCHAR(255) NOT NULL,
lname VARCHAR(255) NULL,
email VARCHAR(320) NOT NULL,
username VARCHAR(20) NULL,
password VARCHAR(255) NULL,
access_token TEXT NULL,
type ENUM('facebook','google','site') NOT NULL,
gender ENUM('m','f','o') NULL,
reg_date DATE NOT NULL,
token_expire DATETIME NULL,
PRIMARY KEY(id),
UNIQUE(email,username,fb_id,google_id)
)";
But, when I create my table with following structure:
$user = "CREATE TABLE IF NOT EXISTS users(
id INT UNSIGNED AUTO_INCREMENT,
fb_id BIGINT UNSIGNED NULL UNIQUE,
google_id BIGINT UNSIGNED NULL UNIQUE,
fname VARCHAR(255) NOT NULL,
lname VARCHAR(255) NULL,
email VARCHAR(320) NOT NULL UNIQUE,
username VARCHAR(20) NULL UNIQUE,
password VARCHAR(255) NULL,
access_token TEXT NULL,
type ENUM('facebook','google','site') NOT NULL,
gender ENUM('m','f','o') NULL,
reg_date DATE NOT NULL,
token_expire DATETIME NULL,
PRIMARY KEY(id)
)";
It gives me an error when there is a duplicate entry.
Creating table with any of those methods doesn't give any error. After creating the tables I have verified with phpmyadmin that all those columns have unique index in both methods.
Akash, in the 1st create table, the composite (combination) is unique. If you want them individually to be unique, separate them into ... separate UNIQUE key statements, like in the 2nd.
Let's say the bottom of your first table read this
PRIMARY KEY(id),
UNIQUE KEY(email,username,fb_id,google_id)
Then there is nothing wrong with these two rows existing in the composite index:
'akash#gmail.com','Akash',101,102
and
'akash#gmail.com','Akash2',101,102

Account Number Auto generation

I new to programming. How do I apply auto generation to the accountNo in accounts table. I tried everything. But I don't know how to get this done. Can anyone explain me.
CREATE TABLE accounts(
accountNo int(100) NOT NULL, // I need this to be auto generated.
accountType VARCHAR(100) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(60) NOT NULL,
birthdate DATE NOT NULL,
gender VARCHAR(7),
city VARCHAR(50) NOT NULL,
street VARCHAR(50),
cellPhone VARCHAR(10),
CONSTRAINT PRIMARY KEY(accountNo)
);
Try this one.
CREATE TABLE accounts(
accountNo int(100) NOT NULL AUTO_INCREMENT,
accountType VARCHAR(100) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(60) NOT NULL,
birthdate VARCHAR(20) NOT NULL,
gender VARCHAR(7),
city VARCHAR(50) NOT NULL,
street VARCHAR(50),
cellPhone VARCHAR(10),
CONSTRAINT PRIMARY KEY(accountNo)
);
ALTER TABLE accounts AUTO_INCREMENT = 1001;
When you are adding new data to your accounts table, accountNo will be auto generated.
Make it as an AUTO_INCREMENT:
CREATE TABLE accounts(
accountNo int(100) NOT NULL AUTO_INCREMENT,
accountType VARCHAR(100) NOT NULL,
firstName VARCHAR(50) NOT NULL,
lastName VARCHAR(60) NOT NULL,
birthdate DATE NOT NULL,
gender VARCHAR(7),
city VARCHAR(50) NOT NULL,
street VARCHAR(50),
cellPhone VARCHAR(10),
CONSTRAINT PRIMARY KEY(accountNo)
);
For further informations have a look at this: https://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
declear it AUTO_INCREMENT and you can define PRIMARY KEY like that also. Read manual auto_increment
accountNo INT NOT NULL AUTO_INCREMENT PRIMARY KEY