mySQL
首次进入
终端中进入mysql.exe的路径下,输入登入命令及初始化时随机生成的root用户密码
并记得修改方便记忆的密码
很好奇初始的数据库里面有什么
再来单个看,首先是information_schema
主要是库的各种信息,举个例子SCHEMATA
命令的大小写不敏感,shemata存储的是库的信息
试验一下,新建一个库“DB1”,schemata将会更新
顺手删库跑路
exit; #即可退出数据库
弄库
建个例子
表名大小写不敏感
NULL:是否允许为空
Key:键(约束)
Default:默认值
mysql 支持三类数据类型
----数值-----
类型 | 大小(字节) | 范围(符号) | 范围(无符号) | 数量级 |
---|---|---|---|---|
TINYINT | 1 | -128~127 | 0~255 | 百 |
SMALLINT | 2 | -32 768~32 767 | 0~65 535 | 万 |
MEDIUMINT | 3 | -8 388 608~8 388 607 | 0~16 777 215 | 百万 |
INT或者 INTEGER | 4 | -2 147 483 648~2 147 483 647 | 0~4 294 967 295 | 亿 |
BIGINT | 8 | -9 223 372 036 854 775 808~9 223 372 036 854 775 807 | 0~18 446 744 073 709 551 615 | 亿亿 |
FLOAT | 4 | -3.402 823 466 E+38~ -1.175 494 351 E-38,0,1.175 494 351 E-38~ 3.402 823 466 351 E+38 | 0,1.175 494 351 E-38~ 3.402 823 466 E+38 | |
DOUBLE | 8 | -1.797 693 134 862 315 7 E+308~ -2.225 073 858 507 201 4 E-308,0,2.225 073 858 507 201 4 E-308~ 1.797 693 134 862 315 7 E+308 | 0,2.225 073 858 507 201 4 E-308~ 1.797 693 134 862 315 7 E+308 |
增删改查
create table t1(key1 type1, key2 type2, ...); #建表
select * from t1; #查
insert t1 values(val1,val2,...); #插入表项 (增)
insert into t1 (key1)values(val1); #未赋值键值=NULL
delete from t1 where key1=val1; #删除表项 (删)
update t1 set key1=val1 where key2=val2; #修改表项 (改)
select * from T where key1=val1; #显示表项 (查)
drop table t1; #删除整表(删表)
drop database T; #删除整库
约束
主键约束
create table t2(key1 type1 not null); #非空约束
create table t2(key1 type primary key, ...); #建立主键(约束)表
primary key:不允许重复或NULL
复合主键 只需要某一主键值不同即可
create table t3(key1 type primary key auto_increment, key2 val2...); #自增约束表
create table t4(key1 type, key2 val2... primary key(key1,key2...)); #复合主键表
自增约束
从1开始计数,输入重复键值仍以当前自增值存入记录
delete from t1; #删除所有表项(表为空)
alter table t1 add primary key (key1); #增 主键
alter table t1 drop primary key; #删 主键
alter table t1 modify key1 type1 primary key;#改 onlyone主键 drop再modify/add
desc/describe t1; #查 主键
唯一约束
create table t5(key1 type1, key2 type2, unique key1); #增 唯一约束
create table t5(key1 type1 unique, key2 type2);
create table t5(key1 type1, key2 type2, unique (key1,key2)); #复合唯一
alter table t5 drop index key1; #删
默认约束
create table t6(key1 type1 default val1); #默认值val1
外键约束
create table class #主表
(
id int primary key, #无法删除 被副表引用的记录
name varchar(20)
);
create student #副表
(
id int primary key,name varchar(20),class_id int,
foreign key(class_id) references class(id)
#副表中外键取值必须存在于主表中
);
三大范式
范式是为了使用时更方便
1NF 第一范式
数据表中的所有字段都是不可分割的原子值
create table t1
( id int d primary key,
name char(100),
address char(30) #地址就可以拆分,不满足第一范式
);
create table t1
( id int d primary key,
name char(100),
country char(10),
province char(10),
street char(10);
door_num int
); #无法再拆分,满足第一范式
2NF 第二范式
满足1NF的前提下,除主键外,每列均完全依赖于主键,或是联合主键
属性(或属性组)X确定的情况下属性Y值确定,定义Y依赖于X,写作 X → Y(“X决定Y”)
主键 决定 其他列
create table t1
(
product_id int,
customer_id int,
product_name varchar(10), #只依赖于主键的部分字段 product_id
customer_name varchar(10),
primary key(product_id,customer_id)
);
联合主键,拆表后每表一个主键,如此满足第二范式
create table oder
(
order_id int primary key,
product_id int,
customer_id int
);
create table product
(
id int primary key,
name varchar(10)
);
create table customer
(
id int primary key,
name varchar(10)
);
联结
Mysql是关系型数据库:数据被分解为多张表(可伸缩性,scale well,数据量增大仍可工作),通过值相关联(relation)
联结时,至少有一个列(属性)出现在两个及以上的表中
多层字句的查询效率通常较低,应该尽可能的使用联结
联结过多表会导致效率会下降
内部/等值联结
--equijoin
select * from A, B where A.a = B.b
select * from A inner join B on A.a = B.b
--笛卡尔积,叉联结(cross join)
select * from A, B --仅将A所有行与B所有行一一对应,实则关系错误
自联结
select a1.a,a1.b from A as a1, A as a2 where a1.a=a2.a
--为避免二义性,mySQL “必须”使用“别名”表示“对同一张表的两次引用”
--注意select选中的a1,a2用于筛选
自然联结
--没有重复列(属性),(已知的)内部联结均为自然联结
select a.*, b.c1, b.c2 from a ,b where (condition)
--对表a使用通配符,其他表明确所需列,则a中重复列被去掉
外部联结
select * from A left/right outer join B on (condition)
--检索left:A或right:B中所有的用户,不满足condition的行赋NULL
组合查询
注意组合查询的每个select必须查询相同的列,表达式、或 聚集函数(顺序随意)
自动去除重复行
union all 不去重
select a,b from A where condition
union
select a,b from B where condition
--union将两次查询的结果一次返回
SQLite
sqlite超轻量数据库,2000年诞生,没有服务管理进程,多数linux系统内置sqlite3
基本操作
select current_timestamp; --获取当前系统时间
2020-7-23 11:39:45
SELECT column1, column2
FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
--未弄懂的部分
PRAGMA auto_vacuum = 0;
PRAGMA freelist_count; --标记为免费和可用的数据库页数
PRAGMA parser_trace; --解析SQL命令控制打印的调试状态
PRAGMA recursive_triggers; --递归触发器
PRAGMA max_page_count; --page?cache_size?
PRAGMA journal_mode = DELETE; --设置日志文件的存储与处理
--delete:事务结束日志删除 OFF:不保留任何记录
--PERSIST: 原地重写头部 MEMORY:存放内存非磁盘
--TRUNCATE: ??
PRAGMA synchronous = NORMAL; --OFF:不进行同步 NORMAL:关键的磁盘操作的每个序列后同步 FULL:每个关键的磁盘操作后同步
PRAGMA secure_delete = true; --控制内容删除,安全删除标志??
PRAGMA sql_trace = true; --SQL跟踪结果转储屏幕,需通过SQLITE_DEBUG编译引用
pragma case_sensitive_like = false; --开启后like对大小写不再敏感,默认是关闭的
pragma cache_size=1; --可以查看和设置内存中缓存的最大页数
pragma count_changes = true; --只对update delete insert 命令作用
PRAGMA encoding = utf-8; --设置字符串编码格式UTF-8、UTF-16le 或 UTF-16be
PRAGMA schema_version; --数据库头部的架构版本,执行改变命令(create或drop)后递增
PRAGMA writable_schema; --是否能够修改系统表(sqlite_master等)
PRAGMA user_version; --自定义版本值(开发标记用)
PRAGMA temp_store_directory = 'directory_path'; --临时数据库文件的位置
以上pragma设置参数时均可通过 [database.] 指定数据库
在 SQLite 中,主键可以是 NULL,这是与其他数据库不同的地方
SQLite中只有左联结