MySQL数据库(增删改)

本文使用的是windows环境下的mysql
使用前需开启和关闭服务:
以管理员身份打开命令终端(57是我取的mtsql名字,不用深究):

  1. net start mysql57
  2. net stop mysql57

这里注意,如果要卸载服务,必须在菜单栏里选择卸载,不能用第三方卸载,否则再在同一台机子上安装将会极其麻烦!

安装成功后,菜单栏选择mysql line client unicode ,输入口令 就可以开始使用了。但是这个感觉好丑,所以接下来都用到了Navicat for MySQL工具。

1.对于为什么我们要使用数据库?
主要是使用数据持久化-将数据从一掉电数据就丢失的存储介质(内存)转移到持久存储介质(硬盘) 。
普通文件/二进制文件/Excel/数据库
数据库作用:主要是可以高效的存储和管理数据方便的检索数据

2.什么是关系型数据库?

  • 用二维表组织数据
  • 表-实体
  • 行-记录
  • 列-字段
  • 表与表之间有关系
  • 编程语言 - SQL - 结构化查询语言

3.数据库 数据库系统 数据库管理系统
Db - Database -数据的仓库(集散地)
DBS -包括了DB DBMS DBA(数据库管理系统)
(R)DBMS -(关系型) 数据库管理系统 - 管理数据库的软件
- MySQL - 小巧但是强大
- Oracle - 这个数据库可以做商业智能 安全强大 比较强大
- DB2 安全强大 商业智能
但是Oracle 和DB2 昂贵 一般像银行都会使用
- SQL server 这个性能一般 用微软开发才选 ,不然一般不选
- SQLite - 嵌入式数据库(移动端)
4.数据库服务器和客户端工具
起了服务器就要用工具连它
工具有:

  • SQLyog
  • Toad for MySQL
  • Navicat for MySQL
    (苹果系统装 Navicat premium比较强大)

SQL(Structured Query Language)
– 关系型数据库的编程语言
– DML(数据操作语言):insert 插入/delete删除/update更新
– DQL(数据查询语言):select
– DCL(数据控制):grant/revoke/begin/commit/rollback
– DDL(数据定义语言):create(创建)/drop(删除)/alter(修改)
– DML(数据控制语言):grant/revoke/begin/commit/rollback

如果指定的数据库存在则删除改数据库

drop database if exists 数据库名;

创建数据库并指定默认的字符集

create database 数据库名 default charset utf8;

切换到 数据库名 数据库;

use 数据库名;

关系型数据库是通过二维表来组织数据
下面2点要注意 :
– 创建学生表 名时(创建表命名最好加个t或者tb前缀,表命是表)
– 要设置主键(primary key) -能够标识唯一一条记录的列

删除表

drop table is exists tb_student

创建一个新的学生表,添加学号、姓名、性别、联系电话、出生日期,设置主键

create table tb_student
(
stuid int not null comment '学号',
sname  varchar(10) not null comment '姓名',
ssex bit default 1 comment '性别',
stel char(11) comment '联系电话',
sbirth  date comment '出生日期',
primary key (stuid)     -- 这里就是在设置主键
);

– 修改学生表

alter table tb_student add column saddr varchar(100);

– 删除表选项

alter table tb_student drop column stel;

– 插入学生记录

insert into tb_student values (1001,'王大锤',1,'1990-2-12','四川成都');
insert into tb_student (stuid,sname) values (1002,'骆昊');

– 直插入三项内容

insert into tb_student (stuid,sname,ssex) values (1003,'骆昊', 0);
insert into tb_student values 
(1004,'张三丰',1,'1940-12-3','湖北武汉'),
(1005,'黄蓉',0,'1950-3-3','湖北武汉'),
(1006,'杨过',1,'1987-02-3','湖北武汉');

– 删除数据

delete from tb_student where stuid=1003;  

这里要注意!删除的时候,千万不要删除整个表,提前做好备份!

– 更新数据
– 通常情况下更新或者删除单条数据都是以ID字段(主键)作为条件

update tb_student set sbirth ='1980-11-28',saddr ='四川德阳'
where sname ='骆昊';

– 修改多行

update tb_student set saddr='四川绵阳'
where stuid = 1004 or stuid =1005 or stuid =1006;

– 修改多行

update tb_student set saddr ='四川绵阳'
where stuid in (1004,1005,1006);

