python42 1.外键的变种 2.数据表操作

昨日回顾:

一. 数据库的介绍
   管理数据的软件
 二. 数据库的 分类:
   a.关系型数据库
    有约束
   b.非关系型数据库
    没有约束
    
 三. MySQl 的安装:
   a. exe msi文件 点击下一步下一步
   
   b. 压缩包
    bin
     mysqld : 启动mysql服务的
     mysql  : 连接mysql
    环境变量
 四. 数据库(文件夹)
   数据表 (文件)
    数据行(文件行)
 
 五. 数据库:
   增:
    create database 数据库名;
   删: 
    drop database 数据库名;
   查:
    show databases;
  
  数据表:
   创建:
    create table 表名(
     id  int auto_increment primary key
     列名1  列类型 [not null/ null default ‘‘ ],
     列名2  列类型 [not null/ null default ‘‘ ],
     列名n  列类型 [not null/ null default ‘‘ ]
    )engine=Innodb charset=utf8;
   
    列类型:
     数值类型:
      tinyint
      samllint
      mediumint
      int
      bigint
     
      有符号 / 无符号 (unsigned)
     
     
     字符串类型:
      
      char varchar  text
         
     时间日期类型:
      datetime 年月日时分秒
   
   删除:
    drop table 表名;
   查询:
    show tables;
   
   修改:
    alter table 表名 change/modify 旧列名 新列声明;
    
    alter table 表名 add 新列声明;
    
    alter table 表名 drop 旧列名;
   
   
  数据行:
   
   增:
    insert  into 表名 (列1,列2, 列n) values (值1, 值2, ..., 值n);
   
   删除:
    delete from 表名 where id=12;; 
    truncate 表名;
   
   修改:
    update 表名 set name=‘xxxx‘;
    update 表名 set name=‘xxxx‘ where id=12;
    update 表名 set name=‘xxxx‘,age=12 where id=12;
   
   查询:
    select  * from 表名;
    select name, age from 表名;
  
  
 六.外键 (一对多)
  
  作用:
   1. 约束
   2. 节省空间
  
  create table department (
   id  int auto_increment primary key,
   depart_name varchar(32)  not null  default ‘‘,
   num int  not null default 0
  )engine=Innodb charset=utf8;
   
  create table userinfo ( 
   id  int auto_increment primary key,
   name varchar(32) not null default ‘‘,
   depart_id int not null  default 1,
   
   # constraint 外键名(fk_userinfo_depart) foreign key (列名(depart_id)) references 表名(department)(关联的列名(id)),
   # constraint fk_userinfo_depart foreign key (depart_id) references department(id)
  )engine=Innodb charset=utf8;
  
  
  ps:
   1. 不能将创建外键的语句单独拿出来
     
      alter table userinfo add constraint fk_userinfo_depart foreign key (depart_id) references department(id);
      alter table userinfo drop foreign key 外键名称(fk_userinfo_depart );
   
   2. 外键关联的时候, 必须关联的是表的主键ID
   
   3. 练习的时候, 将语句写在文本中, 然后考过去执行
   
   4. 主键索引 : 加速查找 + 不能为空 + 不能重复
   
   
 今日内容:

1.外键的变种

2.数据表操作

一. 外键的变种: (*********************************************************)
    
    1. 唯一索引:
     
     create table t5(
      id int,
      num int,
      unique(num)
     )engine=Innodb charset=utf8;
     
     作用: 
      num列的值不能重复
      加速查找
      
     create table t6(
      id int,
      num int,
      unique(id, num)
     )engine=Innodb charset=utf8;
     
     联合唯一索引作用: 
      num列和id列的值不能重复
      加速查找
     
     create table t6(
      id int,
      num int,
      unique(id, num......)
     )engine=Innodb charset=utf8;
  
  
    2. 一对多:
      
      部门表:
       id       depart_name
        1          公关部
        2          公共部
        3          保安部
      
      员工表:
       id    name  age   depart_id(外键)
       1     lxxx  12      2
       2     xxxx  13      1
       3     xxxx  13      2
       
    3. 一对一:
      
      用户表:
       id    name     age 
       1      zekai    23 
       2      eagon    34
       3      lxxx     45
       4      owen     83
  
      博客表:
       id         url          user_id  (外键 + 唯一约束)
       1       /linhaifeng       2
       2       /zekai     1
       3       /lxxx             3
       4       /lxxx             4
    
  
    4.  多对多:
      
      用户表:
       id name phone
       1 root1 1234
       2 root2 1235
       3 root3 1236
       4 root4 1237
       5 root5 1238
       6 root6 1239
       7 root7 1240
       8 root8 1241
       
      主机表:
      
       id hostname 
       1 c1.com 
       2 c2.com 
       3 c3.com 
       4 c4.com 
       5 c5.com

