创建,删除数据库              指定字符集

create database 数据库名[default character set utf8];  (~的点 为引号)

drop database 数据库名; 

show create database 数据库名; 查看创建语句    

alter database 数据库名 default character set gbk;  

排序规则;

创建表 

create  table  [if not  exists] 表名( 字段名 类型 [属性 约束],

字段名 类型 [属性 约束],

索引,

约束)

[表选项列表];

 

 类型[(n:长度)] [unsigned:正数] [zerofill:0填充]

        uid int(4) zerofill primary key auto_increment  0001

 

create table 表名(

变量名1 数据类型1 约束条件11 约束条件12,

变量名2 数据类型2 约束条件2 );

not null        非空

unique         唯一

primary key     主键

id int  primary key  auto_increment  自动增加

default ‘默认值’

comment ‘字段注释’

parmary key (stu_id,course_id(4));  定义多字段主键

unique key [索引名](stu_id );       唯一键

unique index [索引名](stu_id [asc]);  唯一索引

index [索引名] (stu_id(4),course_id);  定义索引

 

[constraint 约束名]  foreign key (stu_id, course_id) references 表1(A,

外键                    参考   表1 主键

[on delete cascade onupdate casade]

外键关联主键删除和更新

(表创建外键 必须要有主键)

comment = ‘表的注释’;

charset = 字符编码名称;

auto_increment = 起始整数; //自增长类型值的初值,默认是1

engine = “表的存储引擎名”; //默认是InnoDB支持事务安全

删除表

drop table [if exists] 表名; (先删外键,子表有数据父表不能删)

 

修改表(原数据不会丢失)

alter table表名    修改语句1[, 修改语句2];

修改表名      rename to 新表名;

修改类型 位置 modify 变量名 类型 [first/after变量A];

修改列名 类型 change 变量名 新变量名 类型;

添加变量 位置 add 新变量名 类型 [约束][first/after变量A]; 默最后

添加索引/键       index[索名]/primary key /unique(变量1[,变量2])    

约束名 foreign key(A)references表1(a)

修改默认值    alter 变量 set default ‘默认值’;

drop default ‘默认值’;

删除变量      drop 变量名;

删除键/索引   drop primary key/foreign key 外键名/index 索引名;

 

表选项修改    omment = ‘表的注释’;

charset = 字符编码;

auto_increment = 起始整数; 自动增长初值,默认是1

engine = “表的存储引擎名”;

 

alter table表名add constraint 约束名 foreign key(变量) references 表1(A)

on delete cascade;

 

主键值删除时:restrict(抛异常 默认)cascade(记录删除)

set null(外键为空) no action(什么都不做)

 

插入,更新,删除数据

insert into表1[(变量3,变量2)] values (“值31”,”值21”)[,(“值32”,null)];

insert into表1(字段1,字段2,) select 字段1,字段2  from 表2;

值的形式:数字 函数直接写,字符串时间加单引号

有以下属性的 不应该出现该字段名:auto_increment, timestamp,

 

update表名set 变量1=值/表达式,变量2=值2[where条件];

/*更新 表 中 变量1,变量2的值{满足条件的更改}*/

 

delete from表名[where条件];/*删除表中{满足条件的}记录*/ 事务可回滚

不删约束,auto_increment还能继续增长

 

delete from表名where 2014-year>6; 删除过期记录

truncate {table}表名;        /*删除表(记录和结构)*/

 

复制表

create table 表B like 表A;          复制表B结构

insert into 表B select * from 表A;  插入数据

 

create table 表B select * from 表A;  复制结构数据 丢索引,约束,自增

 

load data infile ‘文件完整名(含路径)’ into table 表名;

载入外部“形式整齐”的数据:

 

索引(查询块 增删改慢

全文索引   /fulltext index 索引名(变量B ))engine=myisam;

/*类型:char,varchar,text,引擎: myisam */

空间索引   /spatial index 索引名(变量B))engine=myisam; 不为空

/*类型:geometry,引擎: myisam */

创建索引 唯一 全文 空间 普通

create [unique/fulltext/spatial] index 索引名on表名(变量名[(长)] [asc/desc]);

alter table 表名 add [unique/fulltext/spatial] index 索引名on表名(变量名);

 

drop index索引名on表名

show index from表          查看表的索引

 

视图(查询得到的虚拟表)

表改→视图不变,视图改→表改

 

create [or replace] /alter  view视图名[(视图变量1,视图变量2)] as

创建  [或修改]  /修改

select变量A,变量B from表名where条件

[with [cascaded/local] check dption]

/*更新[受表,视图字段影响/受视图本身字段影响]受视图权限限制*/

 

drop view [if exists] 视图1,视图2; [restrict/cascade] 删除

 

desc 视图名;               查看视图结构

show table status like ’视图名’; 查看视图状态信息

show create view视图名;     查看视图创建代码