• mysql总结


    mysql 介绍

    (1)   mysql数据库是 瑞典 AB 开发

    (2)   mysql->sun->oracle

    (3)   mysql数据库的特点

    1.    开源

    2.    免费

    3.    跨平台(windows / linux /unix /苹果)

    4.    处理并发性 (13000个)/安全/稳定

    5.    该数据库 轻(对资源要求不高.) 安装文件 37.7m ,而且对cpu / 内存要求不高.

    安装和配置

    1.      一般说,一台机器装一个mysql.

    2.      安装和配置过程见 mysql安装图解

    mysql 使用

    1.      可以使用window dos 登录到mysql数据库.

    基本语法

    在dos 下输入

    mysql –u 用户名  -p密码  

    特别说明:-p 后面的密码 不要有空格 / 使用该命令的时候,需要配置一下环境变量.

    2.      mysql数据库也提供图形化界面来登录mysql数据库

    演示:

    3.       如何在mysql中创建数据库

    基本语法

    create database 数据库名 

    •    创建一个名称为mydb1的数据库。

    create database mydb1; 【sql】

    •    创建一个使用utf-8字符集的mydb2数据库。

    create database mydb2character set utf8

    •    创建一个使用utf-8字符集,并带校对规则的mydb3数据库

    create database mydb3 character set utf8collate utf8_general_ci;   

    mysql的一些常用指令

    ①如何查看,创建数据库的指令:

    show create databse 数据库名;

    ② 显示数据库

    指令 show databases;

    ③查看创建数据库的指令

    show create database 数据库名;

    ④删除数据库:

    drop database 数据库名

    ⑤  如何指定使用某个数据库

    use 数据库名;

    ⑥ 如何备份和恢复数据库.

    mysqldump –u 用户名–p密码 数据名 > 存放路径

    该指令,需要在dos控制台下直接执行

    恢复数据库:

    1.创建一个数据库mydb2 ,但是这个数据库目前是空.

    2.use 数据名

    3.在mysql 控制台下 使用 source 备份文件路径

    u 创建表

    基本语法


    案例 :

    创建一张用户表

    create table users (

    id int ,

    name varchar(64),

    pwd varchar(64),

    birthday date)

     注意逗号的位置


    mysql数据类型(重点)

    ①    数值型:

    1. bit(m) m默认为 1 最大 64

    案例

    create table test1 (id bit(1)); ---//这里显示乱码?

    2. tinyint [unsigned] 如果是有符号则表示 -128 到 127 ,如果是无符号 0-255

    案例

    create table test3(num tinyint) -- -128 到 127

    create table test4(numtinyint unsigned)  0 --- 255 

    4.      smallint

    samllint 是两个字节表示的.

    带符号是  负的 2的15次方 到 2的15次方-1 ,无符号 2的16方 -1

    其它的数值类型,见下图即可

    6.    float

    FLOAT[(M,D)] [UNSIGNED]  是定长

    m :表示有效位

    d: 表示小数点有几位

    案例:

    create table test5( num float);

    create table test6(num float(5,1));

    7. double

    其用法和float 类似,只是表示的范围更大,也是定长

    8. numeric(m,d)

    用于表示小数,或者整数

    create table test7 (num numeric); //这样其实就是可以存放整数.

    create table test8 (num numeric(5,2));//这样就可以表示 有效为5,小数点有两位的数

    ②    字串类型

    一览图:

    常用的有

    (1)   char(m)

    m 范围是 0-255, 定长.

    char(20) 如果你存放‘abc’ 字串,实际在表 ‘abc              ’;

    案例:

    create table test11 (name char(20));

    ☞ 小技巧:

    mysql 自带的client 默认支持 utf8 码,所有我们在添加中文的时候,需要设置让client支持gbk

    * show variables like ‘char%’;   //显示关于字符的设置参数

    * set character_set_client=gbk; //可以存中文

    * set character_set_results=gbk; //可以看中文

    (2)   varchar(m)

    m 表示大小 ,范围 0-65535, 变长

    varchar(20) 如果你存放‘abc’ 字串,实际在表 ‘abc’;

    案例 省略...

    建议: 如果表的某列长度固定,比如 产品编号..学号. .. 而且在 255内,我们应当使用char

    ,如果长度不能取得,或者长度大于255 小于 65535 则使用varchar

    (3)   text

    该类型,可以表示更大的字串.

    ③    日期类型

    (1)   date

    日期 (年-月-日)

    create table test12(birthday date);

    对于date 只保存 年-月-日

    (2)   datetime

    日期时间类型

    create table test13(hiredate datetime);

    (3)   timestamp

    邮戳: 该类型可以保存年-月-日 : 时:分:秒

    它和datetime 最大的区别是,当你 update 某条记录的时候,该列值,最自动更新

    create table test14 (name varchar(64) , salfloat, hiredate1 timestamp, hiredate2 datetime);

    建议: 如果不知道该不该用  timestamp ,就不要用.

    u  创建表综合案例

    字段

    属性

    Id

    整形

    name

    字符型

    sex

    字符型或bit型

    brithday

    日期型

    Entry_date

    日期型

    job

    字符型

    Salary

    小数型

    resume

    大文本型

    create table emp(

    id int,

    name varchar(64),

    sex char(2),

    birthday date,

    Entry_date date,

    job varchar(32),

    salary float,

    resume text)

    u  修改表结构



    案例:

    •    在上面员工表的基本上增加一个image列。

    alter table empadd image blob;


    •    修改job列,使其长度为60。

    alter table empmodify job varchar(60);


    •    删除sex列。

    alter table empdrop sex;


    •    表名改为user。

    rename table empto user;


    •    修改表的字符集为utf-8

    alter table usercharacter set utf8;


    •    列名name修改为username

    show createtable 表名;


    insert语句

    基本语法:

    insert into 表名 [列名.....] values (值....);

     

    l  插入的数据应与字段的数据类型相同。

    比如:

    create tabletest15 (name varchar(64));

    insert intotest15 (name) values(‘aaa’);

    insert intotest15 (name) values(34);

    create tabletest16 (age int);

    insert intotest16 (age) values(34);

    insert intotest16 (age) values(‘aaa’);(错)

    insert intotest16 (age) values(‘111’);(虽然ok,但是不是好的写法.)

    l  数据的大小应在列的规定范围内,例如:不能将一个长度为80的字符串加入到长度为40的列中。

    在values中列出的数据位置必须与被加入的列的排列位置相对应

    create tabletest17 (id int ,name varchar(64));

    insert intotest17 (id,name) values(3,’aaa’);

    insert intotest17 (name,id) values(’aaa’,3);

    字符和日期型数据应包含在单引号中

    l  插入空值,不指定或insert into tablevalue(null)

    update 语法

    基本语法:

    update 表名 set 列名=表达式 ... where 条件

    说明: 如果 where后面没有条件,则相当于对整个表进行操作。

     

    l  UPDATE语法可以用新值更新原有表行中的各列。

    l  SET子句指示要修改哪些列和要给予哪些值。

    l  WHERE子句指定应更新哪些行。如没有WHERE子句,则更新所有的行。

    将所有员工薪水修改为5000元。

    update employee set sal=5000;


    l  将姓名为’zs’的员工薪水修改为3000元;

    update employee set sal=3000 where name=’zs’;


    l  将wu的薪水在原有基础上增加1000元

     update employee set sal=sa+1000 where name=’wu’;


    u  delete语句

    基本语法

    delele from 表名 where 条件;

     

    注意:

    l  如果不使用where子句,将删除表中所有数据。

    要小心使用.

    l  Delete语句不能删除某一列的值(可使用update)

    使用delete语句仅删除记录,不删除表本身。如要删除表,使用drop table语句。

    l  同insert和update一样,从一个表中删除记录将引起其它表的参照完整性问题,在修改数据库数据时,头脑中应该始终不要忘记这个潜在的问题。

    l  删除表中数据也可使用TRUNCATE TABLE 语句,它和delete有所不同,参看mysql文档。

    truncate table 表名,可以删除表的记录,速度快,但不能回滚..

    在mysql中事务的特殊说明:

    (1)   mysql 控制台是默认自动提交事务(dml)

    (2)   如果我们要在控制台使用事务,应该这样

    l  set autocommit=false;

    l  savepoint 保存点

    l  //操作...

    l  rollback to 保存点.

    u  select 语句

    基本语法

    select 列名...., 列(可以运行) from 表名 where 条件;

     

    注意事项:

    l  Select 指定查询哪些列的数据。

    l  column指定列名。

    l  *号代表查询所有列。

    select * from 表名;

    l  From指定查询哪张表。

    l  DISTINCT可选,指显示结果时,是否剔除重复数据

    select distinct * from 表名

    l  练习:

    l  查询表中所有学生的信息。

    select * from student;

    l  查询表中所有学生的姓名和对应的英语成绩。

    select name,english from student;

    过滤表中重复数据。

    select distinct * from 表名

    l  练习

    l  在所有学生分数上加10分特长分(即查询所有学生总分再加10分)。

    select english+math+chinese+10 , name fromstudent;

    l  统计每个学生的总分。

    l  使用别名表示学生分数。

    select english as ‘英语’ , math as 数学 , chinese from student;

    l  使用where子句,进行过滤查询。练习:

    l  查询姓名为wu的学生成绩

    select english  ,name from student where name = ‘wu’;

    l  查询英语成绩大于90分的同学

    select * from student where english>90;

    l  查询总分大于200分的所有同学

    select * from student where (math+english+chinese)>200;

    u  where子句如何使用

    案例:

    l  查询英语分数在 80-90之间的同学。

    select * fromstudent where english>=80 and english<=90;

    l  查询数学分数为89,90,91的同学

    select * fromstudent where math in (89,90,91);

    l  查询所有姓李的学生成绩。

    select * fromstudent where name lik ‘李%’;

    l  查询数学分>80,语文分>80的同学。

    select * from student where matn>80 andchinese>80;

    u  order by 子句

    l  练习:

    l  对数学成绩排序后输出。

    select name,math from student order bymath;

    l  对总分排序后输出,然后再按从高到低的顺序输出

    select math+english+chinese as allfen ,name from student order by allfen;

    l  对姓李的学生成绩排序输出

    select (math+english+chinese) asallfen,name from student where name like ‘李%’ order by allfen;

    u  count

    u  练习:

    l  统计一个班级共有多少学生?

    select count(*)from student;

    l  统计数学成绩大于90的学生有多少个?

    select count(*)from student where math>90;

    l  统计总分大于250的人数有多少?

    select count(*)from student where (math+english+chinese)>250;

    u  sum的用法

    练习:

    n  统计一个班级数学总成绩?

    select sum(math)from student;

    n  统计一个班级语文、英语、数学各科的总成绩

    selectsum(math),sum(english),sum(chinese) from student;

    n  统计一个班级语文、英语、数学的成绩总和

    selectsum(math+english+chinese) from student;

    n  统计一个班级语文成绩平均分

    selectsum(chinese)/count(*) from student;

    u  avg的用法

    u  练习:

    n  求一个班级数学平均分?

    select avg(math)from student;

    n  求一个班级总分平均分

    selectavg(math+english+chinese) from student;

    group by 用法

    练习:对订单表中商品归类后,显示每一类商品的总价

    select product , sum(price) from ordersgroup by product;

    having用法

    练习:查询购买了几类商品,并且每类总价大于100的商品

    select product , sum(price) from ordersgroup by product having sum(price)>100

    u  日期和时间函数

    CURRENT_DATE (  )

    当前日期

    CURRENT_TIME (  )

    当前时间

    CURRENT_TIMESTAMP (  )

    当前时间戳

    DATE (datetime )

    返回datetime的日期部分

    DATE_ADD (date2 , INTERVAL d_value d_type )

    在date2中加上日期或时间

    DATE_SUB (date2 , INTERVAL d_value d_type )

    在date2上减去一个时间

    DATEDIFF (date1 ,date2 )

    两个日期差(结果是天)

    TIMEDIFF(date1,date2)

    两个时间差(多少小时多少分钟多少秒)

    NOW (  )

    当前时间

    YEAR|Month|DATE (datetime )

    年月日

    案例:

    select current_date() from dual ; 得到当前日期

    select current_time() from dual ;  得到请求时间;

    date_add()  date_sub()的用法

    说: 有一个留言表

    create table message(id int , titlevarchar(64), publishdate datetime);

    请查询出,两个小时内,发布的消息:

    select * from message where  date_add(publishdate, interval 2 hour) >= now();

    特别说明

    date_add(日期/date/datetime/timestamp, interval 数  type)

    type 可以使用如下值:

    字符函数:

    ,常用函数一览图:

    CHARSET(str)

    返回字串字符集

    CONCAT (string2  [,... ])

    连接字串

    INSTR (string ,substring )

    返回substring在string中出现的位置,没有返回0

    UCASE (string2 )

    转换成大写

    LCASE (string2 )

    转换成小写

    LEFT (string2 ,length )

    从string2中的左边起取length个字符

    LENGTH (string )

    string长度

    REPLACE (str ,search_str ,replace_str )

    在str中用replace_str替换search_str

    STRCMP (string1 ,string2 )

    逐字符比较两字串大小,

    SUBSTRING (str , position  [,length ])

    从str的position开始,取length个字符

    LTRIM (string2 ) RTRIM (string2 )  trim

    去除前端空格或后端空格

    把 ename 列 的 smiTh 第一个字母大写,其它全部小写,怎么办?

    select UCASE(SUBSTRING (LCASE (‘smiTh’), 1,1)) from dual;

     

     

     

    //首先把‘smiTh’ 的首字母取出,转成大写

     

    ucase(substring(‘smiTh’,1,1))

    //把‘smiTh’ 去掉首字母后,余下的部分取出,//转成小写

    lcase(substring(‘smiTh’,2,length(‘smiTh’)-1))

    //最后拼接

    select

    concat (ucase(substring(‘smiTh’,1,1)),lcase(substring(‘smiTh’,2,length(‘smiTh’)-1))) from dual;

    结果:

    selectconcat(lcase(substring('smiTh',1,1)), ucase(substring('smiTh',2,length('smiTh')-1)))from dual;

    u  数学函数

    !!!

    mysql的常见约束

    ①    primary key (主键)

    特点: 主键是用于唯一标识一条记录的约束,一张表,最多只能有一个主键,主键不能为null,也不能重复

    create table user1 (id int primary key,name varchar(32));

    ②    auto_increment

    可以自增长.

    举例:

    create table user2 (id int primary keyauto_increment , name varchar(32));

    ③    unique (唯一)

    特点: 表的某列的值,不能重复, 可以为null (可以有多个null), 一张表中可以有多个unique.

    create table user4(id int unique,namevarchar(32));

    ④    not null (非空)

    mysql 的表的列,默认情况下可以为null, 如果不允许某列为空 ,则可使用 not null说明

    create table user5(id int primary key, namevarchar(32) not null);

    ⑤    外键 foreign key

    从理论上说明,我们先建立主表,再建从表

    --部门表

    create table dept(id int primary key,

    name varchar(64));

    insert into dept values(1,’财务部’);

    --雇员表

    create table emp(id int primary key,

    name varchar(32),

    deptid int references dept(id));

    上面的建立外键的写法是错误的。

    应该这样.(表级定义)

    create table emp(id int primary key,

    name varchar(32),

    deptid int ,

    constraint emp_fk foreignkey (deptid) references dept(id)

    );

    小结外键:

    (1)      外键只能指向 主表的主键列,或者  unique

    (2)      外键的数据类型和它指向的列的数据类型一样.

    (3)      外键的值,要么为空,要么是指向的那列中存在值.

    (4)外键可以指向本表的主键列,或者unique

    产品分类

    create table producttye(

    id int primary key,

    catagory varchar(32),

    parentId int,

    constraint type_fk foreign key (parentid)references producttye(id));

    insert into producttye values(1,’电器’,null);

    insert into producttye values(2,’电冰箱’,1);

    insert into producttye values(3,’电视机’,1);

    insert into emp values(1,’张三’,2);

    u  check

    create table user7 (age int check(age>12));

    补充讲解mysql 分页查询:

    返回第 4条 ----第 7条记录

    select * from student limit 3,4;

    基本语法

    select * from 表名 where 条件 ... limit 从第几条取,取出几条

    从第几条取 : 这里mysql从0 开始编号.

    l  安装语文成绩排序,查询出第3  名到第5名

    select * from student order by chinese desclimit 2,3

    扩展, 分页: pageNow, pageSize

    select * from 表名 where 条件 [group by .. having .. order by..] limit (pageNow-1)*pagesize, pageSize;

    上机的题:

    把mysql的操作,与 SqlHelper 类整合一下.

    版权声明:本文为博主原创文章,未经博主允许不得转载。

    today lazy . tomorrow die .
  • 相关阅读:
    论文(卷积数据流)-Communication Lower Bound in Convolution Accelerators
    CPU架构相关
    Verilog-数据包检测器
    多比特乘法器的分解
    Verilog-数字时钟无毛刺切换
    Booth乘法器
    C++:地平线2019相关题
    C++:char数组和string类
    C++:strcpy函数
    半导体 semiconductor 相关知识
  • 原文地址:https://www.cnblogs.com/france/p/4808726.html
Copyright © 2020-2023  润新知