zoukankan      html  css  js  c++  java
  • MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作)

    MySQL的外键,修改表,基本数据类型,表级别操作,其他(条件,通配符,分页,排序,分组,联合,连表操作):
    a.创建2张表
    create table userinfo(nid int not null auto_increment primary key,
    name varchar(10),
    age int,
    part_nid int
    )engine=innodb default charset=utf8;

    create table part(
    nid int not null auto_increment primary key,
    caption varchar(20)
    )engine=innodb default charset=utf8;

    添加一些数据,创建一个中间表约束part_nid和part表,这就是外键
    mysql> select * from userinfo;
    +-----+------+------+----------+
    | nid | name | age | part_nid |
    +-----+------+------+----------+
    | 1 | h | 19 | 2 |
    | 2 | hh | 19 | 2 |
    | 3 | hhh | 19 | 2 |
    +-----+------+------+----------+
    3 rows in set (0.00 sec)

    mysql> alter table userinfo add constraint fk_u_p foreign key userinfo(part_nid) references part(nid);
    Query OK, 3 rows affected (0.88 sec)
    Records: 3 Duplicates: 0 Warnings: 0

    然后我们在进行数据的插入,这时候由于约束条件的存在,所以我们就无法进行插入part_nid为100的数据
    mysql> insert into userinfo(name,age,part_nid) values('b',19,100);
    ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`userinfo`, CONSTRAINT `fk_u_p` FOREIGN KEY (`part_nid`) REFERENCES `part` (`nid`))
    mysql>

    b.外键foreign key ,一对多(在创建表之前就知道有关联的列和表)
    总结:
    2张表的建立约束
    --约束
    c.修改表(列的增删查改)
    添加列:alter table 表名 add 列名 类型;
    删除列:alter table 表名 drop column 列名;
    修改列:
    alter table 表名 modify column 列名 类型;--类型
    alter table 表名 change 原列名 新列名 类型;--列名,类型
    添加主键:
    alter table 表名 add primary key(列名);
    删除主键:
    alter table 表名 drop primary key;
    alter table 表名 modify 列名 int,drop primary key;
    添加外键:
    alter table 从表 add constraint 外键名字(形如:fk_从表_主表) foreign key 从表(外键字段)references 主表(主键字段);
    删除外键:
    alter table 表名 drop foreign key 外键名称;
    d.基本的数据类型:数值,时间和字符串
    数值:
    bit 二进制
    tinyint
    smallint
    int
    bigint
    --范围不一样

    decimal:十进制小数,精确的
    FLOAT
    DOUBLE

    字符串:
    char(定长)
    create table tb13(n char(7))
    不管怎样都要占用7个字符的空间,查找速度快,浪费内存空间
    varchar(变长)
    varchar是最大占用7个字符的空间,查找速度慢,节省空间
    text
    mediumtext
    longtext
    二进制数据:
    TineyBlob Blob MediumBlob LongBlob
    #上传文件
    #强制二进制文件
    #将上传的文件保存在硬盘
    时间:
    DATE: YYYY--MM--DD
    TIME: HH:MM:SS
    YEAR: YYYY
    DATETIME: YYYY--MM--DD HH:MM:SS
    TIMESTAMP: YYYMMDD HHMMSS

    enum:
    表级别的操作*****
    select * from tb1;
    #增
    insert into biao2(name,age) values('bob',234);#可以插一条数据,
    insert into biao2(name,age) values('lizebo',26),('eric',30);#可以插多条
    insert into biao1(name,age) select name,age form biao2;#可以把另外一个表中的内容插入到这个表中
    #删
    delete from biao2;#全清空
    delete from biao2 where id=1 and name='alex';#按条件删除
    #查
    select * from 表;#查看表的全部内容,select *这种操作效率比较低,最好的方式就是写一遍,效率高。
    select * from 表 where id>1;#按条件查询
    select nid,name,gender as gg from 表 where id>1;
    #改(跟新)
    update 表 set name='alex' where id>1;#跟新设置某项内容


    #其他
    a.条件
    select * from 表 where id>1 and name != 'alizbeo' and nid=12;
    select * from 表 where id between 5 and 16;#在...之间的数据
    select * from 表 where id in (11,22,33);#是否在这个元组中
    select * from 表 where id not in (22,33,44);
    select * from 表 where id in (select * from biao2);
    b.通配符(模糊搜索)
    select * from 表 where name like 'alex%';#alex开头的所有(多个字符串)
    select * from 表 where name like 'ale_';#_表示ale开头的所有(一个字符)
    c.分页
    select * from 表 limit 5;#前5行
    select * from 表 limit 0,4;#从0行开始取4行
    select * from 表 limit 4 offset 0;#从0开始取4行,这个比较常用
    d.排序
    select * from 表 order by 列 asc;#根据‘列’从小到大排序
    select * from 表 order by 列 desc;#根据‘列’从大到小排序
    select * from 表 order by 列1 desc,列2 asc;#根据‘列1’从小打到排序,如果相同则按列2从小到大排序
    e.分组(重要)
    select num form 表 group by num;
    select num,nid from 表 group by num,nid;
    select num,nid from 表 where nid>10 group by num,nid order nid desc;
    select num,nid ,conut(*),sum(score),max(score),min(score) form 表 group by num,nid;

    当我们对聚合条件进行查询时候,我们需要使用having
    select num from 表 group by num having max(id)>10;
    特别的:group by 必须在where 之后,order by之前
    列子:我们有这样的一个userinfo表:
    mysql> select * from userinfo;
    +-----+--------+------+----------+
    | nid | name | age | part_nid |
    +-----+--------+------+----------+
    | 1 | h | 19 | 2 |
    | 2 | hh | 19 | 2 |
    | 3 | hhh | 19 | 2 |
    | 4 | eirc | 23 | 1 |
    | 5 | lizebo | 23 | 1 |
    | 6 | bobli | 24 | 1 |
    | 7 | jim | 21 | 3 |
    | 8 | jams | 22 | 3 |
    +-----+--------+------+----------+
    我们需要进行分组查询,
    mysql> select part_nid, min(nid),max(nid),count(nid) from userinfo group by part_nid;
    +----------+----------+----------+------------+
    | part_nid | min(nid) | max(nid) | count(nid) |
    +----------+----------+----------+------------+
    | 1 | 4 | 6 | 3 |
    | 2 | 1 | 3 | 3 |
    | 3 | 7 | 8 | 2 |
    +----------+----------+----------+------------+
    3 rows in set (0.00 sec)
    这里的part_nid就是把相同的nid分成了3类,1,2,3类
    min(nid)==最小的nid,max(nid)==最大的nid,count(nid)==总数nid
    f.联合
    组合,自动处理组合
    select nid from 表 union select nid from 表2;#这是去重数据的
    select nid from 表 union all select nid from 表2;#这是去重的
    g.连表操作
    第一种连表操作:
    如果有一张userinfo和part表,并且这2张表式通过外键关联的,我们要查询这2张表中所有数据
    mysql> select * from userinfo;
    +-----+--------+------+----------+
    | nid | name | age | part_nid |
    +-----+--------+------+----------+
    | 1 | h | 19 | 2 |
    | 2 | hh | 19 | 2 |
    | 3 | hhh | 19 | 2 |
    | 4 | eirc | 23 | 1 |
    | 5 | lizebo | 23 | 1 |
    | 6 | bobli | 24 | 1 |
    | 7 | jim | 21 | 3 |
    | 8 | jams | 22 | 3 |
    +-----+--------+------+----------+
    8 rows in set (0.00 sec)

    mysql> select * from part;
    +-----+---------+
    | nid | caption |
    +-----+---------+
    | 1 | IT |
    | 2 | TI |
    | 3 | SA |
    | 4 | DEV |
    +-----+---------+
    4 rows in set (0.00 sec)
    如果我们这样查询连表会产生笛卡儿积,表1中的每条数据都会到表2中进行查询4次。
    mysql> select name,age,part_nid from userinfo,part;
    +--------+------+----------+
    | name | age | part_nid |
    +--------+------+----------+
    | h | 19 | 2 |
    | h | 19 | 2 |
    | h | 19 | 2 |
    | h | 19 | 2 |
    | hh | 19 | 2 |
    | hh | 19 | 2 |
    | hh | 19 | 2 |
    | hh | 19 | 2 |
    | hhh | 19 | 2 |
    | hhh | 19 | 2 |
    | hhh | 19 | 2 |
    | hhh | 19 | 2 |
    | eirc | 23 | 1 |
    | eirc | 23 | 1 |
    | eirc | 23 | 1 |
    | eirc | 23 | 1 |
    | lizebo | 23 | 1 |
    | lizebo | 23 | 1 |
    | lizebo | 23 | 1 |
    | lizebo | 23 | 1 |
    | bobli | 24 | 1 |
    | bobli | 24 | 1 |
    | bobli | 24 | 1 |
    | bobli | 24 | 1 |
    | jim | 21 | 3 |
    | jim | 21 | 3 |
    | jim | 21 | 3 |
    | jim | 21 | 3 |
    | jams | 22 | 3 |
    | jams | 22 | 3 |
    | jams | 22 | 3 |
    | jams | 22 | 3 |
    +--------+------+----------+
    32 rows in set (0.00 sec)
    消除笛卡儿积:
    第一种方式:
    mysql> select name,age,part_nid from userinfo,part where userinfo.part_nid=part.nid;
    +--------+------+----------+
    | name | age | part_nid |
    +--------+------+----------+
    | h | 19 | 2 |
    | hh | 19 | 2 |
    | hhh | 19 | 2 |
    | eirc | 23 | 1 |
    | lizebo | 23 | 1 |
    | bobli | 24 | 1 |
    | jim | 21 | 3 |
    | jams | 22 | 3 |
    +--------+------+----------+
    8 rows in set (0.06 sec)
    其实就是按关联条件查询!
    第二种方式:
    mysql> select * from userinfo left join part on userinfo.part_nid=part.nid;
    +-----+--------+------+----------+------+---------+
    | nid | name | age | part_nid | nid | caption |
    +-----+--------+------+----------+------+---------+
    | 4 | eirc | 23 | 1 | 1 | IT |
    | 5 | lizebo | 23 | 1 | 1 | IT |
    | 6 | bobli | 24 | 1 | 1 | IT |
    | 1 | h | 19 | 2 | 2 | TI |
    | 2 | hh | 19 | 2 | 2 | TI |
    | 3 | hhh | 19 | 2 | 2 | TI |
    | 7 | jim | 21 | 3 | 3 | SA |
    | 8 | jams | 22 | 3 | 3 | SA |
    +-----+--------+------+----------+------+---------+
    8 rows in set (0.00 sec)
    可以用inner join on其实对left join on进行了一个null的过滤。
  • 相关阅读:
    正则表达式(十四)——找出某一个网页内部的所有的邮箱
    正则表达式(十三)——分组
    正则表达式(十二)——字符串的替换
    正则表达式(十一)——find和lookingAt
    查看隐藏文件夹
    SpringBoot 热部署
    oracle dmp文件泵导入
    python -爬虫-pycrul安装问题
    阿里云https tomcat配置
    jar包下载
  • 原文地址:https://www.cnblogs.com/lizeboLB/p/7783882.html
Copyright © 2011-2022 走看看