数据查询语言(DQL:Data Query Language):其语句,也称为“”“数据检索语言”,用以从表中获得数据,确认数据怎样在应用程序给出,保留字段SELECT是DQL是用的最多的动词。
数据操作语言(DML:Data Manipulation Language ):其语句包括动词INSERT,UPDATE和DELETE,分别用于添加,修改,删除表中的行。也称为动作查询语言。
数据控制语言(DCL:DataControl Language):他的语句通过GRANT或者REVOKE获得许可,确定单个用户和用户组对数据库的访问,某些RDBMS可用GRANT和REVOKE控制对表单个列的访问。
数据定义语言(DDL:Data Define Language ):其语句包括动词CREATE和DROP,在数据库中创建新表或者删除表,为表加入索引,DDL包括许多人数据库目录中获得数据有关的保留字,他是动作查询的一部分。
数据库中一行是一个实体或者记录
一列是一个属性或者字段
mysql
-p 指定密码
-u 指定用户
-P 指定端口
-h 主机地址
-S 指定套接字文件,套接字文件是一种特殊的文件类型,用于通信,它提供了受文件系统访问控制保护的进程间通信。套接字就是支持TCP/IP网络通信的基本操作单元,linux以文件的形式实现套接口,与套接口相应的文件属于sockfs特殊文件系统,创建一个套接口就是在sockfs中创建一个特殊文件,并建立起为实现套接口功能的相关数据结构。
[root@localhost ~]# mysql -uroot -p123 -hlocalhost -P3306 -S /var/lib/mysql/mysql.sock #套接字文件默认存储位置
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 32
Server version: 5.7.43 MySQL Community Server (GPL)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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>
语法:show databases;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema | #虚拟库,主要存储了系统中一些数据库对象信息,例如用户表信息,列信息,权限信息,字符信息
| mysql | #授权库,主要存储系统用户的权限信息
| performance_schema | #主要存储数据库服务器的性能参数
| sys | #主要存储数据库服务器的性能参数(把performance-schema的复杂度降低)
+--------------------+
4 rows in set (0.00 sec)
语法:create database 数据库名 default charset "指定字符集";
数据库名命名规则:
区别大小写
唯一性
不能使用关键字 create select等
不能单独使用数字
案例:创建一个名为school的数据库
mysql> create database school default charset"utf8"; #utf8mb4 表情,颜文字
Query OK, 1 row affected (0.00 sec)
语法:show create database 数据库名;
mysql> show create database school;
+----------+-----------------------------------------------------------------+
| Database | Create Database |
+----------+-----------------------------------------------------------------+
| school | CREATE DATABASE `school` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)
语法:select database( );
mysql> select database();
+------------+
| database() |
+------------+
| NULL |
+------------+
1 row in set (0.00 sec)
语法:use 库名;
mysql> use school; #切换库
Database changed
mysql> select database(); #查看当前所在库
+------------+
| database() |
+------------+
| school |
+------------+
1 row in set (0.00 sec)
mysql> drop database school; #删除school库
Query OK, 4 rows affected (0.01 sec)
mysql> show databases; #查看所有库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
表是数据库存储数据的基本单元,由若干个字段组成,主要用来存储数据记录
create table 表名(
字段名1 类型 [(宽度) 约束条件],
字段名2 类型 [(宽度) 约束条件],
字段名3 类型 [(宽度) 约束条件]
)[存储引擎 字符集];
==在同一张表中,字段名是不能相同
==宽度和约束条件可选
==字段名和类型是必须的
mysql> create table student(
-> `id` int primary key auto_increment comment"学生id",
-> `age` int not null comment"学生年龄",
-> `address` varchar(20) comment"学生地址",
-> `phone` int comment"联系方式");
Query OK, 0 rows affected (0.00 sec)
语法:desc 表名;
mysql> desc student;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| age | int(11) | NO | | NULL | |
| address | varchar(20) | YES | | NULL | |
| phone | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
语法:show table status like“表名” \G
mysql> show table status like"student" \G
*************************** 1. row ***************************
Name: student
Engine: InnoDB
Version: 10
Row_format: Dynamic
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: 1
Create_time: 2023-09-26 18:08:09
Update_time: NULL
Check_time: NULL
Collation: utf8_general_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
语法:show create table 表名
mysql> show create table student;
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| student | CREATE TABLE `student` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '学生id',
`age` int(11) NOT NULL COMMENT '学生年龄',
`address` varchar(20) DEFAULT NULL COMMENT '学生地址',
`phone` int(11) DEFAULT NULL COMMENT '联系方式',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
语法: select 指定字段 from 表名;
mysql> select age from student; #查看student表中的age
+-----+
| age |
+-----+
| 18 |
| 20 |
| 88 |
+-----+
3 rows in set (0.00 sec)
mysql> select age,id from student; #查看student表中的age和id
+-----+----+
| age | id |
+-----+----+
| 18 | 1 |
| 20 | 2 |
| 88 | 6 |
+-----+----+
3 rows in set (0.00 sec)
语法:select * from 表名;
mysql> select * from student; #查看student表中的所有信息
+----+-----+---------+------------+
| id | age | address | phone |
+----+-----+---------+------------+
| 1 | 18 | 山西 | 1999999999 |
| 2 | 20 | NULL | 1888888888 |
| 6 | 88 | NULL | NULL |
+----+-----+---------+------------+
3 rows in set (0.00 sec)
alter 修改表名称 修改字段名称 修改字段数据类型 修改字段的修饰符
语法:alter table 表名 add 新字段;
mysql> alter table student add math int null comment"数学成绩"; #添加一个新字段
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table student add name varchar(10) null comment"姓名" after id; #在id后添加一个新字段name
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
| age | int(11) | NO | | NULL | |
| address | varchar(20) | YES | | NULL | |
| phone | int(11) | YES | | NULL | |
| math | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> alter table student add (chinese int null comment"语文成绩",history int null comment"历史成绩"); #同时添加两个新字段
语法: rename table 原表名 to 新表名;
alter table 原表名 rename 新表名;
mysql> rename table student to students; #将表名student改为students
Query OK, 0 rows affected (0.01 sec)
mysql> show tables; #查看表
+------------------+
| Tables_in_school |
+------------------+
| students |
+------------------+
1 row in set (0.00 sec)
mysql> alter table students rename student; #将students改为student
Query OK, 0 rows affected (0.01 sec)
mysql> show tables; #查看表
+------------------+
| Tables_in_school |
+------------------+
| student |
+------------------+
1 row in set (0.00 sec)
语法: alter table 表名change 旧字段名 新字段名 字段类型 约束符
mysql> alter table student change math english int(12) null; #将math改名为English
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
| age | int(11) | NO | | NULL | |
| address | varchar(20) | YES | | NULL | |
| phone | int(11) | YES | | NULL | |
| english | int(12) | YES | | NULL | |
| chinese | int(11) | YES | | NULL | |
| history | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+----------------+
语法: alter table 表名 modify 字段名 字段类型 约束符
mysql> alter table student modify chinese int(13);
# 修改chinese是字段类型
语法: insert into 表名(字段)values(值)
mysql> insert into student values(1,18,"山西",1999999999);
Query OK, 1 row affected (0.00 sec)
mysql> insert into student(age,phone) values(20,1888888888);
Query OK, 1 row affected (0.01 sec)
mysql> insert into student set id=6,age=88;
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+----+-----+---------+------------+
| id | age | address | phone |
+----+-----+---------+------------+
| 1 | 18 | 山西 | 1999999999 |
| 2 | 20 | NULL | 1888888888 |
| 6 | 88 | NULL | NULL |
+----+-----+---------+------------+
3 rows in set (0.00 sec)
语法: delete from 表名 where id=" ";
mysql> delete from student where id=1; #删除id=1的字段
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+------+-----+---------+------------+---------+---------+---------+
| id | name | age | address | phone | english | chinese | history |
+----+------+-----+---------+------------+---------+---------+---------+
| 2 | NULL | 20 | NULL | 1888888888 | NULL | NULL | NULL |
| 6 | NULL | 88 | NULL | NULL | NULL | NULL | NULL |
+----+------+-----+---------+------------+---------+---------+---------+
2 rows in set (0.00 sec
语法:update 表名 set 要更新的信息 where id=" ";
mysql> select * from student;
+----+------+-----+---------+------------+---------+---------+---------+
| id | name | age | address | phone | english | chinese | history |
+----+------+-----+---------+------------+---------+---------+---------+
| 2 | NULL | 20 | NULL | 1888888888 | NULL | NULL | NULL |
| 6 | NULL | 88 | NULL | NULL | NULL | NULL | NULL |
+----+------+-----+---------+------------+---------+---------+---------+
2 rows in set (0.00 sec)
mysql> update student set name='xiaoming' where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+----+----------+-----+---------+------------+---------+---------+---------+
| id | name | age | address | phone | english | chinese | history |
+----+----------+-----+---------+------------+---------+---------+---------+
| 2 | xiaoming | 20 | NULL | 1888888888 | NULL | NULL | NULL |
| 6 | NULL | 88 | NULL | NULL | NULL | NULL | NULL |
+----+----------+-----+---------+------------+---------+---------+---------+
2 rows in set (0.00 sec)
mysql> create table stu3(select * from student);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from stu3;
+----+----------+-----+---------+------------+---------+---------+---------+
| id | name | age | address | phone | english | chinese | history |
+----+----------+-----+---------+------------+---------+---------+---------+
| 2 | xiaoming | 20 | NULL | 1888888888 | NULL | NULL | NULL |
| 6 | NULL | 88 | NULL | NULL | NULL | NULL | NULL |
+----+----------+-----+---------+------------+---------+---------+---------+
2 rows in set (0.00 sec)
mysql> create table stu4 (select id,name from student);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from stu4;
+----+----------+
| id | name |
+----+----------+
| 2 | xiaoming |
| 6 | NULL |
+----+----------+
2 rows in set (0.00 sec)
mysql> create table stu5(SELECT id,name,age,address,phone from student where 5=4);
#等式不成立只复制结构,等式成立全部复制
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc stu5;
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | 0 | |
| name | varchar(10) | YES | | NULL | |
| age | int(11) | NO | | NULL | |
| address | varchar(20) | YES | | NULL | |
| phone | int(11) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
mysql> select * from stu5;
Empty set (0.00 sec)
语法:drop table 表名;
mysql> drop table stu5;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| stu3 |
| stu4 |
| student |
+------------------+
3 rows in set (0.00 sec)
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)
mysql> show variables like "%storage_engine%";
+----------------------------------+--------+
| Variable_name | Value |
+----------------------------------+--------+
| default_storage_engine | InnoDB |
| default_tmp_storage_engine | InnoDB |
| disabled_storage_engines | |
| internal_tmp_disk_storage_engine | InnoDB |
+----------------------------------+--------+
4 rows in set (0.01 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.43 |
+-----------+
1 row in set (0.00 sec)