mysql基础1

来源:互联网 发布:淘宝快递面单打印 编辑:程序博客网 时间:2024/05/10 06:50
 

Mysql  数据库 ---》 专门存储数据的软件

CoreJava
     1 内存  Student List  临时数据
  2 文件                持久化的数据
      student.name=suns;student.age=10;student.birhtday=1900-10-11
   student.name=liyi;student.age=10;student.birhtday=1900-10-11
   student.name=zzq;student.age=10;student.birhtday=1900-10-11
        弊端 : 
            1 用户通过程序获得数据麻烦
   2 数据安全
  
  3 数据库             
         1 持久化的数据
         2 用户通过程序获得数据方便
   3 数据安全
   
     4 数据库存储数据的方式
       表格(Table) 一种特定类型的数据
                  存储一张表中
   
    行(Row) == 记录 === 一个特定对象的数据
    列(Column) === 属性 === 一个特定对象的属性
   
  5  关系型数据库  
     概念: 以表格的方式来存储数据的数据库
         叫做关系型数据库
     
     MySQL  Oracle DB2 SqlServer
        Access Postgre SQLlit   
  
  对象型数据库 ---> 对象来存储数据
  

     MySQL ---> SUN ---> Oracle 
  优势 开源 免费 (*) 
  MySQl 社区版
        企业版
    
        1 安装  
           安装包释放到c根目录

安装MySQL数据库
    (1)开始-->运行-->cmd-->cd \
    -->cd C:\mysql-5.5.11-win32\bin
    (2)安装
      mysqld -nt-install mysql
    (3)卸载
      mysqld -remove mysql
    (4)配置环境变量
     鼠标右击-->我的电脑-->属性-->高        级-->环境变量-->系统变量-->
     Path 
     .;C:\mysql-5.5.11-win32\bin;
    (5)使用
     开始-->运行-->cmd-->cd \
    -->mysqld -nt-install mysql
    (6)启动服务
       net start mysql
    (7)停止服务
       net start mysql
    (8)连接数据库
       mysql -u root -p
    测试 select version();

  
      使用:
        client ---->  Server
        登陆Mysql
         命令行---》cmd ----》mysql -uroot -p; 
        退出Mysql
         exit | \q  
  
      show databases 显示服务器中存在的数据库
   use 数据库名字
         show tables  显示对应数据库中的表格名字
         help \h 提示mysql常用命令
   source .sql

  查询   SQL 标准查询语言
  命令  
     select first_name
     from employees ;
    
     select first_name,last_name
     from employees;
  
  select employee_id,
         first_name,
   last_name,
   email,
   phone_number,
   hire_date,
   job_id,
   salary,
   commission_pct,
   manager_id,
   department_id
   from employees;  
  
   select * from employees ;

   原因: 1  * SQL可读性差  
          2  * sql执行效率差
  
--------------------------------------
 条件查询
        概念: 按照某个条件到数据库中查询对应的数据
      
       a)  select *
           from employees
           where first_name = 'Steven';
  
     select *
     from employees
     where first_name = 'Lex';
    b) and关键字   和 一起 并且
       or关键字    或者 
    not关键字  不 非 
   
       查询 employee_id=100 并且 first_nam Steven;
         
       select *
    from employees
    where employee_id=100 and first_name='Steven';
   
    查询  first_name Lex  Steven
   
    select *
    from employees
    where first_name = 'Lex' or first_name='Steven';
   
  c)  = > < <> >= <=
      查询工资>9000这些员工的信息
      select *
   from employees
   where salary <> 9000;
                <
       =
       <>
         
        查询:工资大于9000 并且 first_name 等于 Lex     
  
        select *
        from employees
        where salary<9000 or first_name = 'Lex';  
  
  d) between and
           not between and   
     在一个闭区间范围中进行查询
     应用
  
  工资大于等于3000 并且 工资小于等于8000 员工信息
  select *
        from employees
        where salary >= 3000 and salary<= 8000;
        3000--8000
  
  select *
        from employees
        where salary not between 3000 and 8000;
  
  e) in
     表达在一个可选范围的或者关系 
     not in
    
     工资为 3000 8000 120000
     select *
     from employees
     where salary=3000 or salary=8000 or salary=12000;
    
       select *
     from employees
     where salary not in (3000,8000,12000);
  
        f) like 模糊查询    
     % 0---多个任意字符
     _ 1个任意字符
    
     select *
     from employees
     where first_name like 'S_';
    
     1) first_name 包含 s字母
        select *
     from employees
     where first_name like '%s%';
                          
     2) first_name 5个字母 组成
        select *
     from employees
     where first_name like '_____';
    
    g)  is null
        is not null
     作用:判断一列的值是否为空
   
             没有提成的员工信息
             select *
             from employees
             where commission_pct is not null;
    
