mysql服务器的SQL语句以即基本使用

一.SQL概述
结构化查询语言(Structured Query Language)简称SQL,是一种特殊目的的编程语言,是一种数据库查询和程序设计语言,用于存取数据以及查询、更新和管理关系数据库系统;同时也是数据库脚本文件的扩展名。

1.数据库相关工作职位大概两种:DBD和DBA
dba是数据库管理员database administrator dbd是数据库开发人员database developer

二.SQL语句结构
1.数据查询语言(DQL) 其语句,也称为“数据检索语句”,就是从表中查询数据

2.数据操作语言(DML) 在表中添加,修改和删除数据记录。也称为动作查询语言。

3.事务处理语言(TPL) 它由多条sql语句组成的整体,它的语句能确保被DML语句修改的表中的所有记录及时得到更新。

4.数据控制语言(DCL) 设置数据库的访问权限。

5.数据定义语言(DDL) 在数据库中创建表或删除表(CREAT TABLE 或 DROP TABLE);为表加入索引等。

6.指针控制语言(CCL) 它的语句,像DECLARE CURSOR,FETCH INTO和UPDATE WHERE CURRENT用于对一个或多个表单独行的操作。

三.实验环境
1.查看数据库有3种方式
(1) mysql>show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
±-------------------+
4 rows in set (0.00 sec)

(2) mysql> show databases \G #以行的方式显示
*************************** 1. row ***************************
Database: information_schema
*************************** 2. row ***************************
Database: mysql
*************************** 3. row ***************************
Database: performance_schema
*************************** 4. row ***************************
Database: sys
4 rows in set (0.00 sec)

(3) [root@cong11 ~]#mysql -e 'show databases' -uroot -p123456
在shell中查看
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
±-------------------+
mysql -e后面直接跟sql语句,这种方式一般是在shell脚本中用到

**information_schema这个数据库保存了MySQL服务器所有数据库的信息。如数据库名,数据库的表,表栏的数据类型,访问权限等。

performance_schema 这是MySQL5.5新增的一个性能优化的引擎:命名PERFORMANCE_SCHEMA,主要用于收集数据库服务器性能参数。MySQL用户是不能创建存储引擎为PERFORMANCE_SCHEMA的表

mysql库是系统库,里面保存有账户信息,权限信息等。

mysql5.7增加了sys 系统数据库,通过这个库可以快速的了解系统的元数据信息,元数据是关于数据信息的数据,如数据库名或表名,列的数据类型,或访问权限等。**

