zoukankan      html  css  js  c++  java
  • Python MySQL(学习SQL语句)

    1. 操作文件夹
      1. create database db1 default charset utf8; (创建文件夹)
      2. show databases ;  (显示内容)
      3. drop database db1;  (删除文件夹)
    2. 操作文件
      1. use db1; (进入)
      2. show tables; (显示表)
      3. create table t1(
        id int unsignde auto_increment primary key,
        num decimal(10,5),
        name char(10)
        age ENUM('1','2','3')
        col SET('a','b','c')
        ) engine=innodb default charset=utf8;
        (创建表)
        #  列名 类型(接受数据大小)字段后都可以接null,not null(是否支持为空),接unsigned(表示有无符号);
        #  auto_increment primary key(一个表只能有一个)   ————auto_increment(表示自增) primary key (表示约束:(不能重复   且不能为空)和加速查找)

        #  engine表示使用什么引擎  innodb(支持事务,操作可以回滚,原子性操作)myisam (支持全局检索)
        #  decimal(总位数,小数点后的位数)表示精确的小数比float和double精准
        #  char(10)为自动填充满10个,查询速度快;varchar(10) 不会,比较节省空间(所以应该把定长的放前面,变长的放后面)
            因为要查询char(10)后面的数据时只要直接跳过10个字符就好了,而varchar(10)不知道要跳过多少个
        #  ENUM()枚举类型,只能插入其中单个内容
        #  SET()集合类型,只能插入括号中的任意组合
      4. delete from t1;  (清空表,且会之前的自增会继承)
        truncate table t1; (也是清空表,但自增不继承,而且清空速度比较快)
      5. drop table t1;  (删除表)
      6. 如果上传文件或图片之类的就上传它的路径
    3. 操作文件中的内容(增删改查)
      1. insert into t1(id,name) values(1,'mc');   (插入数据)
        #  如果输入出错可能要考虑下编码,比如:utf-8(但是现在utf-8不用考虑了(/ □ ))
      2. delete from t1 where id<6;  (删除数据)
      3. update t1 set age=18;  (修改数据)
        update t1 set age=18 where age=17;
      4. select * from t1;  (查询表中的内容)
    4. 外键
      1. 优点:
        1. 节省空间
        2. 制定约束
      2. 操作:
        1. 创建一个表
          create table t1(
          id int unsignde auto_increment primary key,
          name char(10),
          department_id int,
          ) engine=innodbd efault charset=utf8;
           
        2. 再创建外键的表
          create table department(
          id int unsignde auto_increment primary key,
          title char(10)
          ) engine=innodbd efault charset=utf8;
           
        3. 加入约束
          这个方法要先创建表二
          create table t1(
          id int unsignde auto_increment primary key,
          name char(10),
          department_id int,
          constraint fk_admin_t1 foreign key (department_id) references userinfo1 department(id)
          ) engine=innodbd efault charset=utf8;
           
          或者
          alter table t1 add constraint fk_t1_department foreign key (department_id )  references department(id) on [delet/update ]reference_option  
          其中 reference_option  有以下几种(默认为RESTRICT):
          1. CASCADE,级联删除/级联更新,即主表delete或update了被其他表引用的数据,对应子表的数据也被delte或update;
          2. SET NULL,当主表delete或update了被其他表引用的数据,对应子表的数据被设为null,注意子表的外键不能设为not null;
          3. RESTRICT,主表不允许delete或update被其他表引用的数据;当没有指定时,默认是采用RESTRICT
          4. NO ACTION,在MySQL中,等效于RESTRICT;
    5. 补充
      1. 每个表只能有一个主键,但是主键不一定只有一列,可以有多列:
        (id1 int not null,
        id2 int not null,
        primary key(id1,id2)) ;  # 把两列组合成主键
      2. 所以用外键约束时,也可以写两列  foreign key (id1,id2 ) 
      3. alter table xx auto_increment=10x; 可以设置自增量
      4. 自增步长可以设置成会话级别或全局级别的:
        会话:set session auto_increment_increment = 2;
        全局:set globalauto_increment_increment = 2;
      5. 自增初始值同理:
        set auto_increment_offset=XX;
  • 相关阅读:
    WPF ViewModel 调用任意前台控件的方法
    xxxx
    modelsim一些error(warning)的原因
    [verilog] inout端口处理
    [c语言]指针数组和数组指针
    电机控制术语
    MAC和PHY关系
    IAR map文件说明
    [corterm3]汇编语法
    TI 2802x系列中断系统及应用
  • 原文地址:https://www.cnblogs.com/otome/p/12443495.html
Copyright © 2011-2022 走看看