CHECK子句会被分析,但是会被忽略。请参见13.1.5节,“CREATE TABLE语法”。接受这些子句但又忽略子句的原因是为了提高兼容性,以便更容易地从其它SQL服务器中导入代码,并运行应用程序,创建带参考数据的表。请参见1.8.5节,“MySQL与标准SQL的差别”。
可以用enum或者set实现相同的效果:
//1.set的效果
mysql> create table test(
-> id bigint primary key auto_increment,
-> paymethod set('货到付款','支付宝付款')
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test(paymethod) values('货到付款');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test(paymethod) values('货到付');
ERROR 1265 (01000): Data truncated for column 'paymethod' at row 1
在java程序中可以读取相关信息:
package com.xie.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Test {
public static void main(String[] args){
try {
Class.forName("com.mysql.jdbc.Driver");
Connection ct=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/shop","root","centre");
PreparedStatement ps=ct.prepareStatement("select * from test");
ResultSet rs=ps.executeQuery();
while (rs.next()) {
System.out.println(rs.getLong(1));
System.out.println(rs.getString(2));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}运行结果:
1
货到付款
//2.enum的效果:
mysql> create table test(
-> id bigint primary key auto_increment,
-> paymethod enum('货到付款','支付宝付款')
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test(paymethod) values('货到付款');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test(paymethod) values('货到付');
ERROR 1265 (01000): Data truncated for column 'paymethod' at row 1
测试java代码如上:
测试结果:
1
货到付款