**
命令的使用:
**
① 连接数据库的命令
mysql -uroot -p
② 查看当前mysql服务器上的database.
show databases;
③ 删除存在的数据库
drop database db_name(数据库的名字)
④ 选择使用哪个数据库
use 数据库名字
⑤ 显示某个数据库所有的tables;
show tables;
⑥ 创建数据库
create database 数据库名字; – 编码设置的问题
create database 数据库名字 default character set utf8;
例子:
create database java0501 default character set utf8;
⑦ 在数据库中创建table.
create table 表名(字段名 字段类型,字段名 字段类型… )
--- ** 主键(primary key)约束: 某个字段设置为主键之后,那么这个字段不允许重复。 因为没有重复内容出现,以此保证每行数据不重复。 create table user(id int primary key,username varchar(20),password varchar(16),intro varchar(200)); --- ** 创建table的时候,设置支持的编码方式为utf8 ** id作为主键字段,希望可以自动增长(auto_increment -- 只有int类型可以自动增长) create table user1(id int auto_increment primary key, username varchar(20),
password varchar(16),intro varchar(200))default charset=utf8;
⑧ 插入一条数据到table中
insert into 表名(字段名1 , 字段名2) values(字段1的值,字段2的值)
例如:
insert into user(id,username,password,intro)
values(1,“alice”,“123456”,“how are you!”);
insert into user1(username,password,intro) values(“小明”,“123456”,“nice to meet you.”);
insert into user1(username,password) values(“tom”,“222222”); – 插入了几个字段值
⑨ 查询语句
select * from 表名;
“ * ” 表示所有的字段名
⑩ 查看表的结构
desc 表名; – use了某个数据库,就可以查看这个数据库中的table的结构。
11. 删除表:
表的结构被删除,那么表中数据也被删除。
drop table 表名;
12.mysql中的数据类型
int , bigint -- 整数 decimal , numeric -- 小数 ····比如:decimal(5,3) , 总长度是5位, 小数位三位, 整数位两位。 varchar -- 字符串类型 char -- 字符串类型 比如: char(10) , 表示必须长度为10位,如果你不够10位会用空格进行补充。 varchar(10),表示最多保存10个字符串, 以字符串的实际长度为准。 datetime -- 日期,表示的范围大 timestamp -- 日期,表示的日期范围小, 支持时区的变化
13. 删除语句:
delete from 表名; -- 删除表中的数据,表的结构还在。 -- 没有指定删除那些行, 所以是全表删除。 delete from 表名 where 字段名=字段值; --- 根据某个字段的数据值,删除指定的数据。
14. 修改语句:
update 表名 set 字段名=新的字段值, 字段名1 = 新的字段值.... ; -- 全表修改 update 表名 set 字段名= 字段值, 字段名= 字段值 ... where 主键名= 数据值 ;
15.查询语句:
-- select * from 表名 ; 全表查询 select * from student -- select 列名1, 列名2.... from 表名 ; 查询部分列 select code, name , age from student -- select * from 表名 where 条件.... ; 查询部分行 select * from student where code >=6; -- select 列名1, 列名2.. from 表名 where 条件... ; 查询结果为指定列的部分行内容 select code, name , age from student where code >=6; -- 给字段取别名: select 列名1 as aa, 列名2 as bb.. from 表名 ; -- as可以省略 select code as "学号", name as "名字", age '年龄' from student
1. sql中的常用英语单词
create : 创建 drop:删除 insert:插入 select:查询 update:更新
delete:删除 table: 表 query:查询 row:行 record:记录 filed:字段
primary : 主要 database:数据库 show: 展示 view:视图 modify:修改
alter:修改 from : 从… where: 当… have:有 desc: 描述
like:像… as: 作为… auto_increment:自动增长 character:字符
order:顺序 desc:降序 asc: 升序 and: 并 limit: 限定
group: 组 count: 数量 syntax: 语法 error:错误 default:默认
check:检查 unique: 唯一 join : 合并 foreign key: 外键
primary key: 主键 constraint: 约束 duplicate:重复
2. 数据的存储方式有哪些?
a. 文件--- 存在硬盘 1)文件存储格式 2)缺点:读写速度慢 , java的io操作比较麻烦。 b. 变量 --- 存在内存中 读写速度很快, 这些数据都是临时数据。 c. 数据库 -- 数据库管理系统中 数据库管理系统是一套软件,是一种存储和管理数据表的软件系统。 适用于大数据量,多人并发操作。 ** 表是二维表
3. 数据库服务器软件的安装
数据库客户端软件的安装(或使用cmd操作)
4. 数据库的类型
a. 关系型数据库: 支持sql mysql(免费) , oracle(收费) , sql server(微软) b. 非关系型数据库:不支持sql Redis , Hbase , MongoDB
5. ER模型(E - Entity 关系, R- Relation 关系)
– ER模型, 实际描述的是数据库中表与表之间的关系,以图形的方式展示出来。
– 矩形代表实体
– 菱形代表关系
– 椭圆代表实体的属性
– 实体之间的关系种类:
1对1, 1对多, 多对多。
6. sql(Struct Query Language)
– sql语言不区分双引号和单引号。
– sql语言关键字不区分大小写, 数据值区分大小写。
例如:
create table…
Create Table…
name – fjm
name – FJM (FJM != fjm)
7. 数据类型:
– int , char , varchar , datetime
8 约束:
a. 主键约束:primary key , 唯一且非空 一个字段作为主键:create table t_pk(t_id int auto_increment primary key,.....) 多个字段作为联合主键: create table t_pk_n(floorId int , floorIndex int , roomName varchar(10) , primary key(floorId , floorIndex)) -- 主键约束没有命名 create table t_pk_n(floorId int , floorIndex int , roomName varchar(10) , constraint abc primary key(floorId , floorIndex)) -- 主键约束命名为abc
b. 非空约束:not null , 字段值不能为null. create table t_null(floorId int , floorIndex int , roomName varchar(10) not null , primary key(floorId , floorIndex))
c. 默认值:default , 建表的时候指定字段的数据默认值 create table t_df(floorId int , floorIndex int , roomName varchar(10) default 'hqyj-room' not null , primary key(floorId , floorIndex))
d.唯一约束:unique ,唯一约束的字段值不能重复,但是可以是null. --- 如果字段名和关键字冲突,就需要使用``符合引起来。 create table t_un(floorId int , floorIndex int , roomName varchar(10) default 'hqyj-room' not null , `key` varchar(10) unique, primary key(floorId , floorIndex)) --- 通过表名的前缀 ,避免关键字重复问题: student(s_name, s_age,s_sex , s_key....)
e. 检查: check, 检查数据是否合理。 create table t_ch(id int auto_increment primary key, name varchar(10), age int check(age>0) )
f. 外键约束:foreign key ,解决这个1对多的关系。 ① create table t_cla(c_id int auto_increment primary key , c_name varchar(10)); -- 主表 create table t_stu(s_id int auto_increment primary key , s_name varchar(10), -- 从表 sc_id int , foreign key(sc_id) references t_cla(c_id)) ** t_stu表中的c_id字段,受t_cla表中的c_id字段的数据值的控制。 ** 先删除/更新从表的数据,再删除/更新主表的数据 ** 先插入主表的数据,再插入从表的数据 ② create table t_cla_1(c_id int auto_increment primary key , c_name varchar(10)); create table t_stu_1(s_id int auto_increment primary key , s_name varchar(10), sc_id int ) ** 程序员心理知道这两个表之间需要存在外键约束关系 ** 通过约束constraint,修改表,使其具有外键约束关系 alter table t_stu_1 add constraint fk_cla_stu foreign key(sc_id) references t_cla_1(c_id);
9. sql(查询) – sql 语言中查询是重点
a. 全表查询: select * from 表名 b. 查询部分列: select 字段1, 字段2.... from 表名 c. 查询部分行: select * from 表名 where ....
d. where条件相关内容: 1.模糊查询(like) % 匹配任意字符 _ 匹配一个任意字符 语法规则: select * from 表名 where 字段名 like 字符串 select * from school where sc_name like "%交通%"; select * from school where sc_name like "___交通%"; 2.数据的条件查询: > 大于 , <小于 , >=大于等于 , <=小于等于 , =等于 语法规则: select * from 表名 where 字段名>? select * from school where sc_area>3000 and : 且 , 多个条件一起进行判断,且都时真的情况 or : 或, 多个条件一起进行判断, 满足其中的某个条件就表示满足。 3.查询结果的排序(order by) desc:降序 asc:升序, 默认是升序排序方式。 语法规则: select * from 表名 where 条件 .. order by 字段名 排序方式; select * from school order by sc_code desc; select * from school where sc_code > 1002 order by sc_total; --- 先对查询的结果按sc_total降序,然后按sc_birth 升序。 select * from school where sc_code < 1005 order by sc_total desc ,sc_birth ; 4.分页查询(limit) 语法规则: select * from 表名 where 条件 ... limit n,m; ** n表示从第几行开始查询(第一行对应0.也就是从0开始。), m表示一共要查询多少条数据(如果超过总行数,那么有多少显示多少条。) select * from school limit 1,2; select * from school where sc_area > 3100 limit 0 ,10; select * from school where sc_area > 3100 limit 5 ,10; select * from school where sc_area > 3100 order by sc_area limit 0 ,2 下列语句语法错误:先排序,然后使用limit select * from school where sc_area > 3100 limit 0 ,2 order by sc_area ** 分页的意义是什么? table中数据量比较多,用户并不一定要查看所有内容,可以 选择分批给用户查询出数据。 ** 分页需要设置每页的起始行和每页显示的条数。 page:页码 rows: 每页显示的行数 计算每页的起始位置: (page-1)*rows ** 当前表中的数据应该分为多少页: rows:每页显示的行数 table表的总行数(totalRows):select count(*) from 表名; 总页码: int pages = (totalRows%rows==0)? totalRows/rows: totalRows/rows+1; 5. count函数的使用: select * from school; --- 全表的数据 select count(sc_code) from school ; -- 计算查询结果中sc_code的行数, 也就是查询结果总行 select count(*) from school ; -- 计算出查询结果的总行数