------------------------------------------------
SQL 命令中的排序  
    Order By 列名 asc 升序 (默认)
                  desc 降序 
  SQL在进行查询时 默认没有进行排序 
 
  select *
  from employees
  order by salary desc;
 
  特殊 1) null 在mysql数据库中是极小值
                 oracle数据库中是极大值
       2) 字符串排序 字母逐个对比的方式进行排序
    3) 多列排序
 
       select *
    from employees
    order by first_name asc , employee_id desc;
 

 
------------------------------------------------
* mysql 函数
  CoreJava 方法
  Mysql 函数 必须写在 SQL语句中 方可使用
 
    select 列名 
 from 表
 
 
 java  add(int i,int j);
       System.out.println();
 
 
    select char_length('suns');
 
    select length('中s');

字符串函数
 * char_length('suns')  ---> 计算字符串长度
  length('中s')  ---> 计算字符串占用字节数
 * concat('suns','shuai') -----> 连接多个字符串
  
  select concat(first_name,last_name)
  from employees;
  
  concat_ws(';','sun','shuai')  -----> 多个字符串 按照一个分割符连接在一起
  
       -
  'sun' 'shuai' ----> sun-shuai
  
 * instr('sunshuai','un') --> 判断一个字符串中是否包含另外一个字符串
  
  sunshuai  un
  
  select instr('sunshuai','jian');
  返回值 存在 第一次出现的位置
         不存在 0
  
  lower('') ----> 把字符串转换成 小写
  upper('') ----> 把字符串转换成 大写
  
 * left() ----> 从左向右查询特定长度的字符
  'suns'
  select left('suns',3);
  
  
 * right() ---> 从右向左查询特定大小的字符
  select right('suns',2);
  
  lpad() ----> 如果从左向右获得的字符个
               数超出原有字符串长度,可
      以应用特定字符占位
  'suns' 8  "@"
         ####suns  
  
  select lpad('suns',8,'#');
  
  rpad()
  
  select rpad('suns',8,'#');
  
  trim() ---> 剔除2边空格字符
  
  select trim('         s         v        ');
  
  
 * substring() ----> 截取字符串
  
  'suns'
  uns
  select substring('suns',2);
  
  un                     其实位置,长度
  select substring('suns',2,2);
  
  
  repeat() ----> 把一个字符串重复显示多次
  
  select repeat('suns',3);
 * replace()  ----> 把一个原有字符串中的内容 替换成新串
                         原  新
        select replace('suns','u','j'); 
  
 * reverse() ---> 翻转一个字符串
   
     select reverse('suns');
  snus
  
  space(10) ----> 添加空格
  
  
  
数字函数
  
  floor()    去小数
  round()    四舍五入
  TRUNCATE()
  truncate()
日期函数

  NOW()  返回当前的日期和时间
  CURDATE()  返回当前的日期
  CURTIME()  返回当前的时间
  DATE(时间)  提取日期或日期/时间表达式的日期部分
  TIME(时间)  提取日期或日期/时间表达式的时间部分
  EXTRACT(摘取)(字段 From 日期)  返回日期/时间按的单独部分
    字段的合法值:
       MICROSECOND
       SECOND
       MINUTE
       HOUR
       DAY
       WEEK
       MONTH
       QUARTER
       YEAR
       SECOND_MICROSECOND
       MINUTE_MICROSECOND
       MINUTE_SECOND
       HOUR_MICROSECOND
       HOUR_SECOND
       HOUR_MINUTE
       DAY_MICROSECOND
       DAY_SECOND
       DAY_MINUTE
       DAY_HOUR
       YEAR_MONTH
  DATE_ADD(日期, INTERVAL 数量 字段)  
    
  DATE_SUB(日期, INTERVAL 数量 字段)
  DATEDIFF(日期1, 日期2)  
  DATE_FORMAT(日期, 格式)
    格式字符:
        %a 缩写星期名
        %b 缩写月名
        %c 月,数值
        %D 带有英文前缀的月中的天
        %d 月的天,数值(00-31)
        %e 月的天,数值(0-31)
        %f 微秒
        %H 小时 (00-23)
        %h 小时 (01-12)
        %I 小时 (01-12)
        %i 分钟,数值(00-59)
        %j 年的天 (001-366)
        %k 小时 (0-23)
        %l 小时 (1-12)
        %M 月名
        %m 月,数值(01-12)
        %p AM 或 PM
        %r 时间,12-小时(hh:mm:ss AM 或 PM)
        %S 秒(00-59)
        %s 秒(00-59)
        %T 时间, 24-小时 (hh:mm:ss)
        %U 周 (00-53) 星期日是一周的第一天
        %u 周 (00-53) 星期一是一周的第一天
        %V 周 (01-53) 星期日是一周的第一天,与 %X 使用
        %v 周 (01-53) 星期一是一周的第一天,与 %x 使用
        %W 星期名
        %w 周的天 (0=星期日, 6=星期六)
        %X 年,其中的星期日是周的第一天,4 位,与 %V 使用
        %x 年,其中的星期一是周的第一天,4 位,与 %v 使用
        %Y 年,4 位
        %y 年,2 位
  STR_TO_DATE(字符串, 格式)
  LAST_DAY(日期)   

 

 

 

 

 

原创粉丝点击