• mysql数据表的基本操作:表结构操作,字段操作


    本节介绍:

    表结构操作

    • 创建数据表、
    • 查看数据表和查看字段、
    • 修改数据表结构
    • 删除数据表

    字段操作

    • 新增字段、
    • 修改字段数据类型、位置或属性、
    • 重命名字段
    • 删除字段

    首发时间:2018-02-18  21:31


    表结构操作

    创建数据表:

    • 语法  :

    create table [if not exists] 表名(
    字段名字 数据类型,
    字段名字 数据类型
    )[表选项];

     

    • 表选项  :

      • 字符集:charset表中存储数据的字符集
      • 校对集:colloate表中用来校对数据的校对集
      • 存储引擎  :engine存储数据的存储引擎
      • 表选项和库选项的区别是,如果不设置表选项就会采用库选项的设置,就好象一个“局部变量”。
    • 使用示例  :

    -- 建表之前必须指定数据库,可以使用use来指定后续的操作是基于哪个数据库的 ,也可以使用数据库名作为前缀来指定数据表创建在哪个数据库。
    -- 使用数据库名作为前缀来指定数据表创建在哪个数据库。 create table
    if not exists mydatabase.student( name varchar(20), sex varchar(20), number varchar(20), age int )charset utf8;
    -- 使用use来指定后续操作基于哪个数据库 use mydatabase; create table if not exists class( name varchar(20), room varchar(20) )charset utf8; -- 演示不带表选项的创建表 use mydatabase; create table if not exists class( name varchar(20), room varchar(20) );
    • 补充说明  :
      • if not exists 是先检查是否存在同名的表,如果存在,则不执行后面的创建语句。 十分建议使用。如果你确定这个表不存在,那么可以不使用。
      • 如果没有指定表选项,将使用默认的,比如mysql默认的存储引擎是innodb。

    查看数据表  :

    查看数据表可以查看已有数据表、数据表的字段信息

    • 语法  :
    -- 查看所有表
    show tables;
    -- 查看部分表
    show tables like '模糊匹配';
    -- 查看表的创建语句
    show create table 数据表名;
    -- 旋转查看结构
    show create table 数据表名G;
    -- 查看表结构:查看表中的字段信息:
    Desc/desc 表名;
    describe 表名;
    show columns from 表名;
    • 模糊匹配
      • _匹配单个字符
      • %匹配多个字符
    • 使用示例  :
    show tables;
    show tables
    like 'my%';
    show
    create table student;
    show
    create table studentG;
    desc student; describe student; show columns from student;

    图例:

    1. show create table student;跟show create table sudentG;

    image

    image

    Desc/describe /show columns from 表名;

    image

    修改数据表结构  :

    修改表只能修改表名和表选项。

    • 语法  :
    -- 修改表名:
    rename table 老表名 to 新表名;
    --修改表选项:
    Alter table 表名 表选项 [=] 值;
    • 使用示例  :
    rename table student to my_student;
    rename table class to my_class;
    -- 
    Alter table my_student charset gbk;
    Alter table my_collation_bin collate =utf8_bin;

    删除数据表  :

    • 语法  :
    Drop table 表名1,表名2...;
    • 使用示例  :
    drop table demo;
    drop table demodata;
    • 补充说明  :
      • 删除不可恢复,删除需谨慎。


    字段操作  :

     

     

    新增字段  :

    新增字段是在表存在的基础上新增字段

    • 语法  :
    Alter table 表名 add [column] 字段名 数据类型 [列属性] [位置];
    • 使用示例  :
    Alter table 表名 add [column] 字段名 数据类型 [列属性] [位置];
    Alter table demo add column id int first;
    Alter table demo add id int;
    Alter table demo add  class int after age;
    Alter table demo add  number int not null after age;
    • 补充说明  :
      • 位置常用语法:
        • first :表示位于第一列,
        • after 字段名 :代表在某个字段后面;
      • 列属性:主键,空值 等;

    修改字段  :

    修改字段一般都是修改字段数据类型或者字段属性

    • 语法  :
    Alter table 表名 modify 字段名 数据类型 [属性] [位置];
    • 使用示例  :
    Alter table my_student modify number char(10) after id;
    Alter table demo modify  number int null ;
    --
    alter table student modify name varchar(20) not null;
    
    --
    alter table student modify name varchar(20) not null primary key;
    • 补充说明  :
      • 字段名和数据类型是必填的,属性和位置是选填的。
      • 如果字段本身带着属性的,那么必须带上原来的,否则会被去除掉;如果需要在原有属性的基础上添加新的属性,则在填入时,那么在带上原有属性的基础上加上新的属性即可
      • image

    重命名字段  :

    • 语法  :
    Alter table 表名 change 旧字段 新字段 数据类型 [属性] [位置];
    • 使用示例  :
    alter table demo change class room varchar(10);
    Alter table my_student change sex gender varchar(10);
    • 补充说明  :
      • 数据类型是必填的,但可以是新的【重名字段可以顺便改数据类型】
      • 改名的同时也能修改字段的数据类型,属性和位置。【如果字段带有属性,重命名字段时可以不带上】image

    删除字段  :

    • 语法  :
    Alter table 表名 drop 字段名;
    • 使用示例  :
    Alter table my_student drop age;
    alter table demo drop room;
    • 补充说明  :
      • 删除需谨慎,删除字段代表着将该字段下的所有数据都将删除。

  • 相关阅读:
    Bzoj1072--Scoi2007排列perm
    Bzoj1041--Haoi2008圆上的整点
    Bzoj3932--Cqoi2015任务查询系统
    HDU 1024 Max Sum Plus Plus(DP)
    HDU 1029 Ignatius and the Princess IV
    【noip模拟题】数列
    Hello World
    vue-router 进阶
    vue2.0 源码解读(二)
    vue2.0 源码解读(一)
  • 原文地址:https://www.cnblogs.com/progor/p/8453351.html
Copyright © 2020-2023  润新知