zoukankan      html  css  js  c++  java
  • 博客整理day34 数据库sql操作

    python day34 数据库

    一 创建表

    增加表

    create table 表名(
    	字段名 列表型[可选的参数], #必须加逗号
        字段名 列表型[可选的参数]  #最后一行不加逗号
    )charset = utf8  #后面
    

    增加数据

    #语法
    insert into 表名(列1,列2) values (值1,值2);
    #例子
    insert into t1 (id,name) values (1,'momo');
    

    查询数据

    #语法
    select 列1,列2 from 表名;  (*表示查询所有的列)
    show tables;
    #列子
    select id,name from t1;
    

    修改数据

    #1.修改表名 语法
    alter table 旧表名 rename 新表名;
    
    alter table t1 rename t11;
    
    #2.增加字段 语法
    #添加的列永远是添加在最后一列之后
    alter table 表名
    	add 字段名 列表型[可选的参数];
        
    alter table t11 add name varchar(32) not null defaut '';
    
    #添加的列永远是添加在第一列
    alter table 表名
    	add 字段名 列表型[可选的参数] first;
        
    alter table t11 add name varchar(32) not null defaut '' first;
    
    #添加的列永远是添加在....列之后
    alter table 表名 add 字段名 列表型[可选的参数] after 字段名;
        
    alter table t11 add name varchar(32) not null defaut '' after age;
    
    #3.删除字段 语法
    alter table 表名 drop 字段名 ;
        
    alter table t11 drop name;
    
    #4.修改字段 语法
    #修改字段数据类型
    alter table 表名 modify 字段名 数据类型[可选的参数];
        
    alter table t11 modify name char(30);
    
    #修改字段名和数据类型
    alter table 表名 change 字段名 字段名 数据类型[可选的参数];
        
    alter table t11 change name name2 char(30) not null default '';
    

    删除数据

    #删除数据 语法
    drop table 表名;   
    
    drop table t1;
    

    复制表结构

    create table t11 like t111;
    

    二 查看表结构

    #方法一
    	describe 表名;
    #方法二
    	desc 表名;
    #方法三  查看创建表的SQL语句
    	show create table 表名
    

    三 MySQL支持的数据类型

    整型

    类型 大小 范围(有符号) 范围(无符号) unsigned 用途
    tinyint 1字节 (-128,127) (0,255) 小整数值
    smallint 2字节 (-32 768,32 767) (0,65 535) 大整数值
    mediumint 3 字节 (-8 388 608,8 388 607) (0,16 777 215) 大整数值
    int或integer 4 字节 (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整数值
    bigint 4 字节 (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 大整数值
    float 4 字节float(255,30) (-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) 0,(1.175 494 351 E-38,3.402 823 466 E+38) 单精度 浮点数值
    double 8 字节double(255,30) (-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) 双精度 浮点数值
    decimal 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 double(65,30) 依赖于M和D的值 依赖于M和D的值 小数值
    #浮点型
    create table t1(
    	id int auto_increment primary key,
        salary decimal(16,10),
        num float
    )charset=utf8;
    
    #float : 精确到小数点两位
    #decimal : 可以控制精确的小数点位 decimal(m,n) m是数字的总个数(负号不算),n是小数点后个数
    

    字符串

    类型 大小 用途
    CHAR 0-255字节 定长字符串
    VARCHAR 0-65535 字节 变长字符串
    TINYBLOB 0-255字节 不超过 255 个字符的二进制字符串
    TINYTEXT 0-255字节 短文本字符串
    BLOB 0-65 535字节 二进制形式的长文本数据
    TEXT 0-65 535字节 长文本数据
    MEDIUMBLOB 0-16 777 215字节 二进制形式的中等长度文本数据
    MEDIUMTEXT 0-16 777 215字节 中等长度文本数据
    LONGBLOB 0-4 294 967 295字节 二进制形式的极大文本数据
    LONGTEXT 0-4 294 967 295字节 极大文本数据
    #char(长度):  定长
    create table t1(
    	id int unsigned auto_increment primary key,
        name char(10) not null default 'momo'
    )charset=utf8;
    
    #varchar(长度): 变长
    create table t2(
    	id int auto_increment primary key,
        name varchar(10) not null default 'momo'
    )charset=utf8;
    
    '''区别:
    	char: 定长, 无论插入的字符是多少个,永远固定占规定的长度
    	场景:
            1. 身份证
            2. 手机号 char(11)
            3. md5加密之后的值,比如密码 等 char(32)					
    	varchar: 变长, 根据插入的字符串的长度来计算所占的字节数,但是有一个字节是用来保存字符串的大小的			
    	注意:如果,不能确定插入的数据的大小,一般建议使用 varchar(255)
    '''
    

    日期时间类型

    类型 大小 (字节) 范围 格式 用途
    DATE 3 1000-01-01/9999-12-31 YYYY-MM-DD 年月日
    TIME 3 '-838:59:59'/'838:59:59' HH:MM:SS 时分秒
    YEAR 1 1901/2155 YYYY 年份值
    DATETIME 8 1000-01-01 00:00:00/9999-12-31 23:59:59 YYYY-MM-DDHH:MM:SS 年月日时分秒
    TIMESTAMP 4 1970-01-01 00:00:00/2038 结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07 YYYYMMDDHHMMSS 混合日期和时间值,时间戳
    create table t1(
        d date,
        t time,
        dt datetime
    );	
    

    四 表的完整型约束

    auto_increment : 自增

    primary key : 主键索引,可以加快查询速度,列的值不能重复

    not null : 标识该字段不能为空

    default : 为该字段设置默认值

    #列子1
    create table t1(
    	id int,
        name char(5)
    )charset = utf8;
    #例子2
    create tabel t2(
    	id int auto_increment primary key,
        name char(10)
    )charset = utf8
    #例子3
    create table t3(
    	id int unsigned auto_increment primary key,
        name char(10) not null defualt 'hello',
        age int not null default 0
    )charset = utf8
    

    五 枚举

    ENUM 中文名称叫枚举类型,它的值范围需要在创建表时通过枚举方式显示。ENUM只允许从值集合中选取单个值,而不能一次取多个值。

    create table t1 (
        id int auto_increment primary key,
        gender enum('male','female')
    )charset utf8;
    
    mysql> insert into t9 (gender) values ('male');#不是male或者female就会报错
    

    六 操作表数据行

    #语法
    insert into 表名 (列1, 列2) values (值1,'值2');
    #例子
    insert into t1 (id, name) values (1, 'simple');
    

    #语法
    delete from 表名 where 条件;
    #例子
    mysql> delete from t1 where id=1;
    
    delete from 表名;  #删除表中所有的数据
    
    truncate 表名; 	 #没有where条件的
    区别:	
    	1. delete之后,插入数据从上一次主键自增加1开始, truncate则是从1开始
    	2. delete删除, 是一行一行的删除, truncate:全选删除 truncate删除的速度是高于delete的
    
    

    #语法
    update 表名 set 列名1=新值1,列名2=新值2 where 条件;
    mysql> update t11 set name='momo' where id=1;
    

    #语法
    select 列1, 列2 from 表名;  (*代表查询所有的列)
    select * from t66 where id=1;
    
    #between..and...: 取值范围是闭区间
    select * from t11 where id between 1 and 3;
    
    #避免重复DISTINCT
    mysql> select distinct name from t11;
    
    #通过四则运算查询
    mysql> select name, age*10 as age from t1;
    
    # in 用法
    mysql> select * from t11 where id in (1,3,5);
    
    #like : 模糊查询
    mysql> select * from t11 where name like 'm%';
    mysql> select * from t11 where name like '%o';
    mysql> select * from t11 where name like '%mo%';
    
  • 相关阅读:
    如何计算两个日期之间相差天数
    解决并发问题的小技巧
    Linq实现下拉框绑定
    No DataType in DataTemplate in Windows Phone(二)
    使用TOAD操作oracle初步
    使用log4net记录server Log
    尘世一场烟火
    No DataType in DataTemplate in Windows Phone(—)
    MVC设置初始页时发生的无法找到资源的简单错误
    oracle 使用in的灵异事件
  • 原文地址:https://www.cnblogs.com/samoo/p/11761473.html
Copyright © 2011-2022 走看看