MySQL(基础篇)

MySQL(基础篇)


一、理解

MySQL是数据管理系统,实际上就是一个软件

提供的功能

  1. 封装了计算机通讯所需的配置
  2. 提供了一套规则,通过它我们可以对另一台计算机上的数据进行操作

二、下载和安装MySQL步骤

  1. MySQL下载网址:https://dev.mysql.com/downloads/mysql(下载zip版本)
  2. 将解压包放至你想储存的地方
  3. 与bin同目录下,手动创建data目录,然后cmd在bin目录下执行下述代码
mysqld --initialize-insecure	# 为service端生成基本的配置文件
  1. 启动mysql服务
# cmd在bin目录下,执行下述代码
mysqld		# 启动服务端(等待客户端的连接)
mysql -u user_name -h ip地址 -p	# 启动客户端;用户在本地时,-h可以不写

三、快捷启动MySQL服务

  1. 将bin目录,添加到环境变量中
  2. 制作windows服务
# 制作 ("..." means directory of bin)
...\mysqld --install
# 移除
...\mysqld --remove
# 启动
net start mysql
# 关闭
net stop mysql

四、MySQL数据库基础知识

1. 认识数据库成员

  • 数据库:相当于一个文件夹
  • 数据库表:相当于一个文件
  • 数据行:文件中的一行数据

2. 数据库的数据类型

数值型数据

bit(n)		-- 只能放二进制数0/1,n表示二进制数的长度,超过长度的数字会报错,n(1~64)
tinyint,smallint,int,bigint		-- all is int,but range is different

-- the following types are float
decimal(gross_length, decimal_length)		-- precise
float		-- reduce precision as number increasing
double		-- reduce precision as number increasing

字符串型数据

char(n)						-- 定长数据类型,搜索数据快,n(1~255)
varchar(n)					-- 变长数据类型,节省空间,搜索速度相比较定慢n(1~255)
text,mediumtext,longtext	-- 变长,可接收较多的数据,区别是可接收的数据长度不一样

二进制型数据

TinyBlob,Blob,MediumBlob,longBlob		-- 区别是最大能接收的数据不同

时间型数据

date YYYY-MM-DD		-- 1998-02-22
time HH:MM:SS		-- 17:55:36
year YYY			-- 1999
datetime YYYY-MM-DD HH:MM:SS

3. 常用命令

show databases;		# 查看当前mysql的所有数据库
create database 数据库名;		# 创建数据库
use 数据库名;		# 进入数据库
show tables;		# 查看当前数据库下的表
create table 表名(nid int, name varchar(20), pwd varchar(64));		# 创建数据库表(简单式)
select * from 表名;		# 查看表中的所有数据行,“*”可替换成想选择的列名
insert into 表名(nid, name, pwd) values(1,"arno", "123");		# 往表中插入数据

4. 用户管理命令

create user "用户名"@"ip地址" identified by "密码";		  -- 创建用户
drop user "用户名"@"ip地址";									-- 删除用户
rename user "用户名"@"ip地址" to "新用户名"@"ip地址";		    -- 修改用户
set password for "用户名"@"ip地址" = password("新密码");	  -- 修改密码

5. 权限管理命令

# 权限:select(查看) or insert(插入)
# %:可放在ip中,表任意项
grant 权限 on 数据库.表 to "用户名"@"ip地址";

五、MySQL数据库级别的命令

1. 创建数据库

create database 数据库名;
create database 数据库名 default charset utf8 collate utf8_general_ci;
create database 数据库名 default character set gbk collate gbk_chinese_ci;

2. 删除数据库

drop database 数据库名称;

3.修改数据库字符集

alter database 数据库名 character set utf8 collate utf8_general_ci;

六、MySQL数据库表级别的命令

primary key 意味着将该列设为主键,一张表只能有一个主键,主键唯一不能重复且不为null
auto_increment 意味着将该列设为自增列表,从1开始
key 意味着将该列设为唯一列表,列值唯一不能重复,但可以为null

1. 创建数据库表

create table table_name(
	nid int not null auto_increment primary key,
	column_name type_name is_none(null/not null) default value
)engine=innodb default charset=utf8;

