mysql基础使用

22 阅读3分钟

www.cnblogs.com/E-star/p/33…
1、数据库的安装
·next
·联网,自动下载资源
·注意记住自己的用户名和密码,否则无法登录数据库
root:root
·默认是安装在C盘,有三个文件夹,只有找到MySql目录都是安装信息

2、环境变量的配置
win10:
选中我的电脑—>点击右键—>属性—>高级系统设置—>环境变量
—>系统变量—>path变量—>双击打开—>新建—>
—>粘贴C:\Program Files\MySQL\MySQL Server 8.0\bin—>确定

win7:
在编辑中粘贴C:\Program Files\MySQL\MySQL Server 8.0\bin;到最前面,
后面的分号一定不能少--->确定

3、启动和关闭服务
控制面板—>调整成小图标—>管理工具—>服务—>直接输入不要犹豫在哪输入
—>mysql—>找到MySQL80/MySQL5.7服务—>选中点击右键—>启动—>停止

4、登录服务
使用CMD命令行打开终端—>
输入mysql -u root -p 回车
—>输入password:root —>进入系统

#-------------------------------------
C:\Users\root>mysql -u root -p
Enter password: ****
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.12 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
#-------------------------------------

解决:mysql不是内部命令的方法:
回到第2项,进行环境变量的配置
解决卸载不能重复安装:
卸载window服务:
在命令行中输入:sc delete MySQL80
在windows就没有MySQL80,MySQL,MySQL5

5、使用工具连接mysql
打开Navicat Premium12—>点击连接—>选中mysql—>弹出连接对话框—>
输入连接名称:随意
输入主机名:localhost,127.0.0.1,ip地址
输入端口号:3306
输入用户名:你的用户名
输入密码:你的密码
—>点击测试连接—>连接成功
—>在左边视图区看到连接—>双击打开连接—>看到数据库

解决无法连接:
1、可能没有启动服务,启动服务
22059异常:
	mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

6、创建数据库
使用终端命令创建:高手(需要经验,熟练)
use mysql;
create database ui10Test;

使用客户端编辑器进行操作
	选中连接名称点击右键--->新建数据库--->输入数据库名次--->确定
	sql:CREATE DATABASE `ui10_test` CHARACTER SET 'utf8';

7、创建表
·字段
就是列(column),就是对象的属性
let person= {name:“tom”,age:20,id:1,birth:new Date()};

	id	name	age
	1	tom		20	
	
·记录
	行(row),就是对象
	id	name	age
	1	tom		20	记录
	2	sam		19	记录
	
·表
	table,就是存放对象的数组,都是记录
			
·字段的属性
	字段的数据类型:typeof(id),typeof(name)
	int    ===>parseInt(num);
	bigint ===>parseInt(num);
	double ===>parseFloat(num);
	float  ===>parseFloat(num);
	varchar string
	date 日期类型
	
建表语句:
create table ui10_student(
	id int(10) PRIMARY key auto_increment comment '学生的id',
	username varchar(50) not null comment '用户名',
	`password` varchar(50) not null comment '密码'
);

8、添加数据
insert into ui10_student (id,username,password) values (1,‘test’,‘test’);
insert into:添加数据的关键字表示插入
ui10_student:表名称
(username,password):表字段
values (‘test’,‘test’):对应的字段值

9、查询数据
select count(id) from ui10_student;查询有几条数据
select * from ui10_student;查询所有
select * from ui10_student where id = 2;根据id进行查询
select * from ui10_student where id > 2;查询id>2所有记录
select id, username from ui10_student where username = ‘root’ and id<4;只显示id和username
select * from ui10_student where username = ‘root’ and id<4;查询username = ‘root’ 并且 id<4所有记录

10、修改数据
update ui10_student set username =‘root34’;就会把所有的username全部修改称root34
update ui10_student set username =‘root123’ where id = 1;b把id = 1的记录username值修改称root123

11、删除数据
delete from ui10_student;全部删除,不能执行执行操作
truncate table ui10_student;全部删除,重置序列
delete from ui10_student where id = 5;删除id=5的记录
delete from ui10_student where username=‘root123’ and id = 5;删除id=5且username='root123’的记录

12、node连接数据库

13、常用的sql语句
·use mysql:切换数据库到mysql
·create database ui10Test; 创建数据库ui10Test
·show tables;查看当前数据库所有表格
·show create table ui10_student;查看建表sql语句
·insert into ui10_student (username,password) values (‘test’,‘test’);添加数据
·select count(id) from ui10_student;查询有几条数据
·select count(id) from ui10_student;查询有几条数据
·select * from ui10_student;查询所有
·select * from ui10_student where id = 2;根据id进行查询
·select * from ui10_student where id > 2;查询id>2所有记录
·select * from ui10_student where username = ‘root’ and id<4;查询username = ‘root’ 并且 id<4所有记录
·select id, username from ui10_student where username = ‘root’ and id<4;只显示id和username
·update ui10_student set username =‘root34’;就会把所有的username全部修改称root34
·update ui10_student set username =‘root123’ where id = 1;b把id = 1的记录username值修改称root123
·delete from ui10_student;全部删除,不能执行执行操作
·truncate table ui10_student;全部删除,重置序列
·delete from ui10_student where id = 5;删除id=5的记录
·delete from ui10_student where username=‘root123’ and id = 5;删除id=5且username='root123’的记录
·alter table ui10_student add gender varchar(10); 添加字段gender