mysql数据库基本操作

对数据库的操作

 1.数据库

  增:create database 数据库名;

    例:create database db1  

  删:drop database 数据库名;

    例:drop database db1

  改:对数据库的操作没有修改,有需求的话可以删掉重新建

  查:show databases (展示所有数据库)

  使用:use 数据库名;

    例:use db1

 2.数据表

  首先 使用一个数据库在数据库下建数据表

  use db1

  新建表:create table 表名(列表名1 约束条件,列表名2 约束条件)

    例:create table t1(id int ,name char(32));


    增:insert into t1(列名1,列名2)values (值1,值2)

      例:insert into t1(id,name)value (1,’aaa‘)

      改进:create table 表名 ( 列名1 列类型)engine=Innodb charset=utf8;

         ps:engine=Innodb引擎为innodb ,charset=utf8字符编码为utf8

      改进2:create table 表名 (列名1 列类型 auto_increment primary key
                        )engine=Innodb charset=utf8;

        例:create table t4 (id  int auto_increment primary key,
                            name char(32)  not null  default '')engine=Innodb charset=utf8;

         ps:auto_increment : 自增 primary key : 主键索引 (作用: 加快查找的速度)
                          not null : 不能为空  default : 默认值

         注:在使用auto_increment时列表类型必须设置为数值型

    删:drop table表名;

      例:drop table t1

    改:

      修改字段:   alter table 表名(t3) change 原列名(name) 新列名(username                 varchar(32) not null default '');


                    新增字段:alter table 表名(t3)  add  新列(pwd char(32)  not null  default '');     

      删除字段:   alter  table 表名(t3) drop 列名(pwd);
                  

    查:show tables 查看所有表

      desc 表名    查看表的结构

      show create table 表名  查看表的创建过程

  3.数据行

    增:insert into t1(列名1,列名2)values (值1,值2)

    删:delete from 表名(t3); 将表中的所有的 数据删除掉, 再次添加的时候, 继续会延续上      一个 ID
                
                      truncate 表名(t3);    将表中的所有的 数据删除掉, 再次添加的时候, ID 会重新开始

    改:update t3 set username='zekai';
                
                   update t3 set username='xxxx'  where  id=3;
    
                   update t3 set username='xxxx', pwd='xxxxx'  where  id=3;

  

    查:
                  select * from t3; : 将表中的 所有的列全部列出
                  select 列名, 列名, 列名 from t3 : 将某一列的值查出

原文地址:https://www.cnblogs.com/duGD/p/11014207.html