yetin
yetin

Reputation: 61

SQL Query Primary Key constraint

I am a newbie in sql. Can someone please enlighten me on the following sql query. My problem lies in staffID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID)

$sql   =   "CREATE   TABLE   Persons(staffID   int   NOT   NULL 
AUTO_INCREMENT,
PRIMARY  KEY(personID),FirstName  varchar(15),LastName  varchar(15),Age 
int)";

Thank you. Regards.

Upvotes: 0

Views: 42

Answers (2)

Hitesh Mundra
Hitesh Mundra

Reputation: 1588

Auto_increment column should be int type & primary key. but your sql statement is wrong where staff_id is not primary key. You can set staffid to primary key and person to unique key

$sql   =   "CREATE   TABLE   Persons(staffId int primary key AUTO_INCREMENT
, personid int unique key not null , FirstName  varchar(15),LastName  varchar(15), Age int)";

Upvotes: 1

aksappy
aksappy

Reputation: 3400

You are trying to create a primary key of an invalid column, as well as omitting an auto increment column. Please try with the below SQL. See this documentation from MySQL

CREATE TABLE Persons(staffId INT AUTO_INCREMENT, 
     personId VARCHAR(10),
     firstName VARCHAR(15), 
     LastName VARCHAR(15),
     Age INT, 
     primary key (staffId,personId));

Upvotes: 1

Related Questions