首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > Mysql >

mysql查看全部用户

2012-07-22 
mysql查看所有用户!--[if !supportLists]--声明:以下内容是读MySQL : The Complete Reference的笔记。1.

mysql查看所有用户
<!--[if !supportLists]-->    声明:以下内容是读MySQL : The Complete Reference的笔记。


1.       <!--[endif]-->创建数据库

create database db1;

<!--[if !supportLists]-->2.       <!--[endif]-->为防止重复

create database if not exists db1;

<!--[if !supportLists]-->3.       <!--[endif]-->选择数据库

use db1;选择数据库db1

如果不选择数据库,可以使用 select x from databasename.tablename;

<!--[if !supportLists]-->4.       <!--[endif]-->删除数据库

drop database databasename;

如果不存在MySQL会警告,为防止警告,可以使用 if exist

drop database if exists db;

<!--[if !supportLists]-->5.       <!--[endif]-->创建表

create table table-name ( field-definition, …) type = table-type

type是可选项,默认类型是MyISAM

<!--[if !supportLists]-->6.       <!--[endif]-->字段定义

如果某个字段不允许为null,则可以指定 not null

但是如果not null应用于auto_increment和timestamp,auto_increment会自动用下一个值,而timestamp会使用当前时间戳

create table user( id int(3) not null auto_increment primary key,

name varchar(20) not null

birth timestamp not null);

insert into user (id, name, birth) values (null, ‘xyz’, null);

id     name       birth

1      xyz         2009-07-02 08:15:04

注意:每个表仅能有一个自增字段,而且必须声明为主键

<!--[if !supportLists]-->7.       <!--[endif]-->设定字段默认值

name varchar(20) default ‘unknown’;

对于未设定default的字段,如果是允许为null,则MySQL将未赋值的字段设定为null,如果设定为not null,MySQL会为数值类型设定为0,字符串类型设定为空串,时间戳设定为当前时间,ENUM类型设定为枚举中第一条数据

<!--[if !supportLists]-->8.       <!--[endif]-->AUTO_INCREMENT类型只能用于int类型字段

<!--[if !supportLists]-->9.       <!--[endif]-->索引

索引可以使MySQL查询时不用搜索整个表,从而提高查询速度。

但是索引也有确定:1.索引会占用磁盘空间 2.索引影响插入,删除和更新操作执行时间,因为进行这些操作时必须更新索引

对于经常使用where,order by和group by这些子句进行select查询的字段和用于连接表的字段,推荐使用索引。

创建索引,可以在创建表时创建索引,例如

create table stocks (

symbol char(4) not null,

price float(6, 2) not null,

index sym (symbol));

以上就为symbol字段创建了索引sym

可以通过制定多个index来创建多个索引

create table stocks(

id int(4) not null,

symbol char(4) not null,

price float(6, 2) not null,

name varchar(50) not null

index (name), index (symbol), primary key(id)

);

通过连接素有被索引的字段(最多不超过15个)创建多个索引,可以用逗号间隔字段名来创建

index (fname, lname);

create index index-name on table-name (field-name, …);

create index usrname on sysusers (uname);

对于blob和text类型字段,可以强制性指定子都的个数来为他们添加索引。这可以通过向create index语句中的字段名后的圆括号中插入需要的索引长度来实现。

create index synopsis on books (synopsis(100))

注意:如果在create index 和create table时未指定索引名字,MySQL会自动使用相应的字段名为索引命名

如果不需要索引时,可以使用drop index index-name on table-name,如

drop index usrname on sysusers;

<!--[if !supportLists]-->10.   <!--[endif]-->Unique索引

unique表示该字段不可以重复,但是unique字段可以保存null,而且保存不止一个null值。

create table usr(name varchar(20) unique, pwd varchar(8));

create table usr(name varchar(20) , pwd varchar(8), unique(name));

insert into usr (name, pwd) values (null, ‘xyz’),, (null, ‘abc’)-à这是允许的

<!--[if !supportLists]-->11.   <!--[endif]-->主键约束

主键要求不能重复,不能为null,能唯一标识表中每一条数据

主键可以是一个单独的字段,也可以是多个字段的组合

主键的定义

create table users(fname varchar(15) primary key, lname varchar(15) )

create table users(fname varchar(15), lname varchar(15), primary key(fname));

注意:第一种方法只能声明单个字段作为主键,要想声明多个字段作为主键,只能使用第二种方法,primary key(fname, lname)

<!--[if !supportLists]-->12.   <!--[endif]-->外键约束

MyISAM类型的表不支持外键约束,只有Innodb支持外键约束。

create table department (id int(4) not null, name vharchar(50) not null, primary key(id)) type innodb;

create table employees(id smallint(6) not null, name char(255) not null, fk_department int(4) not null, index (fk_department),

foreign key(fk_department) references department(id)) type=innodb;

此时在employee表中的fk_department是指向department中id的外键,即fk_deparment必须是department中存在的id,如果保存的不存在,则违反了外键约束

注意:在删除表时,要先删除employees表,然后才能删除department表,否则出错

且MySQL中的MyISAM不支持外键约束,如果type=MyISAM,则外键不会起作用




热点排行