java 常用 MySQL 和 Oracle
MySQL 数据库,本质是个文件系统 存储数据的仓库 实际存数据的是表
数据库管理系统 : 建库建表
类--->表
列--->类字段,成员变量
对象--->表中每行数据 表的一行称为一条记录
数据类型:
int 整数
double 浮点数 不能有约束
varchar 可变字符型 必须有约束
data 日期类型 格式 yyyy-MM-dd 注: 只有年月日,没有时分秒
新建库: create database if not exists 库名
删除库: drop database
显示库: show databases
使用数据库: use 数据库名
创建数据表的格式:
create table 表名(
列名1或字段名 数据类型 约束, (约束是约束数据内容的)
列名2 数据类型 约束,
列名3 数据类型 约束
);
主键约束 : 非空,唯一性 不具备任何含义
id int primary key auto_increment
查看表 show tables 显示表中的结构
desc 表名 显示表数据
删除表 drop tables 表名
drop table if not exists 表名
修改表结构:
添加列,添加字段: alter table 表名 add 列名 数据类型 约束
修改列,在原有的列上修改: alter table 表名 modify 列名 数据类型 约束
修改列名 : alter table 表名 change 旧列名 新列名 数据类型(长度)
删除列 : alter table 表名 drop 列名
修改表名 : rename table 表名 to 新名
向数据表添加数据 : insert
格式: insert into 表名(列名1,列名2,列名3) values (值1,值2,值3)
注意: 列名,表名问题
对应问题,个数,数据类型
添加数据格式,不考虑主键------> 格式: insert into 表名 (列名) values (值)
添加数据格式,所有值全给出------> 格式: insert into 表名 values (全列值) 注意:值主键也得写
添加数据格式, 批量写入 ------> 格式:insert into 表名 (列名1,列名2,列名3)values (值1,值2,值3),(值1,值2,值3),(值1,值2,值3)...
更新数据 ,在原有数据的基础上修改
格式: update 表名 set 列1=值1,列2= 值2 where 条件 (条件找数据中的唯一性)
<> 不等于 = <= >= 没有== 与: and or 非 :not
id in (1,3,4,5,6) 表示id 为1,3,4,5,6 都被修改
删除表中的数:
格式: delete from 表名 where 条件 一条一条删,不清空主键记录数
truncate table 表名 直接将表删除,重新建表, 主键从零开始
命令行改乱码问题 : set names 'gbk'
*****SQL 查询语句:
查询指定列的数据--->格式: select 列名1 ,列名2 from 表名
查询所有列的数据--->格式: select * from 表名
查询去掉重复记录--->格式: select distinct 跟随列名 from 表名
查询重新命名列---->格式: select 列名 as 新列名 from 表名
查询数据中直接进行数学计算:---->select 列名, 列名 +数字 from 表名
between ... and ... 在...之间, 要求前面的数比后面的小 也可以做日期查询
like 模糊查询 配合通配符
select * from 表名 where 列名 like ' % ... %'
select * from 表名 where 列名 like '-----' (一个下划线等于一个字符)
查询 不为空的
*方式1 :select * from 表名 where 列名 is not null
方式2 :select * from 表名 where not (列名 is null )
|
|