———————————————————————————————————————————————————————————-

-- 创建课程表(课程编号 课程名称 学分 )
create TABLE tb_scourse
( scid int  not null,
cname varchar(20) not null,
ccredit int not null,
PRIMARY key (scid)
);
-- 增加数据进去
insert into  tb_scourse (scid,cname,ccredit) values
(1111,'Python程序设计',4),
(2222,'HTML网页设计',2),
(3333,'Linux操作系统',1),
(4444,'数据库基础',1);

-- 创建人的表单
create table tb_person
(
personid int not null auto_increment,
pname varchar(20) not null,
pbirth date,
primary key(personid)
);
-- 创建卡的表单
create table tb_idcard
(
cardid char(18) not null,
cpolice varchar(20) not null,
cexpire date not null,
pid int not null,
-- pid 是要参照人id的 这样就把身份证绑定到了人上
PRIMARY key (cardid)
);

-- 外键约束
alter table tb_idcard add constraint fk_idcard_pid
foreign key (pid) references tb_person (personid);
-- 这里限定身份证id只能参照人的id

-- 唯一性约束
alter table tb_idcard add constraint uk_idcard_pid
unique(pid)
insert into tb_person (pname,pbirth) values
('骆昊','1980-11-28'),
('王大锤','1990-01-01');

insert into tb_idcard values
('510622198011289999','成都市金牛区公安局','2030-01-10',1),
('510622198343289999','北京朝阳区公安局','2050-11-10',2);

-- 银行卡
create table tb_account
(
accid char(16) not null,
balance decimal(16,2) default 0.0,
-- decimal  限制是小数 16位数 2位小数
pid int not null,
primary key (accid)

);
alter table tb_account add constraint fk_account_pid
foreign key (pid) references tb_person (personid);

-- 添加余额信息
insert into tb_account values
('1111222233334444',9999.99,1),
('1343435665233335',100,1);

insert into tb_account values
('1234443346554534',5000.55,2),
('5555555555555555',-100.0,2),
('5555222333555545',-100,2);


-- 这里新增一个scid 学生选课时 唯一标识
create table tb_sc
(
scid int not null auto_increment,
sid int not null,
cid int not null,
score float,
primary key (scid)
--  这里也可以写复合组键primary key(sid,cid) 但是不建议这样写 万一第二年继续考 就不行了
);
-- 复合主键(多个列合在一起作为主键)
-- 实际开发中一般都不用复合主键因为可能导致各种麻烦

-- 中间表建立关联
alter table tb_sc add constraint fk_sc_sid
foreign key (sid) references tb_student (stuid);

alter table tb_sc add constraint fk_sc_sid
foreign key (cid) references tb_course (courseid);

-- 插入信息
insert into tb_sc (sid,cid,score) values
(1001,1111,90),
(1001,2222,80),
(1002,1111,60),
(1002,3333,95),
(1005,4444,78);


-- 一夫一妻制 丈夫和妻子
-- 用户和订单 一对多关系
-- 读者和图书 多对多关系 

create table  tb_h
(
hid int not null comment '身份证',
hname varchar(20) not null comment '姓名',
wid int,
PRIMARY key (hid)
);
create table tb_w
(
wid int not null comment '身份证',
wname varchar(20) not null comment '姓名',
primary key (wid)
);

-- 修改表
alter table tb_h add constraint fk_h_wid
foreign key (wid) references tb_w (wid);

-- 唯一性约束
alter table tb_h add constraint uk_h_wid
unique (wid);


-- 建立参照完整性

insert into tb_h (hname) values ('王大锤');
insert into tb_h (hname) values ('郭靖');
insert into tb_w (wname) values ('柳岩');
update tb_h set wid=1 where hid =1;


on delete restrict;
-- 删除时不让删
insert into tb_user (username,userpass,nickname) values

-- DCL 以下就是:

-- 授予权限和召回权限
-- 创建用户并指定口令
create user hellokitty identified by '123123';
grant all on school.tb_student to hellokitty; 
-- 给学生表的所有权限

revoke all on school.tb_student from hellokitty;
-- 取消权限
grant select on school.tb_student to hellokitty;
-- 查询权限
grant all on school.*to hellokitty;
-- 赋予所有权限
grant all on *.* to 'hellokitty'@'%';
-- %这个是可以从所有地方登录
revoke all on *.* from hellokitty;
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值