zoukankan      html  css  js  c++  java
  • insert增数据详解

    查看表结构:

    desc 表名;

    describe的缩写,意为描述

    增加数据不会改变表的结构,只是增加了行。

    创建一张表:

    1 mysql>  create table class( 
    2     ->  id int primary key auto_increment, 
    3     ->  name varchar(10) not null default '',
    4      -> gender char(1) not null default '', 
    5     ->  company varchar(20) not null default '',
    6      ->  salary decimal(6,2) not null default 0.00,
    7      ->  fanbu smallint not null default 0
    8      -> );
    • 把id设置为主键、自增(自增的主键在添加数据可以不写)
    • name、gender、company、salary、fanbu这些属性都设置为不能为空,如果没有添加则默认为空字符串、0.0
    • decimal(6,2),表示薪资有六位数,小数点后占两位

    insert步骤:

    1. 往哪张表添加行?
    2. 给哪几列添加值?
    3. 分别是什么值?
    mysql>  insert into class
         ->  (id,name,gender,company,salary,fanbu)
         ->  values
         ->  (1,'张三','','百度',8888.66,145);

    在添加数据之前,如果使用gbk编码,可能导致中文字符的长度不够的错误,所以可以使用:

    mysql> set names utf8mb4;

    再次添加数据:

    mysql> insert into class 
        -> (name,gender,salary)
         -> values 
        ->  ('李四','',9832.23);

    这次没有全部添加

    虽然没有添加id,但还是显示2,因为前面设置了id为自增的,每次添加数据id都会加一,没有添加的使用默认设置的值。

    如果插入所有列,则可以不声明待插入的列,默认为依次插入所有列

    此时id也必须添加或写null占位(不推荐,会出现兼容问题),否则不会对应

    1 mysql> insert into class
    2      -> values 
    3     -> (3,'王五','','腾讯',3245.23,435);

    如果想添加多行,则每行记录间用逗号隔开

    1 insert into class (name,company,salary) values ('刘备','皇家',23.34), ('曹操','宦官后裔',34.34);
  • 相关阅读:
    EEPlat 的 后台业务处理模型
    poj 1012 Joseph (约瑟夫问题)
    python使用正則表達式
    二维码_encode与decode
    UITableView显示不全
    Bottle 中文文档
    不相交集python实现
    面试题1:落单的数
    Android开发/源代码资源汇总
    leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法
  • 原文地址:https://www.cnblogs.com/sunbr/p/11715454.html
Copyright © 2011-2022 走看看