2.创建数据库
(1) 创建数据库注意事项
在文件系统中,MySQL的数据存储区以目录方式表示MySQL数据库。因此,上面命令中的数据库名字必须与操作系统的约束的目录名字一致。例如不允许文件和目录名中有,/,:,*,?,”,<,>,|这些符号,在MySQL数据库名字中这些字母会被自动删除。
[root@cong11 ~]#ls /data/mysql/data/
在这里插入图片描述
(2) 创建一个数据库
mysql>create database HA;
mysql服务器的SQL语句以即基本使用_第1张图片
我们可以去数据目录下查看新创建的数据库目录,ls /data/mysql/data/
这时我们发现多了一给HA的数据库 在这里插入图片描述
(3)选择要操作的数据库,并查看自己在所处的位置
mysql服务器的SQL语句以即基本使用_第2张图片
(4) 删除数据库
mysql> drop database HA;**
mysql服务器的SQL语句以即基本使用_第3张图片
(5)移动数据库目录
[root@cong11 ~]# mkdir /db_backup
[root@cong11 ~]# mv /data/mysql/data/HA /db_backup
mysql服务器的SQL语句以即基本使用_第4张图片
发现HA数据库已经不见了,cd进
/data/mysql/data/**这个目录下再 mv /db_backup HA 移回来

(6)使用IF EXISTS 子句以避免删除不存在的数据库时出现的MySQL错误信息, 也可以在创建数据库时使用
mysql>drop database if exists HA; #如果存在则删除
mysql> create database if not exists HA; #if not exists 如果不存在则创建

3.创建表
语法:create table 表名 (字段名 类型, 字段名 类型, 字段名 类型);
(1)创建
mysql> create table student(id int(20),name char(40),age int);
mysql服务器的SQL语句以即基本使用_第5张图片
这里我那另一个数据库创建的表

(2)查看表相关信息
mysql> use baqn;
mysql> show tables;

mysql服务器的SQL语句以即基本使用_第6张图片
(3) 查看表结构
mysql>desc student;
mysql服务器的SQL语句以即基本使用_第7张图片
(4)查看创建表执行了哪些命令
mysql> show create table student \G

*************************** 1. row ***************************
Table: student
Create Table: CREATE TABLE student (
id int(20) DEFAULT NULL,
name char(40) DEFAULT NULL,
age int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

(5)指定默认存储引擎和字符集
mysql> create table student2(id int(20),name char(40),age int) ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql> show create table student2 \G

*************************** 1. row ***************************
Table: student2
Create Table: CREATE TABLE student2 (
id int(20) DEFAULT NULL,
name char(40) DEFAULT NULL,
age int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

(6)删除表
mysql> drop table student2;
mysql服务器的SQL语句以即基本使用_第8张图片
(7)修改表名称
语法:alter table 表名 rename 新表名;
mysql> alter table student rename students;
mysql服务器的SQL语句以即基本使用_第9张图片
(8)修改表中的字段类型
语法:alter table 表名 modify 要修改的字段名要修改的类型;
mysql> alter table students modify id int(10);

±------±---------±-----±----±--------±------+
| Field | Type | Null | Key | Default | Extra |
±------±---------±-----±----±--------±------+
| id | int(10) | YES | | NULL | |
| name | char(40) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
±------±---------±-----±----±--------±------+
3 rows in set (0.00 sec)

(10)修改表中的字段类型和字段名称
语法:alter table 表名 change 原字段名 新字段名 新字段类型;
mysql> alter table students change name stname char(20);
mysql服务器的SQL语句以即基本使用_第10张图片
(11)CHANGE 和MODIFY的区别
CHANGE 对列进行重命名和更改列的类型,需给定旧的列名称和新的列名称、当前的类型。 MODIFY 可以改变列的类型,此时不需要重命名(不需给定新的列名称)

(12)在表中添加字段
语法:alter table 表名 add字段名 字段类型;
enum #列举类型,比如性别,只能在男女选择,是男非女,是女非男
mysql> alter table students add sex enum('M','W');
mysql> desc students;mysql服务器的SQL语句以即基本使用_第11张图片

(13)在表中指定位置添加字段
1.在第一列添加一个字段
语法:alter table 表名 字段名 first;
mysql> alter table students add uid int(10) first;
mysql> desc students;
mysql服务器的SQL语句以即基本使用_第12张图片
2.在age后面添加一个address字段
语法:alter table 表名 after 字段名;
mysql> alter table students add address char(40) after age;
mysql> desc students;
mysql服务器的SQL语句以即基本使用_第13张图片
3. 删除表中字段
语法:alter table 表名 drop 字段名 ;
mysql> alter table students drop address;
mysql> desc students;
mysql服务器的SQL语句以即基本使用_第14张图片
这时我们发现address字段已被删除

4.关于表中记录的操作
(1)插入字段<记录>INSERT
语法:insert into 表名values (字段值1,字段值2, 字段值3); 插入记录时要对应相对的类型
先删除students表,再创建
mysql> drop tables students;
mysql>create table students(id int(20),name char(40),age int);
mysql> desc students;
mysql服务器的SQL语句以即基本使用_第15张图片
(2)插入记录
mysql> insert into students values(1,'zhangs',21); # 插入单条记录

mysql> insert into students values(2,'lis',24),(3,'wange',26); #同时插入多条记录

mysql> insert into students (id,name)values(4,'hangl');#分开插入表记录

5.查询表中记录select
语法:select * from 表名称; # *号表示表中所有的字段
(1) mysql> select * from students;#查询students表中所有记录
mysql服务器的SQL语句以即基本使用_第16张图片
(2) mysql> select * from student\G#当表中记录比较多时可以使用\G查看
mysql服务器的SQL语句以即基本使用_第17张图片
(3) mysql> select name from students;#只查询表中某个字段的内容
mysql>select id,name from students;
mysql服务器的SQL语句以即基本使用_第18张图片
mysql服务器的SQL语句以即基本使用_第19张图片
6.查看别的数据库的表或者不在本数据库上进行查看
语法:SELECT 字段 FROM 数据库名.表名; 效果等同于先使用use数据库,然后再看看表内容
mysql> select * from bdqn.students;
mysql服务器的SQL语句以即基本使用_第20张图片
7.删除表中的记录
(1) 删除students表中id为3的行
mysql> delete from students where id=3;
发现表中id为3的记录不见了
mysql服务器的SQL语句以即基本使用_第21张图片
(2)删除age为空的行
mysql> delete from students where age is null;
mysql服务器的SQL语句以即基本使用_第22张图片
8.更新记录
(1)把表中id为2的记录age更新为25
mysql> update students set age='25' where id=2;
mysql服务器的SQL语句以即基本使用_第23张图片
(2)把表中所有的id都更新为2
mysql> update students set id=2;
mysql服务器的SQL语句以即基本使用_第24张图片
(3)同时更新多条记录,请使用逗号隔开
mysql> update students set id=1,name='zhangsan' where age=21;
mysql服务器的SQL语句以即基本使用_第25张图片
9.SQL基础条件查询语句
首先往表中插入一些数据
mysql> insert into students values(2,'lis',25),(3,'wange',26),(4,'libin',28),(5,'tom',30),(6,'sorry',24);
Query OK, 3 rows affected (0.29 sec)
Records: 3 Duplicates: 0 Warnings: 0

(1)查询STUDENTS表中的NAME,AGE
mysql服务器的SQL语句以即基本使用_第26张图片
(2)去重复查询distinct
mysql> select distinct name,age from students; #可以看见重复的行不在了
mysql服务器的SQL语句以即基本使用_第27张图片
(3)使用AND和OR进行多条件查询
1.查询表中id>3和age>25的记录
mysql> select id,name,age from students where id>3 and age>25;
mysql服务器的SQL语句以即基本使用_第28张图片
2.查询表中id>3 或者 age>25的记录
mysql> select id,name,age from students where id>3 or age>25;
mysql服务器的SQL语句以即基本使用_第29张图片
10.MYSQL区分大小写查询binary
Mysql默认查询是不区分大小写的
BINARY是类型转换运算符,它用来强制它后面的字符串为一个二进制字符串,可以理解为在字符串比较的时候区分大小写。
(1) 插入大写记录
mysql> insert into students values(7,'KILL',32),(8,'kill',32);
mysql服务器的SQL语句以即基本使用_第30张图片
(2) 区分大小写查询
mysql>select * from students where binary name='kill';
mysql> select * from students where binary name='KILL';
mysql服务器的SQL语句以即基本使用_第31张图片
11**.MYSQL查询排序distinct**
语法:select distinct 字段1,字段2 from 表名order by 字段名;
(1)默认为升序asc
mysql> select distinct id from students order by id asc;
mysql服务器的SQL语句以即基本使用_第32张图片
(2)降序desc
mysql> select distinct id from students order by id desc;
mysql服务器的SQL语句以即基本使用_第33张图片

你可能感兴趣的:(mysql)