MySQL(基本介绍及常用命令)

一、window 系统终端命令基础安装

win+r 打开终端 :cmd中进行
安装: mysqld -install
启动: net start mysql
链接登录:
1)mysql -uroot -proot (用户名:root, 密码:root)
或者
2)mysql -hlocalhost -p3306 -uroot –proot
(-h代表主机地址 -p3306—MySQL默认端口未被占用的前提下)
或者
3)mysql -hlocalhost -p3306 -uroot -p "回车"
Enter password:

image.png

退出: exit或者quit;
image.png

二、数据库管理系统(DBMS)分类

1.基于共享文件系统的DBMS,如Microsoft Access和FileMaker。
2.基于客户机—服务器的DBMS,如MySQL(默认端口号:3306)、 Oracle(默认端口号:1521)以及Microsoft SQL Server(默认端口号:1433);

三、SQL(Structured Query Language)

1】SQL是一种专门用来与数据库通信的结构化查询语言
2】数据库(database)保存有组织的数据的容器(通常是一个文件或一组文件)。
3】表(table) 某种特定类型数据的结构化清单。
4】列(column) 表中的一个字段。所有表都是由一个或多个列组成的。
5】数据类型(datatype) 所容许的数据的类型。每个表列都有相应的数据类型,它限制(或容许)该列中存储的数据。
6】行(row) 表中的一个记录。

四、MySQL常用语法

【1】数据“库”语法

1.显示所有的数据库:show databases;
2.创建数据库:
1)create database databasename;
或者
2)create database [if not exists] databasename;
例如:create database students;
create database if not exists students1;
3.删除数据库: drop database databasename;
例如:drop database students;
4.导入sql文件: source 被导文件所在的路径/被导文件名
(若是错误则改成\)
source E:/MySQL/testdata/mysql_scripts/create.sql
source E:/MySQL/testdata/mysql_scripts/populate.sql
5.查询版本号:select version();
6.查看报错信息: show errors;
7.选中数据库:use databasename;(分号可省略)
例如:use students1

【2】“表格”语法

1.显示所有可能的表:show tables;
2.显示表结构:
1)desc tablename;
2)show columns from tablename;
3)show full columns from tablename; 相对于上2条更详细(备注等信息)
例如:desc students1;
show columns from students1;
show full columns from students1;

【3】简单数据查询语法:

1.查询所有列: select * from tablename;
例如:select * from students1;
2.查询多列: select columnname1,columnname2,…,columnnameN from tablename;
例如:select id,name,age from students1;
3.查询单列: select columnname from tablename;
例如:select id from students1;
4.去重复,查询不同的数据:distinct
例如:
select distinct prod_price from products;
select distinct prod_price,prod_desc from products;
5.排序:order by 默认升序(asc),降序(desc)
例如:
select prod_price from products order by prod_price;
select prod_price from products order by prod_price asc;
select prod_price from products order by prod_price desc;
select prod_price,prod_id from products order by prod_price desc ,prod_id; 表示先进行价格降序排列,再进行编号升序排序
6.限制查询:limit
limit n:显示前n条记录
limit m,n:显示从行m(第m+1行)开始的n行;
例如:
select prod_price from products limit 5;
select prod_price from products limit 5,3;
limit 和order by联用可以显示最高或者最低的几个;
例如:
select prod_price from products order by prod_price desc limit 3;
select prod_price from products order by prod_price desc limit 1;
select ceshi81.products.prod_price from ceshi81.products order by prod_price desc limit 1;

【4】过滤数据查询法:where
  1. = , > , < ,>= , <=, != or <> 不等于
    例如:
    select prod_id from products where vend_id=1001;
    select * from products where prod_price>10;
    select * from products where prod_name='apple';
    select * from products where prod_name>'Fuses'; 字符从第一个字符开始比较,比F大;若相同则进行第二个字符比较比u大
    select distinct prod_name from products where vend_id=1003 limit 3;
    2.区间: between and 含边界
    Not between and
    例如:select prod_price from products where prod_price between 10 and 35; 10<=prod_price<=35
    select prod_price from products where prod_price not between 10 and 35;
    3.空值查询: is null
    is not null
    例如: select * from vendors where vend_state is NULL;
    select * from vendors where vend_state is not NULL;
【5】组合where子句查询法:

1.and 同时满足
select * from products where vend_id=1003 and prod_price <=10;
2.or 满足其中一个条件
select * from products where vend_id=1003 or vend_id=1005;
select * from products where vend_id=1003 or prod_price <=10 ;
select * from products where vend_id=1003 or vend_id=1005 and prod_price<=10.0; and优先级比or高
select * from products where vend_id=1003 or (vend_id=1005 and prod_price<=10.0); 先进行括号里面的筛选
3.in 操作符(等同于or) [not in]
select * from products where vend_id=1003 or vend_id=1005 or vend_id=1001;
select * from products where vend_id in(1001,1003,1005);
综合案例:
select distinct prod_price
from products
where vend_id in (1001,1003,1005)
order by prod_price desc
limit 3;

你可能感兴趣的:(MySQL(基本介绍及常用命令))