如果在创建新表时不包括VARCHAR的大小,MySQL是否可以正常工作?
不,查询将无法进行。让我们创建相同的场景并检查错误-
mysql> create table DemoTable1821 ( Id int, 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
要消除上述错误,您需要提供varchar(10)的大小。让我们首先创建一个表-
mysql> create table DemoTable1821 ( Id int, FirstName varchar(10), LastName varchar(10) );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable1821 values(1,'John','Smith'); mysql> insert into DemoTable1821 values(2,'John','Doe'); mysql> insert into DemoTable1821 values(3,'Adam','Smith');
使用select语句显示表中的所有记录-
mysql> select * from DemoTable1821;
这将产生以下输出-
+------+-----------+----------+ | Id | FirstName | LastName | +------+-----------+----------+ | 1 | John | Smith | | 2 | John | Doe| | 3 | Adam | Smith | +------+-----------+----------+ 3 rows in set (0.00 sec)