图片 1

MySQL基本简单操作02

MySQL基本总结操作

先进入Mysql容器。

[root@promote ~]# docker exec -it mysql /bin/bash
root@30d60b852cf5:/# mysql -uroot -p000000
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 8
Server version: 8.0.11 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> create database gubeiqing;
Query OK, 1 row affected (0.02 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| gubeiqing          |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

进去数据库,新建数据表。

mysql> use gubeiqing;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> create table gubeiqing_table(name varchar(20) not null , age varchar(20) not null);
Query OK, 0 rows affected (0.11 sec)

mysql> show tables;
+---------------------+
| Tables_in_gubeiqing |
+---------------------+
| gubeiqing_table     |
+---------------------+
1 row in set (0.01 sec)

随之给数据库新扩大列,基本回顾语法是:ALTER TABLE 表名 add column 列名 列类型 是否为空;

mysql> desc gubeiqing_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | NO   |     | NULL    |       |
| age   | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

mysql> alter table gubeiqing_table add column job varchar(20) not null;
Query OK, 0 rows affected (0.26 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc gubeiqing_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | NO   |     | NULL    |       |
| age   | varchar(20) | NO   |     | NULL    |       |
| job   | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

修改列名,基本回顾语法是:ALTER TABLE 表名 change column 原列名 修改后的列名 列类型 是否为空;

mysql> desc gubeiqing_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | NO   |     | NULL    |       |
| age   | varchar(20) | NO   |     | NULL    |       |
| job   | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table gubeiqing_table change column job gbq_job varchar(20) not null;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc gubeiqing_table;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(20) | NO   |     | NULL    |       |
| age     | varchar(20) | NO   |     | NULL    |       |
| gbq_job | varchar(20) | NO   |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

删去刚刚新添的列,基本归纳语法是:ALTER TABLE 表名 drop column 列名;

mysql> desc gubeiqing_table;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | varchar(20) | NO   |     | NULL    |       |
| age     | varchar(20) | NO   |     | NULL    |       |
| gbq_job | varchar(20) | NO   |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table gubeiqing_table drop column gbq_job;
Query OK, 0 rows affected (0.11 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc gubeiqing_table;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(20) | NO   |     | NULL    |       |
| age   | varchar(20) | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)

下一场说一下歪曲搜索,就例如未来要在数据Curry搜寻zhangsan的音讯,可是只记得zhang剩余的一部分都不记得了,那么就能够动用模糊找寻,基本总结语法是:SELECT * from 表名 WHERE 字段 LIKE '模糊字段';

mysql> select * from gubeiqing_table where name like 'zhang%';
+----------+-----+
| name     | age |
+----------+-----+
| zhangsan | 20  |
+----------+-----+
1 row in set (0.01 sec)

%用来表示不记得的有的,是通配符。
除了%之外还大概有
_表示别的单个字符,[ ]点名范围或集合中的任何单个字符,[^]不属于内定范围或集结的任何单个字符。

前言


上一篇关周全据库常用SQL语句语法大全要害是关系型数据库大要结构,本文细说一下关系型数据库查询的SQL语法。

SQL数据查询

前言


上一篇关周全据库常用SQL语句语法大全最主假使关系型数据库大要结构,本文细说一下关系型数据库查询的SQL语法。

图片 1

语法回看


SELECT [ALL|DISTINCT] <目标列表达式>[,<目标列表达式>]…
  FROM <表名或视图名>[,<表名或视图名>]…
  [WHERE <条件表达式>]
  [GROUP BY <列名> [HAVING <条件表达式>]]
  [ORDER BY <列名> [ASC|DESC]…]

SQL查询语句的各样:SELECT、FROM、WHERE、GROUP BY、HAVING、OCRUISERDER
BY。SELECT、FROM是必需的,HAVING子句只能与GROUP BY搭配使用。

语法回想


SELECT [ALL|DISTINCT] <目标列表达式>[,<目标列表达式>]…
  FROM <表名或视图名>[,<表名或视图名>]…
  [WHERE <条件表达式>]
  [GROUP BY <列名> [HAVING <条件表达式>]]
  [ORDER BY <列名> [ASC|DESC]…]

SQL查询语句的一一:SELECT、FROM、WHERE、GROUP BY、HAVING、OCRUISERDER
BY。SELECT、FROM是必需的,HAVING子句只可以与GROUP BY搭配使用。

防微杜渐干活


1.开立数据库和数据表
2.布署基本数据
3.本文以SQL Server为例介绍

-- 创建学生表
CREATE TABLE Students(
    Id int NOT NULL PRIMARY KEY,
    Name varchar(20) NOT NULL,
    Class varchar(15) NOT NULL,
    Gender varchar(10) NULL,
    Age int NULL,
    Phone varchar(15) NULL,
    [Address] varchar(100) NULL
)

-- 创建课程表
CREATE TABLE Courses(
    Id int NOT NULL PRIMARY KEY IDENTITY(1,1),
    Name varchar(50) NOT NULL
);

-- 创建成绩表
CREATE TABLE Scores(
    Id int PRIMARY KEY IDENTITY(1,1),
    SId int NOT NULL,
    CId int NOT NULL,
    Grades decimal(5,2) NOT NULL,
    IsPassed bit NOT NULL
);

-- 插入学生表基础数据
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016001,'小明','一班','男',20,'18817716611','北京');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016002,'小龙','一班','男',19,'18817716622','天津');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016003,'小王','二班','男',20,'18817716633','北京');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016004,'婷婷','一班','女',17,'18817716644','济南');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016005,'张三','一班','男',19,'18817716655','北京');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016006,'小赵','一班','男',20,'18817716666','北京');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016007,'丽丽','二班','女',18,'18817716677','北京');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016008,'花花','一班','女',19,'18817716688','沈阳');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016009,'静静','二班','女',20,'18817716699','北京');

