当前位置:Gxlcms > 数据库问题 > MySQL(二)

MySQL(二)

时间:2021-07-01 10:21:17 帮助过:3人阅读

TABLE table_name( column1 datatype contrai, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY(one or more columns) );

  创建班级表:

  1. <span style="color: #0000ff;">create</span> <span style="color: #0000ff;">table</span><span style="color: #000000;"> classes(
  2. id </span><span style="color: #0000ff;">int</span> unsigned auto_increment <span style="color: #0000ff;">primary</span> <span style="color: #0000ff;">key</span> <span style="color: #808080;">not</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">,
  3. name </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">10</span><span style="color: #000000;">)
  4. ); <br><br></span>
  1.  创建学生表:
  1. <span style="color: #0000ff;">create</span> <span style="color: #0000ff;">table</span><span style="color: #000000;"> students(
  2. id </span><span style="color: #0000ff;">int</span> unsigned <span style="color: #0000ff;">primary</span> <span style="color: #0000ff;">key</span> auto_increment <span style="color: #808080;">not</span> <span style="color: #0000ff;">null</span><span style="color: #000000;">,
  3. name </span><span style="color: #0000ff;">varchar</span>(<span style="color: #800000; font-weight: bold;">20</span>) <span style="color: #0000ff;">default</span> <span style="color: #ff0000;">‘‘</span><span style="color: #000000;">,
  4. age </span><span style="color: #0000ff;">tinyint</span> unsigned <span style="color: #0000ff;">default</span> <span style="color: #800000; font-weight: bold;">0</span><span style="color: #000000;">,
  5. height </span><span style="color: #0000ff;">decimal</span>(<span style="color: #800000; font-weight: bold;">5</span>,<span style="color: #800000; font-weight: bold;">2</span><span style="color: #000000;">),
  6. gender enum(</span><span style="color: #ff0000;">‘</span><span style="color: #ff0000;">男</span><span style="color: #ff0000;">‘</span>,<span style="color: #ff0000;">‘</span><span style="color: #ff0000;">女</span><span style="color: #ff0000;">‘</span>,<span style="color: #ff0000;">‘</span><span style="color: #ff0000;">人妖</span><span style="color: #ff0000;">‘</span>,<span style="color: #ff0000;">‘</span><span style="color: #ff0000;">保密</span><span style="color: #ff0000;">‘</span><span style="color: #000000;">),
  7. cls_id </span><span style="color: #0000ff;">int</span> unsigned <span style="color: #0000ff;">default</span> <span style="color: #800000; font-weight: bold;">0</span><span style="color: #000000;">
  8. )</span>

 

  修改表-添加字段:  

          alter table 表名 add 列名 类型;

          例:alter table students add birthday datetime;


  修改表-修改字段:重命名版       

          alter table 表名 change 原名 新名 类型及约束;
          例:alter table students change birthday birth datetime not null;


   修改表-修改字段:不重命名版      

          alter table 表名 modify 列名 类型及约束;

          例:alter table students modify birth date not null;


  修改表-删除字段 :

          alter table 表名 drop 列名; 
          例:alter table students drop birth


  删除表:
        drop table 表名;
        例:drop table students;


  查看建表语句:

      show create table 表名;
      例: show create table classes;


二: 数据库的怎删改查(curd)  

    curd的解释: 代表创建(Create)、更新(Update)、读取(Retrieve)和删除(Delete)  

  1.查询基本使用   

    (1)查询所有列  

        select * from 表名; 例: select * from classes;  

  
    (2)查询指定的列


        select 列1,列2 from 表名; 例: select id,name from classes;  

 

  2.增加

      格式:INSERT [INTO] tb_name [(col_name,...)] {VALUES | VALUE} ({expr | DEFAULT},...),(...),...


      说明:主键列是自动增长,但是在全列插入时需要占位,通常使用0或者 default 或者 null 来占位,插入成功后以实际数据为准

     (1)全列插入:值的顺序与表中字段的顺序对应

        insert into 表名 values(...);

        例:insert into students values(0,’郭靖‘,1,‘蒙古‘,‘2016-1-2‘);

     (2)部分列插入,值的顺序与给出的列顺序对应

       insert into 表名(列1,列2,...)values(值1,值2,..);

       例:insert into students(name,hometown,birthday) values(‘黄蓉‘,‘桃花岛‘,‘2016-3-2‘);

     (3)全列多行插入:值的顺序与给出的列顺序对应

        insert into 表名 values(...),(...)...;  

        例:insert into classes values(0,‘python1‘),(0,‘python2‘);

        nsert into 表名(列1,...) values(值1,...),(值1,...)...;

        例:insert into students(name) values(‘杨康‘),(‘杨过‘),(‘小龙女‘);   

  3.修改

     格式: UPDATE tb_name SET col1={expr1|DEFAULT} [,col2={expr2|default}]...[where 条件判断]  

       update 表名 set 列1=值1,列2=值2... where 条件

       例:update students set gender=0,hometown=‘北京‘ where id=5;   

 

   4.删除

      格式:DELETE FROM tbname [where 条件判断]

         TRUNCATE tbname

         delete from 表名 where 条件

         例:delete from students where id=5;


    
如果全部清空表中的数据delete from tbname;但是自增字段没有被重置
               truncate taname; 速度更快,并将自增字段重置

 

三:备份

  运行mysqldump命令

  1. mysqldump –uroot –p 数据库名 <span style="color: #808080;">></span><span style="color: #000000;"> python.sql;
  2. # 按提示输入mysql的密码</span>

 

四:恢复

  1.连接mysql,创建新的数据库

  2.退出连接,执行下面的命令 

  1. mysql <span style="color: #808080;">-</span>uroot –p 新数据库名 <span style="color: #808080;"><</span><span style="color: #000000;"> python.sql
  2. # 根据提示输入mysql密码</span>

 

MySQL(二)

标签:type   values   sign   one   charset   连接   truncate   打开   使用   

人气教程排行