数据库_mysql

为什么要存储数据

      注册登录为例子: 用户注册之后,我们需要在数据库服务上保存用户信息
下次用户登录的时候,我们就可以查询到了

关系型数据库

  • mysql
    1. mysql 分为收费版和社区版 (收费版的提供技术支持)
    2. mysql 是开源的
    3. 市场很大,应用很多,很多问题都有解决方案
    4. 单个服务超过3百万条数据之后就会变慢,但是我们可以采用分布式解决
  • oracle
    1. 收费的(国企中,垄断行业)
    2. 单个节点处理很大量数据

使用内置工具连接mysql

mysql -uroot -p (mysql 代表执行的bin目录中 mysql.exe)

  • 不是内部或外部命令
    解决方案:
      1. 每次进入mysql的bin目录,然后执行
      2. 把这个bin目录添加环境变量

  • 忘记密码了
      1. 如果没有更改过密码可以在日志找到
      2. 如果改过密码的话,建议重装,如果不想重装的话,可以进入无密码模式进行修改

  • mysql8.0的原始密码比较复杂, 改密码的话,加密方式也有更改,设置8为以上,数字,小写字母,大写祖母, 符号

    1. 修改mysql的加密方式, 然后设置 123456
     ALTER USER 'root'@'localhost' identified by '123456' ;
    2.flush privileges ;  刷新

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;

数据库中类型

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

  • tinyint
  • int(一般常用 4个字节)
  • bigint

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

  • char 指定长度 ,
    例如:name char(20), 即使这个name他占不了20个字符,也会开启20个字符的空间
  • varchar 指定长度
    例如 name varchar(20) 如果name的值只能占8个字符,只会8个字符空间
  • text 不需要指定长度,一般用来存储大段的文本

3.浮点型

  • float 是单精度的浮点数,精确到小数点后面7位
  • double 是双精度的浮点,精确到小数点后面15位。
  • decimal 重要的数据,我们使用decimal, decimal本质上就是个字符串,所以没有精度缺陷
    例如:
       float(3,2) 前面那个是总的长度, 后面这个小数点后面的长度
       double(10,4)
       decimal(7,2)

4.时间格式

  • datatime yyyy-mm-dd hh:mm:ss 1000-9999
  • data yyyy-MM-dd
  • timestamp yyyymmdd hhmmss 1970-2038

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

  • 非空 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 语句)


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; // 从第一行记录开始,查询两条
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。