码迷,mamicode.com
首页 > 数据库 > 详细

MySQL增删改查的常用操作指令总结

时间:2016-07-10 08:42:11      阅读:214      评论:0      收藏:0      [点我收藏+]

标签:

总结:
1、数据库操作:
创建库:
    create database db_name;

查询库:
    show databases;     //显示所有的数据库
    show create databases db_name; //显示特定的数据库

删除库:
    drop database db_name;

修改库:
    alter database db_name [modify command];
    alter database db_name character set gbk;

2、表操作:
创建表:
    use db_name;
    create table tbl_name (column_structure) [tbl_option];
如:
    create table tbl_name(
    variable_name1 varchar(10)
    variable_name2 int
    );
或 create table db_name.tbl_name(
    variable_name1 varchar(10)
    variable_name2 int
    );

查询表:
查看有哪些表:   
    show tables;
或show tables like ‘pattern_%‘;
查看表的创建信息:
    show create table tbl_name; 
或show create table tbl_name \G
查看表的结构:
    describe tbl_name;
或desc tbl_name;]

删除表:
    drop table [if exists] tbl_name;

修改表:
修改表名:
单个:rename table old_tbl_name to new_tbl_name;
多个:rename table old_tbl_name1 to new_tbl_name1, old_tbl_name2 to new_tbl_name2;
跨数据库:rename table old_tbl_name to db_name.new_tbl_name;
修改列定义:
增加新列定义:add
    alter table tbl_name add new_column_name data_type;  
如alter table exam_student add height int;
修改列的定义(新的属性或者数据类型):modify
    alter table tbl_name modify column_name new_data_type;
删除一个列:drop
    alter table tbl_name drop column_name;
重命名一个列:change
    alter table tbl_name change old_column_name new_column_name new_data_type;
修改表选项:
    alter table tbl_name new_tbl_option;
    alter table tbl_name character set utf8;

3、数据操作:
创建数据:
    insert into tbl_name (fields_list) values (values_list);
如:insert into exam_student (stu_name, stu_no) values (‘xiaoming‘, ‘php030_01‘);
若插入所有字段值:
insert into exam_student values (‘xiaoming‘, ‘php030_01‘, 98);

查询数据:
查看字段信息:
    select * from tbl_name;
参看具体字段列表信息:    
select fields_list from tbl_name where condition;
如:默认状态下select stu_name, stu_no from exam_student;
或:select stu_name, stu_no from exam_student where 1;
有条件查询:
select * from tbl_name where condition;
如select * from exam_student where fenshu >= 60;

删除数据:
    delete from tbl_name condition;
如delete from exam_student where fenshu <= 50;

修改数据:
    update tbl_name set field = new_value where condition;
如:update exam_student set fenshu =100 where fenshu >= 97;

4、主键:primary
如:法一:
create table teacher(
t_id int primary key [auto_increment],
t_name varchar(5),
class_name varchar(6),
t_days tinyint unsigned
);
法二:
create table teacher(
t_id int,
t_name varchar(5),
class_name varchar(6),
t_day tinyint unsigned,
primary key (t_id)
);
insert into teacher values (1, ‘王老师‘, ‘0225‘, 23);
自动增长:auto_increment
    alter table tbl_name auto_increment initial_value;

5、外键:foreign key
如:
主表:
create table itcast_class(
class_id int primary key auto_increment,
class_name varchar(10) not null default ‘itcast_php‘ comment ‘班级名称‘
) character set utf8;
从表:
create table itcast_student(
stu_id int primary key auto_increment,
stu_name varchar(10) not null default ‘‘,
class_id int,
foreign key (class_id) references itcast_class (class_id)
) character set utf8;
插入数据的顺序:先主表,后从表
insert into itcast_class values (null, ‘php0331‘);
insert into itcast_student values (null, ‘张三‘, 1);
删除操作:
alter table itcast_student drop foreign key itcast_student_ibfk_1;
alter table itcast_student add foreign key (class_id) references itcast_class (class_id) 
on delete set null;/*主表里删除某个字段,从表若对应外键则变成null*/
或者
on delete cascade;/*主表删除一个字段,从表对应的外键则会删除相应的值*/
又或者
on update restrict;/*不允许主表进行更新操作*/
delete from itcast_class where class_id = 1;

  

MySQL增删改查的常用操作指令总结

标签:

原文地址:http://www.cnblogs.com/Andya/p/5657061.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!