2020-07-13

1. 关系型数据库:

      1. mysql 分为收费版和社区版 (收费版的提供技术支持)

      2. mysql 是开源的

      3. 市场很大,应用很多,很多问题都有解决方案

      4. 单个服务超过3百万条数据之后就会变慢,但是我们可以采用分布式解决

2. sql语句:

        数据库(文件夹) database

        表(文件) table

        字段(类中一个属性) col

        行(一个对象对象) row

        create database studb;//创建一个数据库

        use studb;  // 选择一个数据库

        create table stu(id int, name varchar(100),gender varchar(20),age int); // 创建一张表

        insert into stu(id,name,gender,age) values (1,'小明','男',12),(2,'小明2','女',15); // 插入

        select * from stu; // 查询

        update stu set name = 'xiaohong',age =12  where id = 1; // 更新语句

        delete from stu where id = 1;

# 数据库中类型 (每种数据类型都有一种长度,都有一个取值范围)

        整数 (无符号数 UNSIGNED ,和有符号数)

        tinyint

        int(一般常用 4个字节)

        bigint

# demo  (00000-999*23*23)

# create table aa(id int primary key autu_increment)

# id 不会重复,自增长类型的id他是有顺序的,

        1. 当别人知道一个之后,就可以通过暴力导出数据库中其他数据,

         2. 他不利于数据库迁移

         3. int 他有一定的取值范围 (数据比较多的时候很容易到取值范围了)

# 项目中解决问题: id设置成字符串类型,id的值通过程序生成(例如:雪花算法)

# 字符型 (长度在5.5之前值字节,5.5之后是字符)

        char 指定长度 ,例如:name char(20),  即使这个name他占不了20个字符,也会开启20个字符的空间

varchar 指定长度

        例如 name varchar(20)  如果name的值只能占8个字符,只会8个字符空间

        如果这是的值超过了长度, 插入的时候会报错

        如何选取:  如果值的长度变化比较大,从节省空间的角度来考虑,我们可以使用varcahr,但是在计算长度的过程中会消耗一定的性能,如果值的长度变化不大,例如手机号码,我们就可以char。

text 不需要指定长度,一般用来存储大段的文本

# 浮点型

        float:浮点型,含字节数为4,32bit,数值范围为-3.4E38~3.4E38(7个有效位)

        double:双精度实型,含字节数为8,64bit数值范围-1.7E308~1.7E308(15个有效位)

        decimal:数字型,128bit,不存在精度损失,常用于银行帐目计算。(28个有效位)

        float 是单精度的浮点数,精确到小数点后面7位, double 是双精度的浮点,精确到小数点后面15位。但是计算机操作系统的原因,float,double不用来存储重要数据,例如钱

         重要的数据,我们使用decimal, decimal本质上就是个字符串,所以没有精度缺陷

# 时间格式

        date  格式 YYYY-MM-DD,一般用来存储日期(1000-9999)

        datetime 格式 yyyy-mm-dd HH:MM:SS 存储日期和时间(1000-9999)

        timestamp 格式 yyyymmdd hhmmss  存储日期和时间,时间戳 (1970 - 2038年)

# 图片,视频,音频

        1.一般做法:

           上传专门的图片服务器,生成一个链接,把图片链接放到数据库(varchar)

        2. 把图片放到数据库中

           采用blob 存放二进制的

# 约束 (字段名 字段类型  约束)

        非空 not null (超市商品的价格) (Field 'name' doesn't have a default value)

        default '' 设置默认值 (寺庙中和尚的性别)  ( age int default 1)

        UNIQUE  唯一(例如咱们的学号)  (Duplicate entry '12312312' for key 'tel'), 注意: 唯一是可以为空的

        primary key  主键 (not null + 唯一),一张表只能有一个主键,设置联合主键

        整数类型的可以设置 auto_increment  自增长, 必须是主键

        FOREIGN KEY 外键(表关联中,这个字段在其他表中为主键)

# 条件语句 (where 语句)

          查询要占到整个数据库操作的80%

        1. select * from person;  // 整张表查询 ,

          * 代表所有字段,如果用这种方式,java中类中属性的个数与 字段个数不匹配的时候,会报错

        2. select name, age, ... from person;  // 需要什么字段,查询什么字段就可以了

        3. select name,age from person where name = 'aa'; // 根据名字进行查询

        4. select name,age form person where name = 'cc' and age = 3 and 1 = 1; // 名字='cc'并且age=3

        5. select name,age from person where name = 'cc' or age = 3; 名字='cc' 或者 age = 3

        6. select name,age from person where name in ('aa','cc'); // 效率太低,一般不用

        7. select name,age from person where name like '%c%';  // %是通配符

        8. select name,age from person where age > 3; // 把年龄大于3都查出来

        9  select name,age from person where age BETWEEN 2 and 5;  // 查询出[2,5]区间内的数据

        order by 排序

        order by 字段 [asc/desc]  asc 升序 desc 降序

        select name,age from person where age BETWEEN 2 and 5 order by age desc; //查询出[2,5]区间内的数据,按照年龄降序排

        select name,age from person where age BETWEEN 2 and 5 order by age desc, name asc; //查询出[2,5]区间内的数据,先按照年龄进行降序, 如果年龄一样的话, 按照姓名进行升序

        limit begin,count 分页  下标计算从0开始, 查询多少个

        select * from persin where name link '%c%' limit 1, 2; // 从第一行记录开始,查询两条

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。