MySQL will not allow VARCHAR as the primary key?

I am trying to create a table called "Students" with only one field, student name.

student_name must be VARCHAR and the primary key, because I want to uniquely identify students by their name.

CREATE TABLE 'swinters'.'Students' ( 'student_name' VARCHAR NOT NULL, PRIMARY KEY ('student_name') ) ENGINE = InnoDB; 

MYSQL Error Number 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT NULL, PRIMARY KEY (' student_name ')) ENGINE = InnoDB' at line 2.

+4
source share
4 answers

You need to specify VARCHAR(N) , where N is the maximum string length

+8
source

You need to specify the length of the VARCHAR column. those. VARCHAR(255)

+6
source

mySQL loves you, and it tries to encourage you to do it right;)

do not fight correctly. You have two fields.

Use UNIQUE in the name field.

Use a primary key that way. Name it either id or student_id .

You can see a lot more information here:
https://stackoverflow.com/questions/8232943/why-should-i-create-an-id-column-when-i-can-use-others-as-key-fields/8235024#8235024

+3
source

You must indicate how many characters the field will take, e.g. VARCHAR(255)

+2
source

Source: https://habr.com/ru/post/1382766/


All Articles