本文主要是介绍MySQL常用操作(完整性约束条件)---DAY_2,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
自增长AUTO_INCREMENT
USE wxj1;
CREATE TABLE IF NOT EXISTS user3(
id SMALLINT key AUTO_INCREMENT,
username VARCHAR(20)
);
INSERT user3 VALUES(1,'king');
INSERT user3(username) VALUES('kings');
INSERT user3 VALUES(NULL,'kingsS');
INSERT user3 VALUES(DEFAULT,'kingsE');CREATE TABLE IF NOT EXISTS user4(
id SMALLINT key AUTO_INCREMENT,
username VARCHAR(20)
)AUTO_INCREMENT=100;-- change auto_increment
ALTER TABLE user3 AUTO_INCREMENT=100;
-- RECORD FROM ID=100
非空约束 NOT NULL
USE wxj1;
CREATE TABLE IF NOT EXISTS user4(
id int UNSIGNED key auto_increment,
username VARCHAR(20) NOT NULL,
password char(32) NOT NULL,
age TINYINT UNSIGNED
);
INSERT user4 (username,password) VALUES('king','king');
INSERT user4 (username,password,age) VALUES('king1','king1',12);
-- can not be null
INSERT user4 (username,password,age) VALUES(NULL,NULL);
默认值
use wxj1;
CREATE TABLE IF NOT EXISTS user5(
id INT key auto_increment,
username VARCHAR(20) NOT NULL,
password CHAR(20) NOT NULL,
age TINYINT UNSIGNED DEFAULT 18,
addr VARCHAR(50) NOT NULL DEFAULT '北京',
sex enum('man','woman','unknown') NOT NULL DEFAULT 'man'
)ENGINE=INNODB CHARSET=utf8;INSERT user5(username,password) VALUES ('king','king');
INSERT user5 VALUES (3,'kings','kings',DEFAULT,DEFAULT,'unknown')
唯一性
CREATE TABLE IF NOT EXISTS user6(
id TINYINT key auto_increment ,
username VARCHAR(20) UNIQUE NOT NULL,
card char(18) UNIQUE DEFAULT '123456789'
);insert user6(username,card) VALUES('A','111');
insert user6(username,card) VALUES('A1','null');
insert user6(username,card) VALUES('A2',NULL);
阶段小结
creat table review
CREATE TABLE IF NOT EXISTS table_name(
name type [UNSIGNED|ZEROFILL] [NOT NULL] [KEY] [AUTO_INCREMENT(need to connect to key)] [unique]
)ENGINE=engine_name,CHARSET=char_name;
这篇关于MySQL常用操作(完整性约束条件)---DAY_2的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!