一、 MySQL支持的存储引擎:
show engines\G 查看所有支持的存储引擎
show variables like ‘storage_engines%’; 查看正在使用的存储引擎
常见的收索引擎:
1、InnoDB 存储引擎
2.库操作、MyISAM 存储引擎
3、NDB 存储引擎
4、Memory 存储引擎
5、Infobright 存储引擎
6、NTSE 存储引擎
7、BLACKHOLE 黑洞引擎
创建四个表,分别使用innodb,myisam,memory,blackhole存储引擎
create table t1(id int) engine=innodb;
create table t2(id int) engine=myisam;
create table t3(id int) engine=memory;
create table t4(id int) engine=blackhole;
二、表的增删改查:
创建表:
create table 表名(字段名 类型【(宽度) 约束条件】,可以放多个);
注意:
同一张表内的字段名不能重复
字段名和类型是必须要有的
库操作、宽度、约束条件可选
示例:
创建表的时候首先要切断到库 use 库名;
create table t1(id int not null unique auto_increment,
name char(6) not null ,
sex enum(‘male’,‘female’) not null default ‘male’);
#--------------------------------------
创建表 t1,id 整数类型,不能为空,主键,自增长,
name 宽度为6,不能为空,
sex 枚举enum从(‘male’,‘female’)里写一个,不能为空,默认为‘male’
查看表:
show tables; ---------------------------------查看库下所有的表名
desc t1;-----------------------------------------查看 t1 表结构(describe)
show create table t1\G; #查看表详细结构,可加\G 就是把表的符号去除调。
select id,name from t1; ---------------------查看 t1 表里的id和name
select * from t1;----------------------------查看t1 表里的所有内容
插入内容
方法一:
insert into t1 values(1,‘blue’,23)----t1表中插入id 1 name ‘blue’ age 23信息
方法二:
insert into t1(id,name) values(值1,值2)—在t1表中的id,name列添加值1,值2
示例:
创建表:create table s1(id int,name char(6),sex enum(‘male’,‘female’));
方法一:
insert into s1 values(1,‘chen’,‘male’),(2,‘liu’,‘female’);
mysql> select * from s1;
±-----±-----±-------+
| id | name | sex |
±-----±-----±-------+
| 1 | chen | male |
| 2 | liu | female |
±-----±-----±-------+
方法二:
insert into s1(id,name) values(3,‘zhang’);
mysql> select * from s1;
±-----±------±-------+
| id | name | sex |
±-----±------±-------+
| 1 | chen | male |
| 2 | liu | female |
| 3 | zhang | NULL |
±-----±------±-------+
修改表结构:
>语法:
1、修改表名:
alter table 表名 rename 新表名;
>2、库操作,增加字段:
alter table 表名 add 字段名 数据类型 约束条件,add 字段名 数据类型 约束条件;
alter table 表名 add 字段名 数据类型 约束条件 first;—将这条信息放到开头
alter table 表名 add 字段名 数据类型 约束条件 after 字段名;----将这条信息放到字段名后
>3、删除字段:
alter table 表名 drop 字段名;
>4、修改字段:
alter table 表名 modify 字段名 数据类型 约束条件;
alter table 表名 modify 旧字段名 新字段名 旧数据类型 约束条件;
alter table 表名 modify 旧字段名 新字段名 新数据类型 约束条件;
>5、复制表:
复制表结构+记录(key不会复制:主键、外键、索引)
create table new_t1 select * from t1;
#查到t1表里所有的内容复制给new_t1
>只复制表结构:
select * from t1 where 1=2;
因为条件1=2不成立,就查看不到t1表的内容,但是表的结构是可以查到
那么就可以这样操作
create table new_t1 select * from t1 where 1=2;
这个时候因为条件不成立,查到的只有表结构,所以复制给new_t1的只有表结构
create table t2 like t1; --------用like就可以只复制表结构没有数据。
>6、删除表:
drop table 表明;
修改字段示例:
1、修改存储引擎:
alter table t1 engine=innodb;
2、添加字段:
alter table t1 add name varchar(20) not null,add age int(3) not null default 23;
添加字段到name前:
alter table s1 add school varchar(6) not null after name;
添加字段到开头:
alter table s1 add eat varchar(10) not null first;
3、删除字段
alter table s1 drop eat;
4、修改字段类型 modify:
alter table s1 modify name varchar(20);
alter table s1 modify id int not null primary key auto_increment;-------将id修改为主键
5、增加约束
alter table s1 modify id int not null primary key auto_increment;
#将id设置为主键、自增长
6、对已经存在的表增加复合主键:
前提:当前表未设置主键。
alter table s1 add primary key(id,name);
7、删除主键:
前提:如果主键是自增长,那么必须先删除自增长
a、删除自增约束:
alter table s1 modify id int not null;
b、删除主键:
alter table s1 drop primary key;