数据库基础及用户管理授权

数据库概念

关系型数据库 数据结构二维表格 库 -> 表 -> 列(字段):用来描述对象的的一个属性;行:用来描述一个对象的信息

mysql(5.7/8.0) maridb ocracle postgresql sqlserver(windows)

RDB阿里云数据库 高斯(华为的) TDBA(腾讯的)ocenabase(阿里的)人大金仓 达梦

非关系型数据库

缓存型的 redis memcache

文档型的 mongoDB

搜索型的 elasticserch

时序型的 prometheus(监控数据)

常见操作系统 欧拉,乌班图apt安装deb包,

键值对 k/y key/value 键/值

sql语句 DDL创建数据库对象 DML内容 DQL搜索 DCL控制语句

DDL 用于创建数据库的对象:库 表 索引

create database 库名;

create table 表明 (字段名1 数据类型 【字段属性】,【字段属性】 . . . .);

show databases;查看库

use 库名;

show tables;

show tables from 库名;

desc 表名;查表得结构

DML 用于管理表数据

insert into 表名 (字段1,字段2,. . . . )values(对应字段1的值,字段2的值, . . . . );

insert into 表名 values (按照字段顺序的所有字段的值);

delete from 表名 where 条件表达式;

update 表明 set 字段=值 ,. . . . where 条件表达式;

DQL 用于根据条件查询表数据

select 字段1,字段2 ,. . .  from 表名 where 条件表达式;

select * from 表明 limite N;N查看表前几行

select * from 表明 limite N,M;查看N行之后的多少行到M前那行(不包含第N行)

select * from 表明/G;按竖向结构查看

需改表结构 alter table 旧表名 rename 新表名;

增加表字段 alter table 表明 add 新字段 数据类型 字段属性;

修改表字段名 alter table 表明 cahnge 旧字段名 新子段名 字段属性;

删除字段 alter table 表名 drop 字段名;

 char和varchar的区别 char大小固定值,小的往后空格,varchar可变的最小为1

案例扩展
use school;
create table if not exists info (
id int(4) zerofill primary key auto_increment,     #指定主键的第二种方式
name varchar(10) not null,
cardid int(18) not null unique key,
hobby varchar(50));
------------------------------------------------------------------------------------------------------------------------
#if not exists:表示检测要创建的表是否已存在,如果不存在就继续创建
#int(4) zerofill:表示若数值不满4位数,则前面用"0"填充,例0001
#auto_increment:表示此字段为自增长字段,即每条记录自动递增1,默认从1开始递增;
自增长字段数据不可以重复;自增长字段必须是主键;如添加的记录数据没有指定此字段的值且添加失败也会自动递增一次
#unique key:表示此字段唯一键约束,此字段数据不可以重复:一张表中只能有一个主键,但是一张表中可以有多个唯一键
#not null:表示此字段不允许为NULL

主键字段:不能为空,一个表中只能有一个主键,所有字段中主键字段唯一

唯一键:可以空,一个表可有多个唯一键。但一个表内不能重复

自增长命令执行失败id会自增一边

create table if not exists ky27 (id int(4) zerofill primary key auto _increment,name char(10) not null default 'nobody',phone int not null,sex char(4));

insert into ky27 (name,phone,sex) values ('zhangsan',123456789,'男');

alter table ky27 add unique key(phone);

克隆表

法一create table test1(新表) like test2(旧表);

insert into test1 select * from test2 ;

法二

create table test2 (select * from test2);

会出现新旧表结构不一样

清空表法一

delete from  test1 ;用delete删除时自增长字段仍然会按照星空前的顺序自增,一条一条删,清空效率慢

法二 truncate table test1;直接重建表,清空效率快,新表自增长从1开始

创建临时表(当前会话当中)

show tables看不到表名

create temproary  table test1 (id int,name char(10),sex char(4))

mysql 六大常见约束

主键约束 primay key

唯一键约束 unique key

非空约束 not null

默认值约束 default

自增约束 auto_increment

外键约束 foreign key 两个表关联表的的内键与另一个表的外键捆绑。

外键的定义:如果同一个属性字段X在表一中是主键,而在表二中不是主键,则字段X称为表二的外键。

创建主表 profession

create table prof (pid int, pname char(10));

create table student (id int, name varchar(10),age int, proid int);

pid 与proid相关

alter table prof add primary key(pid);

desc prof;

alter table student add constraint FK_pro_foreign key (proid) references prof (pid);插入数据时必须先给主表插入数据insert into prof values (1,'大数据')

insert into student values (1,'yht',26,1);

外键表删delete from student

查看表结构 show create table student\G更详细。

alter table student drop foreign key FK_PRO;删除表结构

DCL

数据库用户管理

create user '用户名'@'源地址' identified by '密码';(源地址为localhost/%)

select user,local,authentcation_string form mysql.user;

查看用户

'root'@'localhost'默认用的localhost登录

'root'@'%' 不同ip链接用户

新建用户 create user 'zhangsan'@'localhost' identified by 'abc123';

select

 password('123456');

create user 'lisi'@''localhost’

select user()查看当前登录用户

rename user 'lisi'@'localhost' to 'wangwu'@'localhost'

drop user 'wangwu'@'localhost';

set password = password('123456');该当前用户密码

set password for 'zhangsan'@'localhost' =password('abc123')只等用户修改密码

寻找root密码

vim /etc/my.cnf 可以在【mysqld】下添加skip-grant-tables

修改

 

重启mysql

systemctl restart mysql

mysql

uae mysql

desc user

update user set authenticaton_string=paassword('abc123') where user='root' and host='localhost';

flush privileges;刷新

改完后

还原修改配置文件

vim /etc/my.cnf

 刷新数据库

数据库的用户授权

grant 权限1,权限2,. . . . on 库名.表明 to '用户名'@'源地址' [identified by '密码']

授权用户权限是 all privilege。这个all privilege 都有哪些权限?all privilege 权限如下

insert (插入数据)select (查询数据)

update (更新表的数据)

delete (删除表中数据)

create (创建库,表)

(删除库,表)drop

refernces

index(建立索引)alter(更改表属性)

create temp orary tableslock tables (锁表)

execute

create view (创建视图(显示视图)show viewcreate routine (创建存储过程alter routine(修改存储过程)event (事件)

trigger on(创建触发器)

grant select,insert,create on kgc.* to 'lisi'@'%' identified by '123456';

flush privileges;

mysql -ulisi -p123456 -h 192.168.232.105 -p 3306

查看用户自己有什么权限

show grants for 'lisi'@'%'

grant all privileges on *.* to 'zhagnsan'@'localhost';

撤销权限

revoke all on *.* from 'zhangsan'@'localhost';

删除用户

drop user 'zhangsan'@'localhost';

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值