标签:
--查看创建表的标的定义,可以查看主键 SHOW CREATE TABLE user1; INSERT user1 VALUES(1,‘king‘);--主键不能重复 INSERT user1 VALUES(13,‘king‘); --复合主键 CREATE TABLE IF NOT EXISTS user2( id INT, username VARCHAR(20), card CHAR(18), PRIMARY KEY(id,card)--复合主键,这是一个主键 );
CREATE TABLE IF NOT EXISTS user3( id SMALLINT KEY AUTO_INCREMENT, username VARCHAR(20) )AUTO_INCREMENT=100;--这是指定自增长的值 查看自增长的值 SHOW CREATE TABLE user3; user3 | CREATE TABLE `user3` ( `id` smallint(6) NOT NULL AUTO_INCREMENT, `username` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=114 DEFAULT CHARSET=utf8 | INSERT user3 VALUES(1,‘king‘);--第一次指定值,id是自增长的 INSERT user3(username) VALUES(‘queue‘);--只是插入username的值,手动指定的,id的值是自增长的值,就是已经有的编号加1 INSERT user3 VALUES(111,‘queue‘); INSERT user3(username) VALUES(‘queue‘); INSERT user3 VALUES(DEFAULT,‘queue‘); ysql> SELECT * FROM user3; +-----+----------+ | id | username | +-----+----------+ | 1 | king | | 2 | queue | | 111 | queue | | 112 | queue | | 113 | queue | +-----+----------+ 修改自增长的值 ALTER TABLE user3 AUTO_INCREMENT=500 给自增长赋值
--测试NOT NULL 非空一般是搭配默认值来玩的 CREATE TABLE IF NOT EXISTS user4( id INT UNSIGNED KEY AUTO_INCREMENT,--这里 无符号的键值 非空 userName VARCHAR(20) NOT NULL, password CHAR(20) NOT NULL, age TINYINT UNSIGNED ); INSERT user4(username,password) VALUES(‘king‘,‘king‘); INSERT user4(username,password,age) VALUES(‘king‘,‘king‘,12); INSERT user4(username,password) VALUES(‘NULL‘,‘NULL‘); mysql> SELECT * FROM user4; +----+----------+----------+------+ | id | userName | password | age | +----+----------+----------+------+ | 1 | king | king | NULL | | 2 | king | king | 12 | | 3 | NULL | NULL | NULL | +----+----------+----------+------+
--测试默认值 CREATE TABLE IF NOT EXISTS user5( id INT UNSIGNED KEY AUTO_INCREMENT, userName VARCHAR(20) NOT NULL, password CHAR(20) NOT NULL, age TINYINT UNSIGNED DEFAULT 18,--DEFAULT +数据 添加默认值 和NOT NULL搭配使用 addr VARCHAR(50) NOT NULL DEFAULT ‘北京‘, sex ENUM(‘男‘,‘女‘,‘保密‘) NOT NULL DEFAULT ‘男‘ ); mysql> DESC user5; +----------+----------------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | userName | varchar(20) | NO | | NULL | | | password | char(20) | NO | | NULL | | | age | tinyint(3) unsigned | YES | | 18 | | | addr | varchar(50) | NO | | 北京 | | | sex | enum(‘男‘,‘女‘,‘保密‘) | NO | | 男 | | +----------+----------------------------+------+-----+---------+----------------+ INSERT user5(userName,password) VALUES(‘king‘,‘king‘); INSERT user5 VALUES(2,‘fuzhiqiang‘,‘dazhi‘,29,‘上海‘,‘保密‘); INSERT user5 VALUES(3,‘fuzhiqiang‘,‘dazhi‘,DEFAULT,DEFAULT,‘保密‘);--利用关键字DEFAULT进行默认值的插入 mysql> SELECT * FROM user5; +----+------------+----------+------+--------+--------+ | id | userName | password | age | addr | sex | +----+------------+----------+------+--------+--------+ | 1 | king | king | 18 | 北京 | 男 | | 2 | fuzhiqiang | dazhi | 29 | 上海 | 保密 | | 3 | fuzhiqiang | dazhi | 18 | 北京 | 保密 | +----+------------+----------+------+--------+--------+
CREATE TABLE IF NOT EXISTS user6( id TINYINT UNSIGNED KEY AUTO_INCREMENT, username VARCHAR(20) NOT NULL, card CHAR(18) UNIQUE--唯一性 ); mysql> DESC user6; +----------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------------+------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | username | varchar(20) | NO | | NULL | | | card | char(18) | YES | UNI | NULL | | +----------+---------------------+------+-----+---------+----------------+ INSERT user6(username) VALUES(‘A1‘); INSERT user6(username,card) VALUES(‘1111‘,‘A1‘); INSERT user6(username,card) VALUES(‘2222‘,‘A1‘);--这个是错误的,因为card字段是唯一性的,不能插入相同的值 +----+----------+------+ | id | username | card | +----+----------+------+ | 1 | A1 | NULL | | 2 | A1 | NULL | | 3 | 1111 | A1 | +----+----------+------+
--建表的步骤 CREATE TABLE [IF NOT EXISTS] tbl_name( --按照下面的顺序呢进行建表 字段名称 字段类型 [UNSIGNED|ZEROFILL] [NOT NULL|NULL] [DEFAULT 默认值] [[PRIMARY] KEY | UNIQUE [KEY]] [AUTO_INCREMENT] --上面中 主键的PRIMARY可以省略, 唯一性约束的KEY可以省略, )ENGINE=INNODB CHARSET=UTF8 AUTO_INCREMENT=100;--引擎:默认的是INNODB, 编码方式:默认的是utf8, 自增长量:可以在这里初始化
标签:
原文地址:http://www.cnblogs.com/Kobe10/p/5762646.html