数据库 DB
- 数据库本身支持并发的网络操作
- 使用数据库对数据的增删查改比把数据存储在文件中处理文件效率高
- 每个文件夹是一个数据库
数据库管理系统DBMS:sqlserver mysql oracle
-
安装
-
下载:https://downloads.mysql.com/archives/community/ 选择版本,下载zip包
-
部署:
- 把包解压到d盘mysql文件夹路径
-
访问:在bin目录下可以用命令行提示符,默认没有密码
mysql -uroot -p --登录
-
显示下所有数据库包:
show databases;
-
显示当前用户:
select user(); --只有一个root@localhost
-
给当前用户创建密码:
set password=password('123')
-
创建用户:
create user 'guset'@'localhost' identified by '123' --(本地能用,‘guest’@‘localhost’ 这是固定格式)
-
创建用户:
create user 'tom'@'192.168.2.%' identified by '123' --(所有192.168.2网段里的人可以用tom 访问我的库,密码123)
-
创建库(create database + 库名)
create database test1
-
授权(grant ) 有 select delete insert alter 等:
grant select on test1.* to 'guest'@'localhost' --给guest用户只赋予select权限
-
如果没有生效 可以刷新
flush privileges;
-
远程登录别人的库
mysql -uguest -p123 -h 192.168.2.111 --用户是guest 密码是123 库在2.111上
-
创建账号并授权:
grant select on test1.* to 'jerry'@'192.168.2.111' indentified by '111' --创建用户jerry 他可以在2.111上登录我的mysql 并用密码111
-
mysql 默认端口是3306
-
切换到数据库里:
use test1
-
显示所有的表(tables)
show tables;
-
删除数据库:
drop database test1;
-
创建一个表:
create table t1(id int,name char(20), age int) --id name 和 age三个列
-
插入数据:
insert into t1 values(101,'tom',25)
-
查询数据:
selet * from t1;
-
修改
update t1 set age=22 where id=101
-
删除表记录
delete from t1 where id=101 --一定要记得加where子句,要不然容易全删除
-
-
存储引擎
-
一张表:数据,索引,表结构 三个内容
-
数据和索引存储在一起,一张表可以分2个文件存储 (Innodb引擎)
- 支持事务:一个事务里可能有多个操作(select insert 等),他们要么同时成功,要么就没发生,绑定在一起就是一个事务,一旦事务执行到一半结束断电就回滚
- 支持行级别锁:行级锁,表级锁,修改行少的时候用行级锁,否则表级锁,行级锁让效率增加,在修改数据频繁的时候有优势。
- 支持外键:约束两张表中的关联字段不能随意添加或删除操作
-
数据和索引不存储在一起,一张表可以分3个文件存储 (Myisam引擎)
-
数据存储在内存中,数据断电消失,只需要存储在1个文件中,而且只存表结构(Memory引擎)
-
显示数据库引擎
show engines; --默认是Innodb
show variables like '%engine%'
-
添加数据库(文件夹)
-
添加完数据库student,并生成一个表t1,会看到3个文件,db.opt先不管,t1.frm是表t1的框架(表结构),t1.ibd是默认的innodb引擎的数据表
-
如果在创建表的时候 加上(engine=引擎类型)就可以指定他的存储引擎:
create table course(id int,cname char(20),price int) engine = Myisam; --这样创建的表会生成三个文件course.frm(框架),course.MYD(数据),course.MYI(索引)
-
还可以创建memory引擎的表:
create table t2(id int,cname char(20),price int) engine = memory; --创建内存引擎的表格,结果数据库文件中只有一个t2.frm文件
-
还可以一次性插入多个数值和指定值
insert into t1 values(102,'jerry',22),(103,'lucy',20); --2条记录 insert into t1(id,name) values(104,'lily'); --只插入id 和name ,age字段会默认null
- desc 显示表结构:
desc student; --查看student表结构
+-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+
-
mysql常用数据类型:int float char datetime
-
datetime类似的还有 year date time timestamp
create table t_datetime(dt datetime,y year,d date,t time,ts timestamp);
-
用show create table的方式查看表t_datetime 看到timestamp后面跟了一串
NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
这个约束作用是,每次更新记录,这个值都会记录成当前的日期时间,可以用于记录客户最后一次登录操作等,需要留痕的地方,以后针对日期类型的字段可以自由添加达到效果
-
字符串,char(最大255,在查询的时候会自动把空格去掉) 用来存身份证号,手机号,用户名,密码等 varchar(最大65536)
-
enum 单选行为 set 多选行为
create table t2(id int,name char(10),gender enum('male','female')); insert into t2 values(1001,'tom','male'); insert into t2 values(1001,'tom',); -- 不写提示warning
create table t3(id int,name char(10),hobby set('游泳','爬山','旅行','音乐','读书')); insert into t3 values(1001,'tom','游泳,爬山,读书'); insert into t3 values(1002,'jerry','音乐,读书'); --这里写在单引号内 逗号隔开,在范围内的会去重,不在范围内的会剔除 mysql> select * from t3; +------+-------+----------------------+ | id | name | hobby | +------+-------+----------------------+ | 1001 | tom | 游泳,爬山,读书 | | 1002 | jerry | 音乐,读书 | +------+-------+----------------------+
-
表的完整性约束:不为空,不能重复(唯一约束),无符号,默认值,自增,主键,外键
create table t1(id int unsigned,name char(10)); --无符号
create table t1(id int not null,name char(10) not null);--容易无效,依然可以插入空的,可以在配置文件my.ini添加一行 sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
mysql> create table t5(id int unsigned not null, -> name char(18) not null, -> sex enum('male','female') not null default 'male'); -- 设置默认值
create table t6(id int unsigned unique , name char(20)); --约束id不能为空 insert into t6 values(1,'tom'); insert into t6 values(1,'jerry'); --失败 --但是id可以插入2个null insert into t6 values(,'lucy'); insert into t6 values(,'lily');
create table t7(id int not null,process char(100),ip char(20),port char(5) ,unique(ip,port)); --把ip 和port 联合在一起,并且不能重复
mysql> create table t8(id int unique not null,name char(20) unique not null); --第一个设置 unique not null 的列 id 会自动设置成主键 PRI mysql> desc t8; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | char(20) | NO | UNI | NULL | | +-------+----------+------+-----+---------+-------+
create table t9(id int not null, process char(100), ip char(20) , port char(5) , primary key(ip,port)); --联合主键
create table t10(id int primary key auto_increment,name char(15)); --默认从1开始
创建学生表中有一个课程表中的cid 为外键
create table student(id int primary key, name char(), cid int, foreign key(cid) references course(cid)); --还可以约束foreign key(cid) references course(cid) on delete cascade on update cascade --update course set cid = 4 where cid=2 的时候,student cid 为2 的同时会变成4 create table course(cid int primary key, name char(20), price float(10,2));
-
数据类型
-
完整性约束
-
修改表的结构
-
多表联结