1-数据库基础01

一、基础

  1. 主键:最好直接序列号no作为主键

    id:唯一(unique),但是不适合作为主键

    唯一约束:不影响查询速度,与索引关联

  2. 数据完整性

    实体完整性:主键
    参照完整性:外键可以找到主表的主键
    用户定义完整性:约束

  3. 范式

    第一范式 1NF:属性不可再分

    第二范式 2NF:每一非主属性都完全依赖主键–>表的纯洁性

    第三范式 3NF:每一非主属性都不传递依赖于关键字

二、SQL语言:DDL、DML、DCL

SQL语言是结构化查询语言,不区分大小写

  1. DDL(数据定义语言, 对象是结构,对数据库、表、视图属性的操作)

    create、drop、alter语句

    1. 新建数据库 create database

       create database school default character set utf8 collate utf8_general_ci;
      
    2. 删除数据库 drop database

       drop database if exists school;
      
    3. 新建表 create table

       create table student(
       	id int(11) primary key,
       	sno varchar(12) not null unique,
       	password varchar(20) not null,
       	sname varchar(50),
       	ssex bit,
       	sage int,
       	sdept varchar(255),
       	sprovince varchar(255),
       	scity varchar(255),
       	sstreet varchar(255),
       	sbirthday date,
       	regist_time datetime,
           foreign key(ssex) references sex(id)
       );
      
    4. 删除表 drop table

       drop table if exists student;
      
    5. 修改表结构 alter table

       alter table student drop foreign key student_fk_1;
       alter table student add foreign key(ssex) references sex(id);
      
       alter table student unique(sno,cno);
       alter table student index name;
      
  2. DML(数据操控语言,对象是数据)

    insert、update、delete、select语句

    1. 插入数据 insert into user() values();

      自增长数据允许中断,可以不写(默认增加)或者可以跳(按给的数值)

       insert into user(name,money,icon)
       values("totio",12000",null)
      

      获取当前时间 now()

    2. 修改数据 update user set col_name = value where condition;

       update user set icon = 'user.jpg'
      
       update user set money = 5000 where name = 'lily21'
      
    3. 删除数据 delete from user where condition;

       delete from user where name = 'ttit'
      
    4. 查询数据 select

       【下面详细】
      
  3. DCL(数据控制语言,对象是角色、权限)

三、数据类型

  1. int
  2. date 日期
  3. datetime 日期+时间
  4. varchar 变长字符串,不会补0(char定长,不足补0)–长度为字节(8bit)
  5. TEXT 文本
  6. LONGTEXT 长文本
  7. DEC/DECIMAL(p,d) 货币,p位总长度,d位小数
  8. bit 位–存性别

四、约束和数据完整性

  1. 主键
  2. 外键
  3. unique
  4. default
  5. not null
  6. auto_increment

五、select语句–单表查询

  1. 查询若干列

     select * from student;
    
     select id,sno,sname,2019-sage as birth_year from student;
    
     select lower(sname) from student;
    
  2. 去重 distinct

     select distinct sdept from student
    
  3. 确定范围 between and,not between and

     select * from student where sage>=25 and sage<=30;
    
     select * from student where sage between 25 and 30;
    
  4. 确定集合 in,not in

     select * from student where sdept in ('数学系', '中文');
    
     select * from student where sdept='数学系' or sdept='中文';
    
  5. 空值 is null,is not null

  6. like,not like

    % 通配符 _ 一个字符 __ 两个字符

     select id,sname from student where sname like '李%'
    
     select id,sname from student where sname like '李_';
    
     select name from student where sname like '%iphone%';
    
  7. not and or

  8. 排序 order by(asc升序,desc降序)

     select id,sname,sage
     from student
     where sage between 25 and 30
     order by sage desc;
    
  9. 聚合函数

    1. count()

       select 'student_number',count(id) as 'count' from student;
      
       select count(distinct sdept) from student;
      
    2. sum()

       select sum(grade) from sc where cno=3;
      
    3. avg()

       select avg(sage) from student;
      
    4. min()

       select min(grade) from sc;
      
    5. max()

       select max(grade) from sc;
      
  10. 分组 group by having condition

    select ssex,avg(sage)
    from student
    group by (ssex);
    
    select cno,sum(grade)
    from sc
    group by cno;
    
    select ssex,count(*)
    from student
    where sage between 20 and 30
    group by ssex;
    
    select cno,avg(grade) as avg
    from sc
    group by cno having avg > 90
    order by avg desc;
    
  11. limit start,total(从start开始,提取total行。 从0开始)

    –分页

    select com_name,avg(max_salary) as avg_max
    from jobs
    group by com_name
    order by avg_max desc
    limit 0,3
    

你可能感兴趣的:(mysql,数据库基础,DML,DDL,Mysql)