-- 插入课程表基础数据
INSERT INTO Courses(Name) VALUES('语文');
INSERT INTO Courses(Name) VALUES('数学');
INSERT INTO Courses(Name) VALUES('英语');

-- 插入成绩表基础数据
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016001,1,120,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016001,2,70,0);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016001,3,89,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016002,1,90,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016002,2,88,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016002,3,96,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016003,1,112,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016003,3,102,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016004,1,80,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016004,2,86,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016004,3,47,0);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016005,1,87,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016005,2,96,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016005,3,68,0);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016006,2,95,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016006,3,100,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016007,1,87,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016007,2,57,0);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016007,3,130,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016008,1,89,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016008,3,66,0);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016009,1,97,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016009,2,104,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016009,3,68,1);

预备干活


1.创办数据库和数据表
2.插入基本数据
3.本文以SQL Server为例介绍

-- 创建学生表
CREATE TABLE Students(
    Id int NOT NULL PRIMARY KEY,
    Name varchar(20) NOT NULL,
    Class varchar(15) NOT NULL,
    Gender varchar(10) NULL,
    Age int NULL,
    Phone varchar(15) NULL,
    [Address] varchar(100) NULL
)

-- 创建课程表
CREATE TABLE Courses(
    Id int NOT NULL PRIMARY KEY IDENTITY(1,1),
    Name varchar(50) NOT NULL
);

-- 创建成绩表
CREATE TABLE Scores(
    Id int PRIMARY KEY IDENTITY(1,1),
    SId int NOT NULL,
    CId int NOT NULL,
    Grades decimal(5,2) NOT NULL,
    IsPassed bit NOT NULL
);

-- 插入学生表基础数据
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016001,'小明','一班','男',20,'18817716611','北京');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016002,'小龙','一班','男',19,'18817716622','天津');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016003,'小王','二班','男',20,'18817716633','北京');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016004,'婷婷','一班','女',17,'18817716644','济南');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016005,'张三','一班','男',19,'18817716655','北京');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016006,'小赵','一班','男',20,'18817716666','北京');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016007,'丽丽','二班','女',18,'18817716677','北京');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016008,'花花','一班','女',19,'18817716688','沈阳');
INSERT INTO Students (Id,Name,Class,Gender,Age,Phone,[Address]) VALUES(2016009,'静静','二班','女',20,'18817716699','北京');

-- 插入课程表基础数据
INSERT INTO Courses(Name) VALUES('语文');
INSERT INTO Courses(Name) VALUES('数学');
INSERT INTO Courses(Name) VALUES('英语');

-- 插入成绩表基础数据
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016001,1,120,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016001,2,70,0);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016001,3,89,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016002,1,90,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016002,2,88,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016002,3,96,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016003,1,112,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016003,3,102,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016004,1,80,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016004,2,86,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016004,3,47,0);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016005,1,87,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016005,2,96,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016005,3,68,0);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016006,2,95,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016006,3,100,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016007,1,87,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016007,2,57,0);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016007,3,130,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016008,1,89,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016008,3,66,0);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016009,1,97,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016009,2,104,1);
INSERT INTO Scores(SId,CId,Grades,IsPassed) VALUES(2016009,3,68,1);