简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM