Account Number Auto generation - mysql

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

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]
);

Creating a column in MySql with id such as CHV18000002

i could do this in Microsoft sql to create a column incrementing such as CHV180000001, CHV180000002 but trying to do that in MySql. I have tried but getting error: incorrect syntax. Any guide to achieve this: This is my code:
CREATE TABLE Candidates (ID INT AUTO_INCREMENT NOT NULL Primary Key,
[ApplicationID] AS ('CHV18'+right('000000'+CONVERT([varchar](6),[ID]),(6))),
[FirstName] [varchar](100) NOT NULL,
[MiddleName] [varchar](100) NOT NULL,
[LastName] [varchar](100) NOT NULL,
[DateOfBirth] [date] NOT NULL,
[Gender] [nchar](1) NOT NULL
Try this to create the table
CREATE TABLE Candidates (ID INT(11) AUTO_INCREMENT NOT NULL Primary Key,
ApplicationID varchar(6),
FirstName varchar(100) NOT NULL,
MiddleName varchar(100) NOT NULL,
LastName varchar(100) NOT NULL,
DateOfBirth date NOT NULL,
Gender varchar(1) NOT NULL);

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;

Creating tables with foreign keys

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));

Should I create a parent table for shared attributes?

I am designing the database (MySQL) in which I have two tables Employees and Guests as following :
CREATE TABLE employee (
`EMP_ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`FIRST_NAME` VARCHAR(8) NOT NULL,
`MID_NAME` VARCHAR(11),
`LAST_NAME` VARCHAR(8) NOT NULL,
`BIRTHDAY` DATE,
`COUNTRY_ID` INT,
`NAT_ID` VARCHAR(8) NOT NULL,
`ID_EXP_DATE` DATE,
`ID_TYPE` VARCHAR(8) NOT NULL,
`Mobile` VARCHAR(8) NOT NULL,
`PHONE` VARCHAR(8) NOT NULL,
`EMAIL` VARCHAR(27) NOT NULL,
`DEPT_ID` TINYINT NOT NULL references DEPARTMENT (ID),
`POSITION` VARCHAR(20),
`EMP_TYPE` TINYINT NOT NULL references EMP_TYPES (type),
`JOINDATE` DATE,
`SALARY` MEDIUMINT DEFAULT 0 ,
`WORKEMAIL` VARCHAR(30),
`MARITALSTAT` VARCHAR(7),
`EMERGCONTACT` VARCHAR(22),
`EMERG_CONT_PHN` VARCHAR(11),
`GENDER` VARCHAR(6),
`RESUMEURL` VARCHAR(60),
`RELIGION` VARCHAR(11),
PRIMARY KEY (`EMP_ID`));
CREATE TABLE Guest (
`guest_ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`FIRST_NAME` VARCHAR(8) NOT NULL,
`MID_NAME` VARCHAR(11),
`LAST_NAME` VARCHAR(8) NOT NULL,
`BIRTHDAY` DATE,
`COUNTRY_ID` INT,
`NAT_ID` VARCHAR(8) NOT NULL,
`ID_EXP_DATE` DATE,
`ID_TYPE` VARCHAR(8) NOT NULL,
`Mobile` VARCHAR(8) NOT NULL,
`PHONE` VARCHAR(8) NOT NULL,
`EMAIL` VARCHAR(27) NOT NULL,
`WORKEMAIL` VARCHAR(30),
`MARITALSTAT` VARCHAR(7),
`EMERGCONTACT` VARCHAR(22),
`EMERG_CONT_PHN` VARCHAR(11),
`GENDER` VARCHAR(6),
`RELIGION` VARCHAR(11),
..................................// More attributes specific for guest table
PRIMARY KEY (`Guest_ID`));
Since both of the tables have auto generated primary keys and it would not be appropriate if they share primary key from person. would it be a good idea to create a table named person with all the common attributes and two child tables as Employee and Guest?
and what would be the best practice to implement this?
Thank You,
There are some things to consider:
Can an Employee be a Guest or vice versa?
How to compare them?
If this plays a role for your application, i´d consider a ParentTable, because so you can easily figure out, if an Employee is also a Guest or not. So you can easily compare them.
On the other hand, you could do a 3rd Option:
A Table called Personal Information.
An Employee has an Reference to this, Guests also.
If an Employee and an Guest both refeer to the same entry, you know they are the same person.
Schema:
yes the schema is like:
Table:
PersonalInformation
ID (Primary Key, auto inc)
And put all your information here
Table Employee
ID (Primary Key, auto inc int)
PersonalInformation_ID (ForeignKey to PersonalInformation.ID)
Add your Employee specific columns here
Table Guest
ID (Primary Key, auto inc int)
PersonalInformation_ID (ForeignKey to PersonalInformation.ID)
Add your Guest specific columns here
Hope this helps you implement it, otherwise ask again.