day 34

day 34 MySQL基本操作

01.操作表;

  1. 表的增删改查

  2.  
    create table 表名(
    		字段名 列类型 [列的约束],
    		字段名 列类型 [列的约束],
        字段名 列类型 [列的约束] # 最后一行不要加逗号
        ......
        )chaarset=utf-8;# 加分号表示sql语句写完了
    
  3. # 推荐增加表的使用方式
    create table t3(
    		id  int unsigned auto_increment primary key,
    		name char(10) not null default 'xxx',
    		age int not null default 0,
      	gender enum('male','female')
    		)charset=utf8;
    
    drop table 表名;# 线上禁用
    
    # 改表名
    alter table 旧表名 rename 新表名; # 修改表名
    
    # 增加字段1
    alter table 表名 
    add 字段名 列类型[列的约束],
    add 字段名 列类型[列的约束];
    
    # 增加字段2
    alter table 表名
    add 字段名 列类型[列的约束] first; # 添加为第一列
    
    # 增加字段3
    alter table 表名
    add 字段名1 列类型[列的约束] after 字段名2; # 添加到字段名2后
    
    # 删除字段
    alter table 表名 drop 字段名;
    
    # 修改字段属性
    alter table 表名 modify 字段名 数据类型【完整性约束条件】;
    
    # 修改字段名和属性
    alter table 表名 change 旧字段名 新字段名 新数据类型 【完整约束性条件】;# 只修改字段名需将旧字段的属性从新打一遍
    
    # 查询数据库内的表
    show tables;
    # 查看某个表的创建语句
    show create table 表名;
    # 查看某个表的所有数据
    select * from 表名;
    # 查看某个表的结构
    describe 表名;
    
    1. 复制表结构
    # like
    create table 新表名 like 想要复制的表名;
    
  4. 列的约束

  5. unsigned # 无符号(用于整型)
    not null # 标识该字段不能为空
    default # 为该字段设置默认值 
    auto_increment # 标识该字段值自动增长(整数类型,且为主键)
    primary key # 表示该字段为该表的主键,可以唯一的标识记录
    unique key # 标识该字段的值是唯一的
    foreign key # 表示该字段为该表的外键
    zerofill # 使用0填充 
    
  6. 列类型

    1. 数字类型
      1. 整型;
        1. tinyint
        2. smallint
        3. int
        4. mediumint
        5. bigint
        6. 都是整数类型,只是取值范围不同
        7. 在后面加上unsigned使其变为无符号的,只有整数
      2. 浮点型;
        1. float;不一定精确的
        2. decimal;非常精确的数字
          1. decimal(m,d) m是数字总个数(符号不算),d是小数点后位数,m最大65,d最大30
      3. img
    2. 字符串
      1. char(长度);定长,括号内添加该字符串最大长度
        1. 用来保存建表时知道该列数据的长度如,身份证,电话号(11),md5加密后的值(32)等
      2. varchar(长度);变长,括号内添加该字符串最大长度
        1. 如果不能确定数据长度时建议使用
      3. char保存长度永远占规定长度,varchar占真实长度加一位用来保存数据长度
    3. 时间日期类型
      1. time;时间
      2. date;日期
      3. datetime;日期加时间
      4. timestamp;时间戳
    4. 枚举类型
      1. enum(列出所有的值);只能输入该范围内的值

02.操作表数据行;

    1. 增加数据;

      # 语法
      insert into 表名 (列1,列2) values(值1,值2);
      
      insert into t1 (id, name) values (1, 'zekai');
      insert into t1 (id, name) values (1, 'zekai2');
      insert into t1 (id, name) values (1, 'zekai2'),
      (2, 'zekai3'),
      (3, 'zekai4');
      
    1. delete
    delete from 表名 where 条件;# 删除符合该条件的行
    delete from 表名;# 删除该表内所有数据
    delete from 表名 where 列名 is null;# 删除值为null的行
    
    1. truncate
    truncate 表名;# 没有where条件只能全部删除
    
    1. 区别;
      1. delete之后,再插入数据从上一次的主键自增加1开始,truncate则是从头开始
      2. delete删除,是一行一行的删除,truncate删除,直接删除全部,速度要高于delete
  1. update 表名 set 列名1=新值1,列名2=新值2 where 条件;
    
  2. select 列1,列2 from 表名;#(*代表所有列)
    
    select * from 表名 列名 between 值1 and 值2;#在值1与值2的闭区间范围内
    
    select distinct 列1,列2 from 表名;# 去重,不显示列1和列2全重复的行值
    
    # 通过四则运算取值 (不要用)
    select 列名1,列名2运算,from 表名; 
    # 列
    select name,age+10,from t2;
    
    select * from 表名 where 列名 in(值1,值2,值3) # 获取到这几个值的行
    
    # 模糊查询like (不要用)
    select * from 表名 where 条件;
    select * from t2 where name like 'n%' # 获取以n开头的数据行,%表示任意字符
    select * from t2 where name like '%n' # 获取以n结尾的数据行
    select * from t2 where name like '%n%' # 包含n的数据行
    
  3. pass

  4. # 创建表
    create table oldboy(
    id int unsigned auto_increment primary key,
    name char(10) not null default 'sb',
    age int not null default 0,
    status enum('teacher','student'),
    wage int not null default 0
    )charset=utf8;
    
    # 插入值
    insert into oldboy (name,age,status,wage) values
    ('nick',25,'teacher',10000),
    ('tank',27,'teacher',5000),
    ('jin_sb',33,'teacher',250),
    ('zekai',31,'teacher',20000),
    ('logan',23,'student',0),
    ('dsb',35,'student',0);
    
    # 查看岗位是teacher的员工姓名、年龄
    select name,age from oldboy where status = 'teacher';
    # 查看岗位是teacher且年龄大于30岁的员工姓名、年龄
    select name,age from oldboy where status = 'teacher' and age>30;
    # 查看岗位是teacher且薪资在9000-1000范围内的员工姓名、年龄、薪资
    select name,age,wage from oldboy where status = 'teacher' and wage between 1000 and 9000;
    # 查看岗位描述不为NULL的员工信息
    select * from oldboy where status not is 'null';
    # 查看岗位是teacher且薪资是10000或9000或30000的员工姓名、年龄、薪资
    select name,age,wage from oldboy where status = 'teacher' and (wage between 1000 and 9000 or wage = 3000);
    # 查看岗位是teacher且薪资不是10000或9000或30000的员工姓名、年龄、薪资
    select name,age,wage from oldboy where status = 'teacher' and wage not in (9000,10000,30000);
    # 查看岗位是teacher且名字是jin开头的员工姓名、年薪
    select name,wage*12 as y_wage from oldboy where status = 'teacher' and name like 'jin%';
    
原文地址:https://www.cnblogs.com/luocongyu/p/11761226.html