zoukankan      html  css  js  c++  java
  • 数据库(二)表操作

    数据库

    操作表

    语法:

    1. create table + 表名(字段名 列类型 [可选参数],字段名 列类型 [可选参数])charset=utf8;

    列约束:

    1. auto_increment:自增1
    2. primary key:主键索引加快查询速度,列的值不能重复
    3. not null 标识该字段不能为空
    4. default 为该字段设置默认值

    列类型:

    1. create table 表名(字段名 列类型 unsigned [可选的参数], 字段名 列类型 [可选参数])charset=utf8;

    2. 数字:

      整型:

      1. tinyint

      2. smallint

      3. int

      4. mediumint

      5. bigint

        a.整数类型

        b.取值范围

        c.unsigned 加上代表不能取负数,只适用于整型

      浮点型:

      1. create table t1(id int auto increment primary key,

        salary decimal(16,10)

        num float)charset=utf8;

      2. float:不一定精准

      3. decimal:非常精准

      字符串:

      1. char(长度):定长

        create table t1(

        ​ id unsigned int auto_increment primary key,

        ​ name char(10) not null default 'xxx',)charset=utf8

      2. varchar(长度):变长

        ​ create table t1(

        ​ id int auto_increment primary key,

        ​ name varchar(10) not null default 'xxx')charset=utf8;

    区别:

    char:定长,无论插入的字符是多少个,永远固定占规定的长度(场景:身份证,手机号,md5加密之后的值,比如密码(32))

    varchar:变长,根据插入的字符串的长度来计算所占的字节数,但是有一个字节是用来保存字符串的大小的;注意:如果不能确定插入的数据大小,建议使用(varchar(255))

    时间日期类型:

    year: yyyy 年

    date: yyyy-mm-dd 年月日

    date time:yyyy-mm-dd hh:mm:ss 年月日,时分秒

    datestamp:yyyy-mm-dd,hh:mm:ss

    语法:drop table + 表名

    改表名:alter table +旧表名 rename +新表名;

    增加字段:alter table 表名

    ​ add 字段名 列类型[可选的参数],

    ​ add 字段名 列类型[可选的参数];

    ​ alter table 表名 add 字段名 列类型[可选参数]first;

    ​ alter table 表名 add 字段名 列类型[可选参数]after字段名 之后;

    删除字段:alter table 表名 drop 地段名;

    修改字段:alter table 表名 modify 字段名 数据类型[完整性约 束条件...];

    复制表结构:show create table 表名;

    语法:show table ;

    操作表数据

    语法:

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

    delete from 表名 where 条件;

    truncate 表名;

    区别:

    1. delete之后,插入数据从上一次主键自增加1开始,truncate则是从1开始
    2. delete删除,是一行一行的删除,truncate:全选删除,删除速度高于delete

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

    语法:

                select 列1,列2 from 表明;(*代表查询所有的列)        
                select * from 表名;  (*代表查询所有的列)
                select * from t66 where id>30 and id<40;
                select * from t66 where id>30;
                select * from t66 where id<30;
                select * from t66 where id<=30;
                select * from t66 where id>=30;
                select * from t66 where id!=30;
                select * from t66 where id<>30;
                    mysql> select * from t1;

    in:

    select *from t1 where id in (指定查询)

    like::

    select * from t1 where name like 'x%';模糊查询

    我把月亮戳到天上 天就是我的 我把脚踩入地里 地就是我的 我亲吻你 你就是我的
  • 相关阅读:
    POJ3094 UVALive3594 HDU2734 ZOJ2812 Quicksum【进制】
    UVALive5583 UVA562 Dividing coins
    POJ1979 HDU1312 Red and Black【DFS】
    POJ1979 HDU1312 Red and Black【DFS】
    POJ2386 Lake Counting【DFS】
    POJ2386 Lake Counting【DFS】
    HDU4394 Digital Square
    HDU4394 Digital Square
    UVA213 UVALive5152 Message Decoding
    UVA213 UVALive5152 Message Decoding
  • 原文地址:https://www.cnblogs.com/zhulipeng-1998/p/12863927.html
Copyright © 2011-2022 走看看