为了方便查询, 用户下面有多少台主机以及某一个主机上有多少个用户, 我们需要新建第三张表:
       user2host:
       
        id userid hostid
         1 1 1
         2 1 2
         3 1 3
         4 2 4
         5 2 5
         6 3 2
         7 3 4 
      创建的时候, userid 和 hostid 必须是外键, 然后联合唯一索引 unique(userid, hostid)
      
      Django orm 也会设计
      
  
  
  二. 数据行的操作:
    
    增:
     insert into 表名 (列名1, 列名2,) values(值1, 值2);
     insert into 表名 (列名1, 列名2,) values(值1, 值2),(值1,值2),(值n,值n);
     
     insert into 表名 (列名1, 列名2,) select 列名1, 列名2 from 表名;
     
    删除:
     delete from 表名;
     
     delete from 表名 where id > 10
     delete from 表名 where id < 10
     delete from 表名 where id <= 10
     delete from 表名 where id >= 10
     delete from 表名 where id != 10
     delete from 表名 where id = 10 and name=‘xxx‘;  and : 并且 两个条件都必须要成立
     delete from 表名 where id = 10 or name=‘xxx‘;   or :  或者 只要满足一个条件成立
    修改:
     update 表名  set name=‘zekai‘, age=23 where id > 10;
     
    查询:
     
     基本:
      select * from 表名;
      select name , age from 表名;
     
     高级:
      
      a. where 条件查询:
       select * from 表名 where  id=10;
       select * from 表名 where  id >10 and id<15;
       select * from 表名 where  id > 10;
       != : 不等与
       >= <=
       
       
       between and: 闭区间
        select * from t4 where id between 9 and 12;
       
       in: 在某一个集合中
        select * from t4 where id in (9,10,11....);
        
        
        select * from t4 where id in (select id from t3 where id between 2 and 4)
        
        是可以这样使用的, 但是不建议大家使用;
      
      b. 通配符:
       alex
       
       select * from 表 where name like ‘ale%‘  - ale开头的所有(多个字符串)
       select * from 表 where name like ‘ale_‘  - ale开头的所有(一个字符)

c.  限制取几条:
        
        select * from 表名 limit 索引偏移量, 取出多少条数据;
        
        
        select * from t3 limit 0, 10;  第一页
        select * from t3 limit 10, 10;  第二页
        
        page = input(‘page:‘)
        
         page    索引偏移量      数据量(offset)
           1         0              10
           2         10             10
           3         20             10
           4         30             10
          
           page   (page-1)*offset   offset
        
        分页核心SQL:
        
         select * from t3 limit (page-1)*offset, offset;
      
      d.  排序:
        
        order by
        
        降序:
         select * from t4 order by 列名 desc; descending
        
        升序:
         select * from t4 order by 列名 asc; ascending
      
        
        多列:
         
         create table t7(
         
          id int auto_increment primary key,
          num int not null default 0,
          age int not null default 0
         )charset=utf8;
         
         insert into t7 (num, age) values (2, 12),(3,13),(4, 12);
        
         select * from t4 order by num desc, name asc;
         
            如果前一列的值相等的话, 会按照后一列的值进行进一步的排序.
      
      e.  分组
        
        select age, 聚合函数(count(num)/sum(num)/max(num)/min(num)/avg(num)) from 表名 group by 列名;
        
        
        select age, avg(num) from t7 group by age;
        
        select age, count(num) from t7 group by age;
        
        select age, count(num) as cnt from t7 group by age;  显示别名 as
       
        having的二次删选:
         select age, count(num) as cnt from t7 group by age  having cnt>1;
         
        where 和 having的区别:
         1). having与where类似,可筛选数据
         2). where针对表中的列发挥作用,查询数据
         3). having针对查询结果中的列发挥作用,二次筛选数据, 和group by配合使用
      
      f.  连表操作
        select * from userinfo, department; (笛卡尔积)
        
        select * from userinfo, department where userinfo.depart_id=department.id;
       
        左连接:
        
         select * from userinfo left join department on userinfo.depart_id=department.id;
         左边的表全部显示, 右边没有用到不显示
        
        右连接:
         
         select * from userinfo right join department on userinfo.depart_id=department.id;
         右边的表全部显示, 左边没关联的用null表示
        
        内连接:
         左右两边的数据都会显示
        
        ps:
         a.只需要记住左连接 left join
         
         b.可以连接多张表 通过某一个特定的条件
         
     注意查询的顺序:
      select name,sum(score) from 表 where id > 10 group by score having age> 12  order by age desc limit 2, 10

