了解数据库和mysql的简单使用

    xiaoxiao2025-05-21  3


    了解数据库和mysql的简单使用


    问题分析


    当数据量很大的时候,所有数据都集中在一个文本文件中的话,读写会很困难,内存消耗大,速度很慢操作很麻烦,因为读写都要根据指定的格式尽心解析,不通用每次获取数据都要全部数据重新读写,不能通过对索引对指定的数据进行读写数据冲突的解决方案要完全依赖Java app去实现更多的功能,像排序,也要完全通过Java app实现

    知识点


    1. 数据库中的关键结构组织和属性

    表 ——是存储数据的最基本单元 表里面包含有字段和数据

    字段: 字段名,字段类型,主键,是否允许为空,默认值 字段类型就是字段的数据类型 比较常用的有 字符串varchar/text 整型int 浮点型double 日期datetime/date

    这里简单说一下date和datetime区别 DATETIME类型可用于需要同时包含日期和时间信息的值。 MySQL 以 ‘YYYY-MM-DD HH:MM:SS’格式检索与显示 DATETIME 类型。支持的范围是’1000-01-01 00:00:00’ 到 ‘9999-12-31 23:59:59’。(“支持”的含义是,尽管更早的值可能工作,但不能保证他们均可以。) DATE类型可用于需要一个日期值而不需要时间部分时。MySQL 以 ‘YYYY-MM-DD’ 格式检索与显示DATE值。支持的范围是 ‘1000-01-01’ 到’9999-12-31’。

    主键:一条数据的主索引,设为主键的字段,数据不能重复,一个表中只有一个主键,可用于表示当前这条数据

    是否允许为空 字段属性,表示插入一条数据的时候,是否允许不插入该字段对应的数据(即为null),主键不能为空 默认值 字段属性,可不设置,表示插入一条数据的时候,如果不插入某字段对应的数据,系统会自动插入其默认值


    2. MySQL基本命令 注意每一句都要以分号结束

    查看有哪些数据库 show databases ; 进入某个库 use 库名; 查看当前库中有哪些表 show tables ; 查看该表具体结构 desc 表名 ; 新建库,库名为test create database test ; 删除库 drop database if exists test ; 新建表 create table account1{ code varchar(6) not null, password int(6) not null, money double(5,2) not null, primary key(code) } 删除表 drop table if exists account1 ; 插入全部字段数据 insert into 表名 value(字段1值, 字段2值, 字段3值); insert into account1 value('100001',111111,100.00); insert into account1 value('100002',222222,200.00); insert into account1 value('100003',333333,300.00); 注意:字符串用英文单引号引住 插入部分字段数据: insert into 表名(字段1, 字段2value(字段1值, 字段2值); insert into account1(code,password) value('10004',444444); 允许为空(表account2) create table account2( code varchar(6) , password int(6) , money double(5,2), primary key(code) ); 设默认值(表account3) create table account3( code varchar(6) not null, password int(6) not null, money double(5,2) not null default 0.0, primary key(code) ); 自增(表account4) 如果一个主键是数字类型,可以设置为自增字段,那么插入数据时,不用插入主键,主键会从1开始递增 create table account4 ( code int (6) not null auto_increment, password int(6) not null, money double(5,2) not null, primary key (code) ); 查询数据 整表查询: select * from 表名;(*表示所有字段) 个别字段查询: select 字段1,字段2 from 表名; 条件查询:where后面带出条件语句 select * from account1 where code='100002'; select password from account1 where code='100002'; select * from account1 where code='100002' and password=111111; (and表示“且”) select * from account1 where code='100002' or password=111111; (or表示“或”) select * from account1 where money>200.00; 修改数据 update 表名 set 字段1=值1,字段2=值2;(一般要加条件) update account1 set password=123;(不加条件,整个表的数据被修改) update account1 set password=123 where code='100002'; (只修改该条件对应的数据) update account1 set password=123,money=0 where code='100002'; (同时修改多个字段) 删除数据 delete from 表名;(一般要加条件) delete from account1;(不加条件,整个表的数据被删除) delete from account1 where code='100006'; (只删除该条件对应的数据)

    3、varchar(6)、int(6)、double(5,2)括号中的数字分别代表什么意思?

    varchar(6):插入的字符串字段不能超过6个字符 int(6):这里的6并不是只能插入6位以内的数字,对于普通的int字段来说,int(6)与int(11)没有区别,插入的数字只要不超过int的范围(4字节)即可;对于设置为补零(zerofill)的int字段,int(6)表示当数位不足6位时,前面就补零至6位,如插入333,系统会补零为000333(补零的情况比较少用,暂不详述) double(5,2):保留2位小数后,整个长度(含小数)不能超过5位(如:123.123在范围内,可以插入;1231.23在范围外,不能插入)
    转载请注明原文地址: https://ju.6miu.com/read-1299109.html
    最新回复(0)