添加数据库
create database <数据库名>
删除数据库
drop database <数据库名>
建表语句
create table <表名> ( <字段名1> <类型1> [,..<字段名n> <类型n>]);
create table table_name(
id int not null primary key auto_increment,
name varchar(20)
)engine=InnoDB auto_increment=1 default charset=utf8;;
讲解::
ENGINE=InnoDB使用innodb存储引擎
DEFAULT CHARSET=utf8 数据库默认编码为utf-8
AUTO_INCREMENT=1 自增键的起始序号为1
扩展:
1.InnoDB,是MySQL的数据库引擎之一,为MySQL AB发布binary的标准之一,InnoDB支持事务处理和外键等高级功能。
2.AUTO_INCREMENT会在新记录插入表中时生成一个唯一的数字。希望在每次插入新记录时,自动地创建主键字段的值,可以在表中创建一个 auto_increment 字段。
删除表语句
drop table <表名>
插入数据
insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )]
例:
insert into students (stu_id,`name`,stu_age,stu_sex,class) VALUE (1001,'张三',24,'男','计科6班');
删除语句
delete from 表名 where 表达式
delete from students where stu_id = '1001';
更新语句
update 表名 set 字段=新值,… where 条件
update students set `name` ='李四' where stu_id = '1001';
添加字段
alter table 表名 add字段 类型 其他;
alter table students add hometown VARCHAR(30) default NULL;
删除字段
alter table 表名 drop 字段名
alter table students drop hometown;
修改字段名
alter table 表名 change 原字段名 新字段名 字段类型
alter table students change class _class varchar(30);
修改字段类型
alter table 表名 modify column 字段名 类型
alter table t_blog modify column content longtext;
修改表名
rename table 原表名 to 新表名
rename table students to student;
删除字段不为空的约束
alter table [表名] modify [列名] 字段类型 null;
alter table students modify name varchar(10) null;
添加主键
alter table 表名 add primary key (字段名);
alter table one add primary key (id);
添加默认值
alter table 表名 alter 字段名 set default 默认值;
alter table students alter sex default `男`;
删除默认值
alter table 表名 alter 字段名 drop default ;
alter table students sex drop default;
删除主键
alter table 表名 drop primary key;
alter table one drop primary key;
添加外键
alter table 表名 add constraint 外键名 foreign key(需加外键表的字段名) referencnes 关联表名(关联字段名);
alter table teachers add constraint fk_1 foreign key (st_id) references student(stu_id);
删除外键
alter table 表名 drop foreign key 外键id;
外键id可以用show create table your_table_name找出来
alter table eqpoperator drop foreign key `lims_eqpoperator_ibfk_1`;
查看表结构
desc 表名
查看建表语句
show create table 表名
查看表状态
show table status;
修改表的搜索引擎
alter table 表名 engine= 搜索引擎名
alter table teachers engine=INNODB;