2. 创建外键

create table color(
	nid int not null primary key,
	name char(16) not null,
	)engine=innodb default charset=utf8;

create table fruit(
	nid int not null primary key,
	smt char(16) not null,
	color_id int not null,
	constraint fk_cc foreign key(color_id) references color(nid),
	)engine=innodb default charset=utf8;

3. 修改数据库表命令

-- add new column
alter table tb__name add column_name type_name;
-- delete column
alter table tb_name drop column column_name;
-- modify column
alter table tb_name modify column column_name type_name;		-- modify the type of column
alter table tb_name rename column old_column_name to new_column_name;		-- the olumn_name were modified

-- add primary key
alter table tb_name add primary key(column_name);
-- delete primary key
alter table tb_name drop primary key;
alter table tb_name modify column_name type_name, drop primary key;

-- add foreigh key
alter table followed_tb_name add constraint foreign_key_name foreign key followed_tb_name(column_name) references main_tb_name(column_name);
-- delete foreign key
alter table tb_name drop foreign key foreign_key_name;

-- modify default value
alter table tb_name alter column_name set default value;
alter table tb_name alter column_name drop default;

4. 查看及删除数据库表

desc table_name;			# 查看表的基本信息
drop table table_name;		# 删除表
delete from table_name;		# 清空表的内容,自增不会回到原点
truncate table table_name;	# 清空表的内容,自增回到原点

七、MySQL数据库行级别

1. 增删改查命令

增加

insert into tb_name(column_name,) values(value,),(value,);
insert into tb_name(column1_name,column2_name) select column3_name,column4_name from tb_name2;
insert into tb_name1(column1_name, column2_name) select column3_name, column4_name from tb_name2;

删除

delete from tb_name where condit1 and/or condit2;

修改

update tb_name set column_name1=value,column_name2 where condit;

查询

select column1_name,column2_name from tb_name where condit;

八、MySQL数据库的条件语句

as 可以为子查询取别名

1.逻辑运算语句

where condit_A and condit_B;
# 可以输入数值、文本或日期
where column_name between value1 and value2;
where in/not in (selsect column_name from tb_name);

2.通配语句

where column_name like "xxx_"	-- 匹配第一个以xxx开头的行
where column_name like "xxx%"	-- 匹配第所有以xxx开头的行

3.分页语句

select * from tb_name limit 5;				-- 取前五行
select * from tb_name limit 5,2;			-- 从第五行开始,取2行
select * from tb_name limit 2 offset 5;		-- 和上同(推荐)

4.排序语句

select * from tb_name order by column_name asc;		-- 顺序排序
select * from tb_name order by column_name desc;	-- 倒序排序
select * from tb_name order by column1_name asc,column2_name desc;		-- 先对列1顺序排序,再对列2倒序排序

5.分组语句

select num from tb_name group by num;
select num,nid from tb_name group by num,nid;
select num,nid from tb_name where nid>0 group by num,nid order nid desc;

九、补充知识点

1. 临时表(子查询)

select age,name from (select nid,name,age from tb1 where nid>10)as T;

2. 列表去重

select distinct column_name from tb_name;

3. 笛卡尔机

Take out a datum from tb2 after all data of tb1 were looped to show,and until all data of tb2 were took out,the loopl end
The number of loop depend on the row's num of tb2

select * from tb1_name, tb2_name;

4. Navicat

1. The commands of add,delete,modify,search can be executed through clicking mouse in the software 
2. you can also use sql command through set up the statement of inquiry

5. mysql的注释语法

select * from tb1 	-- annotation

6. 导入导出数据库文件

Behind the "-p" do not need to add password
"-d" means don't need to export the data of database
if your ip is local host, "-h" can be ignored

-- Export the file of database
...\mysqldump -h ip -u user_name -p -d databases_name > export_path 	-- data + sql statement of structure
...\mysqldump -h ip -u user_name -p /databases_name > export_path 	-- sql statement of structure

-- Import the file of database
...\mysqldump -h ip -u user_name -p databases_name < export_path 	-- sql statement of structure
posted @ 2021-04-16 12:23  notesForKai  阅读(101)  评论(0编辑  收藏  举报