当前位置:Gxlcms > 数据库问题 > MYSQL

MYSQL

时间:2021-07-01 10:21:17 帮助过:5人阅读

select * from table_name select name1,name2 from table_name 获取某一列 select name,JS+10,Django+10,Flask from table_name +10显示 select name as 姓名 from table_name 筛选XXX select name from table_name where name>80 select name from table_name where name between 90 and 100 select name from table_name where name like "a%" select name,JS from table_name where JS>80 order by JS 默认升序 select name1,data1+data2+data3 as 总成绩 from table_name order by 总成绩 select JS as JS成绩 from table_name where JS成绩>70 不可以! 执行语句的顺序 from where select group by hacing order by select * from table_name group by name 分组哦`~ select name,sum(JS) from table_name group by having sum(JS)>150 select * from table_name having id=3 若未分组 where实现更好

聚合函数

    count(列名)
    sum(列名)
    avg(列名)
    max(列名)
    min(列名)
    
    limit
        select * from table_name limit 3
        select * from table_name limit 1,4    取索引

正则: 相对于此 like更快

    select * from table_name there 列表名 recexp ^yu
    select * from table_name there 列表名 recexp yu$

外键: 作为外键一定要和关键数据类型保持一致

创建主表:
create table classch(
        id int primary key auto_increment,
        name varchar (20),
        age INT,
        is_marriged boolean
        );
        
insert into classch (name,age,is_marriged) values ("冰冰",52,0),
 ("丹丹",34,0),
 ("歪歪",32,0),
 ("杉杉",28,0),
 ("小雨",61,0);
创建子表:
create table student(
 id int primary key auto_increment,
 name varchar (20),
 charger_id tinyint) engine=innodb;

insert into student (name,charger_id) values ("alex1",2),
 ("alex2",3),
 ("alex3",3),
 ("alex4",4),
 ("alex5",1),
 ("alex6",1),
 ("alex7",2);
创建外键:
create table student(
 id int primary key auto_increment,
 name varchar(20),
 charger_id int,
 foreign key (charger_id) peferences classch(id)
 ) engine=innodb;
 
 
修改id
update student set charger_id=4 where id=1 or id=7;

添加
insert into student (name,charger_id) values ("alex8",2)

 

多表格查询
连接查询:
内连接:inner join
内连接:left join right join
全连接:full join

create table tableA (id int primary key,name varchar(20));

create table tableB(
        id int primary key,
        name varchar(20),
        tableA_id int);
    
insert into tableA values(1,"alvin");
insert into tableA values(2,"xialv");
insert into tableA values(3,"yuan");

insert into tableB values(1,"小雨",1);
insert into tableB values(2,"小小",2);
insert into tableB values(3,"冰冰",4);


select * from tableA,tableB;

select tableA.id,tableA.name,tableB.name from tableA,tableB 
    where tableA.id=tableB.tableA_id
内连接
select * from tableB inner join tableA on tableB.tableA_id=tableA.id

左右连接:以X为基准 匹配到的显示 匹配不到显示null

select employee.emp_name,department.dept_name from department left join
employee on employee.dept_id  = department.dept_id;

复合查询

子查询
select * from employee where dept_id in (1,2,3,4);
带in的子查询
select dept_id from employee where dept_id in 
            (select dept_id from department);

select dept_id,dept_name from department where dept_id in
            (select distinct dept_id from employee where age>=25);
exists关键字的子查询
select dept_id,dept_name from department where exists
            (select dept_id from employee where age>=25);
    后面如果是true 则执行where之前 否则不执行

索引语法
    索引 创建和维护消耗很多时间和磁盘空间 但查询速度大大提高
    
create table test1(
    id int primary key auto_increment,
    name varchar(20),
    salary int default 1000);

insert into test1(name) values ("111"),
                    ("222"),
                    ("333");

id 本身就是一个唯一的索引哦


正儿八经:
create table table_name(
        字段名1 数据类型,
        字段名2 数据类型,
        [unique | fulltext | spatial ] index | key
        [索引名] (字段名[length]) [ASC | desc]
        );
创建普通索引示例:
create table emp1(
    id int,
    name varchar(20),
    resume varchar(50),
    index index_emp_name (name)
    );
创建全文索引示例:
create table emp3(
    id int,
    name varchar(20),
    resume varchar(50),
    fulltext index index_emp_name (name)
    );
创建多列索引示例:
create table emp4(
    id int,
    name varchar(20),
    resume varchar(50),
    index index_emp_name (name,resume)
    );

任务描述:表user1中有id,name,code;表user2中有id,name,code;

目的:对比user1和user2两张表数据并将user2中名称和user1相同的code写入到user1中;

实现sql:

UPDATE user1 a,user2 b SET a.name=b.name WHERE a.code = b.code

事务的四个特性:
原子性,一致性,隔离性,持久性ACID
关于一些术语:
开启事务 start transaction
事务结束 end transaction

提交事务 commit transaction
回滚 rollback transaction
和事务有关的两条语句
commit rollback
开启和结束的标志:
开始:任何一条DML语句执行,标志开始
结束:提交或者回滚

在事务进行过程中,未结束之前,DMl语句不会更改底层数据库文件中的数据
只是将历史操作记录一下,在文件中完成记录,只有在事务结束的时候,
而且是成功的结束才会修改底层一啊光盘的数据

默认情况下自动提交,但是实际情况可以手动开启

一种方式
start transaction; insert into tablename (name) values (XXX); commit; start transaction; DML; DML; rollback; 关闭自动提交的第二种方式;
set autocommit = off; set session autocommit = off; 此方式只对当前终端有效

事务的四个特性之一:隔离性,
读未提交 read uncommited
读已提交 read committed
可重复读 repeatable read
串行化 serializable
读未提交:事务A未提交,事务B可以读取到,这里读取到的叫做脏数据
读已提交:避免脏数据,但是会导致“不可重复读取”,产生了矛盾~~Oracle默认
可重复读:事务A提交之后的事务B读取不到,数据B可重复读取,导致”幻象读“MYSQL默认
串行化:A在操作时候,B只能排队等待,吞吐量太低,用户体验不好,用的少

*****************索引********
表查询,第一种,全表扫描,第二种,通过索引检索
数据庞大,很少用DML语句,经常用where找它,可加索引

三范式
要求有主键,并且每一个字段原子性不可再分
要求所有非主键字段完全依赖主键,不可产生部分依赖
要求所有非主键不可传递依赖于主键

一对一:
分两个,共享主键
分两张,分两张,外键唯一
一对多:
在多的一方添加外键,外键引用1的主键字段
多对多:
分三张表,学生表,课程表,学生学课表存学生和课程关系信息

实例~~

https://blog.csdn.net/qq_22339269/article/details/84549751

MYSQL

标签:esc   就是   ESS   varchar   关系   limit   and   set   back   

人气教程排行