Resolve ERROR 1064 (42000) that occurred after using varchar (without providing the size)

MySQLMySQLi Database

Let us first see when this situation can arise. Create a table and set column name with datatype but without the size −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar,
   LastName varchar
);
ERROR 1064 (42000): 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 ',
LastName varchar
)' at line 4

You can correct the above error by providing the size for varchar data type like varchar(100). The same will fix the issue.

Let’s fix it and at first create a table −

mysql> create table DemoTable
(
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   FirstName varchar(100),
   LastName varchar(100)
);
Query OK, 0 rows affected (0.60 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable(FirstName,LastName) values('Adam','Smith');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable(FirstName,LastName) values('John','Doe');
Query OK, 1 row affected (0.38 sec)
mysql> insert into DemoTable(FirstName,LastName) values('Chris','Brown');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+-----------+----------+
| Id | FirstName | LastName |
+----+-----------+----------+
|  1 | Adam      | Smith    |
|  2 | John      | Doe      |
|  3 | Chris     | Brown    |
+----+-----------+----------+
3 rows in set (0.00 sec)
raja
Published on 25-Sep-2019 12:15:06
Advertisements