原文地址:https://www.cnblogs.com/llx--20190411/p/11019739.html

时间: 2024-08-03 09:30:22

python42 1.外键的变种 2.数据表操作的相关文章

MySQL-5-唯一索引-外键的变种

唯一索引 唯一索引 联合唯一索引 外键的变种 博客园 FK不能去重,一个用户只有一个博客园 原文地址:https://www.cnblogs.com/c-x-m/p/9061546.html

【数据库】主键,外键,主表,从表,关联表,父表,子表

转自:https://www.2cto.com/database/201707/662425.html 一.前言 在数据库设计中,hibernate,iBatis等ORM框架的使用中经常听说主键,外键,主表,从表,关联表,父表,子表之类的术语,弄懂它们之前的区别与联系对于数据库设计和ORM框架的学习使用是非常有必要的. 二.概述 下面从数据库设计角度,ORM框架使用(以Hibernate为例),PowerDesigner软件以及实际业务角度进行一下介绍. (1) 数据库角度而言 主键:一般情况下

前端学数据库之数据表操作

× 目录 [1]准备工作 [2]创建数据表 [3]查看数据表[4]记录操作[5]记录约束[6]列操作[7]约束操作[8]修改列[9]数据表更名 前面的话 mysql数据库中的数据存储在被称为表(tables)的数据库对象中.表是相关的数据项的集合,它由列(字段)和行(记录)组成.下面将详细介绍数据表操作 准备工作 在进行数据表操作之前,需要先登录mysql服务器,创建一个数据库,并使用创建好的数据库 创建数据表 下面在db1数据库中创建数据表tb1 CREATE TABLE [IF NOT EX

mysql数据表操作&amp;库操作

首先登陆mysql:mysql -uroot -proot -P3306 -h127.0.0.1 查看所有的库:show databases; 进入一个库:use database; 显示所在的库:select database(); 开始进行数据表操作: 1,创建数据表:create table user( id smallint unsigned aotu_crement not null primary key,  //id,整型无符号位非空主键 idNum varchar(20) uni

MySQL5.7(三)数据表操作

概念在数据库中,数据表是数据库中最重要.最基本的操作对象,是数据存储的基本单位.数据表被定义为列的集合,数据在表中是按照行和列的格式来存储的.每一行代表一条唯一的记录,每一列代表记录中的一个域.1.创建数据表说明:数据表属于数据库,在创建数据表之前,应该使用语句"USE <数据库实例名>"指定操作的数据库实例. USE cuixiaozhao;#使用数据库实例-cuixiaozhao: 1 CREATE TABLE <表名>( 2 字段名1,数据类型 [列级别的

二 数据库、数据表操作

一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MySQL 5.5开始新增一个数据库:主要用于收集数据库服务器性能参数,记录处理查询请求时发生的各种事件.锁等现象 mysql: 授权库,主要存储系统用户的权限信息test: MySQL数据库系统自动创建的测试数据库 二 创建数据库 1 语法(help create database) CREATE DA

Django学习手册 - ORM 数据表操作

queryset 数据类型介绍 QuerySet与惰性机制 所谓惰性机制:表名.objects.all()或者.filter()等都只是返回了一个QuerySet(查询结果集对象),它并不会马上执行sql,而是当调用QuerySet的时候才执行. QuerySet特点: <1>  可迭代的 <2>  可切片 <3>  惰性计算和缓存机制 例: info=models.table_name.objects.all()[0:5] #切片 info= models.table

MySQL学习笔记之五 有关数据表操作

MySQL在创建表的时候,创建一个.frm文件保存表和列定义.索引存储在一个有.MYI(MYindex)扩展名的文件并且数据存储在有.MYD(MYData)扩展名的文件中.   一.用SHOW/ DESCRIBE语句显示数据表的信息 语法: SHOW TABLES [FROM db_name] [LIKE wild] or SHOW COLUMNS FROM tbl_name [FROM db_name] [LIKE wild] or SHOW INDEX FROM tbl_name [FROM

Python数据库操作 MySQL数据库与数据表操作#学习猿地

# MySQL数据库与数据表操作 + 数据库的操作 + 数据库创建 + 数据库删除 + 数据表的操作 + 数据表的创建 + 数据表的修改 (表结构) + 数据表的删除 ### 数据库的操作 #### 1.数据库的创建 ```mysql # 链接mysql数据库后,进入mysql后可以操作数据 # 1. 创建库 create database if not exists tlxy default charset=utf8: -- 1. 数据库 tlxy 如果不存在则创建数据库,存在则不创建 --