mysql 显示所有用户_mysql查看全部用户
7. 设定字段默认值
name (20) ‘’;
对于未设定的字段,如果是允许为null,则MySQL将未赋值的字段设定为null,如果设定为not null,MySQL会为数值类型设定为0,字符串类型设定为空串,时间戳设定为当前时间,ENUM类型设定为枚举中第一条数据
8. 类型只能用于int类型字段
9. 索引
索引可以使MySQL查询时不用搜索整个表,从而提高查询速度。
但是索引也有确定:1.索引会占用磁盘空间 2.索引影响插入,删除和更新操作执行时间,因为进行这些操作时必须更新索引
对于经常使用where,order by和group by这些子句进行查询的字段和用于连接表的字段,推荐使用索引。
创建索引,可以在创建表时创建索引,例如
table (
char(4) not null,
price float(6, 2) not null,
index sym ());
以上就为字段创建了索引sym
可以通过制定多个index来创建多个索引
table (
id int(4) not null,
char(4) not null,
price float(6, 2) not null,
name (50) not null
index (name), index (), key(id)
);
通过连接素有被索引的字段(最多不超过15个)创建多个索引,可以用逗号间隔字段名来创建
index (fname, lname);
index index-name on table-name (field-name, …);
index on (uname);
对于blob和text类型字段,可以强制性指定子都的个数来为他们添加索引。这可以通过向 index语句中的字段名后的圆括号中插入需要的索引长度来实现。
index on books ((100))
注意:如果在 index 和 table时未指定索引名字,MySQL会自动使用相应的字段名为索引命名
如果不需要索引时,可以使用drop index index-name on table-name,如
drop index on ;
10. 索引
表示该字段不可以重复,但是字段可以保存null,而且保存不止一个null值。
table usr(name (20) , pwd (8));
table usr(name (20) , pwd (8), (name));
into usr (name, pwd) (null, ‘xyz’),, (null, ‘abc’)-à这是允许的
11. 主键约束
主键要求不能重复,不能为null,能唯一标识表中每一条数据
主键可以是一个单独的字段,也可以是多个字段的组合
主键的定义
table users(fname (15) key, lname (15) )
table users(fname (15), lname (15), key(fname));
注意:第一种方法只能声明单个字段作为主键,要想声明多个字段作为主键,只能使用第二种方法, key(fname, lname)
12. 外键约束
类型的表不支持外键约束,只有支持外键约束。
table (id int(4) not null, name (50) not null, key(id)) type ;
table (id (6) not null, name char(255) not null, int(4) not null, index (),
key() (id)) type=;
此时在表中的是指向中id的外键,即必须是中存在的id,如果保存的不存在,则违反了外键约束
注意:在删除表时,要先删除表,然后才能删除表,否则出错
且MySQL中的不支持外键约束,如果type=,则外键不会起作用