mysql5.5中表的结构
表名:
id:
name:
detail:
这里甚至改变了表格:
ALTER TABLE tablename MODIFY name VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL;
OR
ALTER TABLE `tablename` CHANGE `name` `name` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL DEFAULT NOT NULL;
插入值后:
INSERT INTO `databasename`.`tablename` (`id`, `name`, `detail`) VALUES (NULL, '', 'asdfasfdadsfadsfafd');
查询已成功运行,没有向表中添加错误行,
我怎样才能防止这种空或空。
问候
答案 0 :(得分:0)
试试这个:
ALTER TABLE tablename
MODIFY COLUMN name VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL CHECK (name <> '');
样本:
mysql> create table tablename(id int(2) not null, something varchar(25) null, primary key(id));
Query OK, 0 rows affected (0.15 sec)
mysql> insert into tablename values(0,'hello');
Query OK, 1 row affected (0.38 sec)
mysql> insert into tablename values(1,'salut');
Query OK, 1 row affected (0.31 sec)
mysql> select * from tablename;
+----+-----------+
| id | something |
+----+-----------+
| 0 | hello |
| 1 | salut |
+----+-----------+
2 rows in set (0.00 sec)
现在,我运行UPDATE
命令:
mysql> ALTER TABLE tablename MODIFY COLUMN something VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_persian_ci NOT NULL;
Query OK, 2 rows affected (0.70 sec)
Records: 2 Duplicates: 0 Warnings: 0
我插入普通行:
mysql> insert into tablename values(2,'france');
Query OK, 1 row affected (0.29 sec)
但它不允许我插入NULL值:
mysql> insert into tablename values(3,NULL);
ERROR 1048 (23000): Column 'something' cannot be null
我检查确定:
mysql> select * from tablename;
+----+-----------+
| id | something |
+----+-----------+
| 0 | hello |
| 1 | salut |
| 2 | france |
+----+-----------+
3 rows in set (0.00 sec)