mysql 基础3

 

日期函数
  now()
  curdate()
  curtime()
  month()
  year()
  date_format  
  date_add(日期,interval xx 单位)
  date_sub()
  extract(year from 日期)
  datediff()
  last_day()
  str_to_date()

数字函数
  floor()
  round()
  trancate()

null相关的函数
  1) is null is not null
  2) null进行运算时
     ifnull(arg1,arg2);
 
聚合函数
   avg() sum() max() min() count()

group by

having
   区别 where 执行效率  条件判断语句 不能聚合函数
        having 执行差   条件判断语句 聚合函数

 sql 书写  select from where group by having order by
        执行  from where group by having order by  
  
子查询
   where 
   from (select * from employees)
  
多表联合查询
   1 inner join 
     select xxxx
  from table_a
  inner join table_b
  on xx=xx
   2 left outer join
    
   3 right outer join
  
   4 full join
-------------------------------------------
作业: 
   分析思路
  
---------------------------------------------
Mysql
  DQL  Data Query Language
    sql
  DDL  Data Define Language 
     create drop  alter
  作用:创建表 (创建库)
        修改表
     删除表  (删除库)
 
  DML  Data Modify Language
     insert delete update 
  TCL  Transaction Control Language
     commit rollback 

 ----------------------------------
 create database  名字
 作用:创建mysql中的数据库
 create database if not exists  名字
 create database if not exists suns;
 
 创建数据库的表
 create table t_emp
 1 列名  a)有含义  b) 不能使用mysql中的关键字
 2 列的数据类型
    a)  数字类型
        int  --- 整数
     numeric(10) ---  整数 10位
     numeric(5,2)---  123.22
     double  ----  小数
   
    b)  字符串类型
        char(5)   定长字符串 '12345'
                          '123'
   一旦定义 就默认按照 声明内容
            开辟空间    
        varchar(5) 可变长字符串
                 '12345'
        '123'
   一旦定义 当实际字符串小于声明长度时
            默认改变空间

           普遍 varchar
     确定定长字符串的时候 char

     注意:oracle varchar2
    
   
    c)  日期类型
          date  存储 年月日
    datetime  存储 年月日 十分秒
   
    d)  大数据类型
           大2进制类型
         longblob  4GB   照片 .exe .class doc docx
     大文本类型
     text      20亿字符
   

       create table t_suns(
      列名 类型,
      列名 类型
    );
   
    create table t_suns(
      name varchar(5),
   password varchar(6),
   birthday date ,
   age int
    );
   
       insert into t_suns (name,password,birthday)
       values ('suns1','1234',now());

       建表中的问题
     1 约束  (constraint)
    对所在表中存储在列中的数据要求
  
  2 主键约束 primary key (pk)
    概念 唯一标示 id (identity)
    主键列 唯一 不可重复
  
    create table t_id(
      id int primary key 
    );
   
    insert into t_id (id)
    values(1);
   
  
  3 非空约束 not null (nn)
    概念 当前列 不允许 null插入
   
    create table t_nn(
      id int primary key,
      name varchar(2) not null
    );
   
    insert into t_nn(id,name)
    values (1,'su');
   
  4 唯一约束 unique (uk)
  
    create table t_uk(
      id int primary key,
   name varchar(12) unique
    )
   
    insert into t_uk (id,name)
    values (2,'suns');
   
  
  5 检查约束 check (ck)
    作用:限定列中出现的数据
          必须是符合某种要求
   
    constraint 名字 check(sex xxxx)
   
    create table t_ck(
      id int primary key,
   sex char(1),
      constraint ck_check check(sex in ('M','F'))
    );
   
    insert into t_ck (id,sex)
    values (2,'S');
  
  
    oracle:
     create table t_ck(
      id integer primary key,
   sex char(1) check(sex in ('M','F'))
     );
  
  
  
  
  6 外键约束 foreign key (fk)
     外键约束 多表操作
     外键 : 一张表中主键的内容
             充当另外一张表的内容
      emp 表中的 deptno外键
   
   父表
   create table t_dept(
     deptno int primary key,
     dname varchar(12)
   );
   子表
   create table t_emp(
     empno int primary key,
                          ename varchar(12),
                          deptno int, 
     constraint dept_fk
     foreign key (deptno)
     references t_dept(deptno));
   );
   
  emp
    empno ename sal deptno
     001   suns 100  10
     002   liyi 200  20
     003   lyc  300  30
    
    
  dept
    deptno dname
      10   tranning
   20   dev
   30   game

  7 复合约束
  
    create table t_com(
      id int primary key,
   name varchar(12) not null unique
    );
   
    特例
    create table t_com_2(
      id int not null unique
    );
   
  
    t_person
    id  name  sex
     1  sun   M
     2  wang  F

       8 通过查询语句创建表
      create table temp_emp
   as select * from emp;
  
         注意 不复制约束
  
-----------------------------------------
        drop database 名字
        drop table 名字
  
        drop table temp_emp;

 

 

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值