创建表的可选部分
数据字段属性
1. unsigned 无符号的,可以让空格增加一倍. 只能用在数值型字段, 不能插入负值
2. zerofill 0填充,只能用在数据型字段, 前导0, 自动用0充补齐, 自动用于无符号的数据
3. auto_increment 只能是整数,数据每增加一条就会自动增加1,字段值是不允许重复的
mysql>create t3(id int auto_increment, name char(10)); 这条命令是不对的, 因要auto-increment要是唯一的.要创建索引就行了.
mysql>create t3(id int auto_increment primary key, name char(10));创建索引的目的是
每个表最好有个ID字段,设置为自动增涨的
4. null和not null
null是一个值, 不是没有, 是空数据
建议:在创建表时每个字段都不要插入空值null.使用not null
mysql>create table t4(id int not null, name varchar(30) not null, price double not null);
5. default 缺省值
mysql>create table t4(id int not null default 0, name varchar(30) not null default '', price double not null default 0.00);
CREATE TABLE users(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL DEFALUT '',
height DOUBLE(10,2) NOT NULL DEFAULT 0.00,
age INT NOT NULL DEFAULT 0,
sex CHAR(4) NOT NULL DEFAULT '男'
)
创建索引
1 主键索引 primary key
主要作用是确定数据库表里一条特定数据记录的位置. 最好为每一张表定义一个主键
一个表只有指定一个主键, 主键的值不能为空
2 唯一索引
都可能防止创建重复的值. 可以有多个唯一索引 unique. 目的是为了防止数据重复
3 常规索引
最重要的技术,能提升数据库的性能
索引顺序 5楼 软件 PHP 细说PHP
可以提高查找的速度,但是会减慢数据列上插入/删除/修改
不要将每个表都设置为常规索引
和表一样是独立的数据对象,可以单独使用,也可在创建表时创建
mysql> create index ind1 on users( name, age); 在name及age2个字段上创建常规索引
对要查寻的条件的字段设置成常规索引,可提高查寻的速度
mysql>drop index ind1 on users;
index和key 是同义词,随便用哪个都行
mysql>create table carts(
->id int not null,
->uid int not null,
->sid int not null,
->number int not null,
->primary key(id),
->key cuid(uid), 这里的key也可写成index 创建索引表cuid
->index csid(sid)); 这里的index也可写成key 创建索引表csid
4 全文索引
fulltext类型索引 只能在myisam表使用,只能在varchar char text上用
|