Learning MySQL database from 0 basics (1)

MySQL database technology

One, the basic concept of the database

1. How is java data stored?

Variables, arrays, collections, objects

In memory: fast, but the shortcomings cannot be stored permanently, and the data is in a temporary state

File: It can be saved in a file through IO, permanent, but the database operation is not safe and convenient

Database: permanent storage, fast query speed, convenient data management, safety, shortcomings occupy resources

2. What is a database

A warehouse for storing and managing data, stored in a computer, organized, a collection of large amounts of data that can be shared

Database: DataBase Abbreviation: DB

3. Database advantages

Can store large amounts of data

Easy to search

Maintain data consistency and integrity

Safe and shareable

Through combination analysis, new data can be generated

4. Commonly used databases

Relational database SQL

MySQL

Oracle

SQL Server

MongoDB

IBM Db2

Non-relational database NOSQL

5. Why MySQL Choose

MySQL database is currently a popular open source, free relational database

Small and fully functional

Use portable

Can run on windows or Linux operating system

Suitable for medium, small and even large website applications

2. Common operations of MySQL database

1. MySQL start service

①Manual start: my computer-management-service

②DOS command start: enter cmd in win+r run window, enter services.msc to open the service window

③Enter services.msc directly in the running window to open the service window directly

④Open the cmd command line as an administrator and enter net start mysql

net stop mysql end

2. Log in to the MySQL database

① mysql -u username -p password

② mysql -hIP address -u username -p password

3. Exit the MySQL database

exit or quit

4. Directory of MySQL database

Three, SQL language

1. What is the SQL language?

结构化查询语言:Structured Query Language  是一种所有关系型数据库的查询规范,不同的数据库都支持,通用的数据库操作语言,操作所有关系型数据库的规则

2. SQL general syntax

1.SQL 语句可以单行或者多行,以分号结束
show databases;显示所有数据库信息

2.MySQL数据库的SQL语句不区分大小写,关键字建议使用大写
Show DataBases;

3.三种注释
①-- 注释 (-- 后面有空格)
②#注释内容
③/* 注释  */

3. SQL classification

1.DDL(Data Definition Language) 数据定义语言:
	用来定义数据库对象,数据库、数据表、列表,关键字:creat drop alter等等
2.DML(Data Manipulation Language) 数据操作语言:
	用来对数据中表的数据进行增删改操作,关键字:insert delete update等等
3.DQL(Data Query Language) 数据查询语言:
	用来查询数据库中表的数据, 关键字:select where等等
4.DCL(Data Control Language)数据控制语言
	用来定义数据库的访问权限和安全级别,关键字:grant revoke等等

Four, DDL data definition language, operating databases, tables

1. Operating the database

CRUD:

1.1C:Create
-- 创建数据库
create database 数据库名称;

-- 判断不存在,则创建
create database if not exists 数据库名称;

-- 指定字符集
create database 数据库名称 character set 字符集名称;

1.2R: Retrieve query
-- 查询所有数据库的名称
show databases;

-- 查询某个数据库的字符集
show create database 数据库名称;
1.3U: Update
-- 修改数据库的字符集
alter database 数据库名称 character set 字符集名称
1.4D: Delete
-- 删除数据库
drop database 数据库名称;

-- 判断数据库存在,则删除
drop database if exists 数据库名称;
1.5 Other operations
-- 查询当前是否处于某一个数据库下
select database();

-- 切换到当前数据库
use 数据库名称;

2. Operating data sheet

2.1Create
-- 创建表语法
create table 表名(
	列名1 数据类型1
    列名2 数据类型2
    ……
    列名n 数据类型n
);
mysql在自定义表名和列名时,规则
①必须以字母开头
②长度不能超过30字符
③不能使用关键字,如mysql
④只能使用如下字符:A-Z,a-z,0-9,$,下划线,但不能使用空格和单引号

-- 常用数据类型,主要使用在列上
1.整数类型:int(长度);
	age int(2);
2.小数类型:double(长度);
	score double(5,2);5为最多位,2表示保留2位小数,最大值999.99
3.日期类型:date
	表示:yyyy-MM-dd
4.datatime:不赋值默认null
	表示:yyyy-MM-dd HH:mm:ss
5.timestamp:时间戳类型
包含年月日时分秒,但是秒值精确到小数位后6位,一般银行会用
不赋值,默认当前系统时间,自动赋值
6.字符类型:char(长度);
	固定长度字符类型,
	name char(10); 
	'你好'占用了前4个字符,后6个会空格补齐
7.字符串类型:verchar(长度)
	动态,
	name verchar(10);
	'你好'占用了前4个字符,则自动分配4个字符,节约空间
8.字符串类型:text 长度(0-65535字节)
9.bolb:字节类型
10.clob:字符类型

-- 创建学生表
create table student(
	id int,-- 学号
    name varchar(50), -- 姓名
    age int(10), -- 年龄
    score double(4,1), -- 成绩
    birthday date, -- 生日
    insert_time timestamp -- 添加时间

);
2.2Retrieve query
-- 查询某个数据库中所有表名称
show tables;

-- 查询创建表的SQL语句
show create table 表名;

-- 查询表的结构;
desc 表名;
2.3Update modification
1.添加一列,学生性别 sex
alter table 表名 add 列名 数据结构

2.修改列名、数据类型
alter table 表名 change 列名 新类名 数据类型;
alter table 表名 modify 列名 新数据类型;

3.删除列
alter table 类名 drop 列名;

4.修改表名
alter table 表名 rename 新表名;

5.修改表的字符集
-- 显示创建时的SQL语句
show create table 表名;
alter table 表名 character set utf8;
2.4D: Delete
-- 删除表
drop table 表名;
drop table if exists 表名; -- 存在则删除

Five, DML (Data Manipulation Language) data table addition, deletion and modification operations

1.1 Insert data

语法:
insert into 表名(列名1,列名2……列名n) values(1,值2……值n);
-- 案例
 insert into stu(id,name,sage,score,birthday,insert_time)values(001,'zhangsan',18,97.5,null,null);
 -- 第一个null就是null,第二个null,因为是时间戳类型,默认当前时间
 
-- 插入所有列时,可以不定义列名,默认插入顺序和创建顺序一致
insert into stu values(003,'changjian',18,97.5,null,null);

-- 注意:
1.列名和值要一一对应;
2.除了数字类型,其他类型都要引号
-- 插入部分字段(部分列)
insert into stu(id,sage,score) values(004,19,99.99);

-- 复制表
create table 新表名 like 表名;-- 只是创建了表结构,表内无数据

-- 数据迁移
insert into 新表 select * from 表名;

-- 复制部分字段
insert into 新表(id,name,age) select id,name,age from 表名;

[External link image transfer failed. The source site may have an anti-hotlinking mechanism. It is recommended to save the image and upload it directly (img-ooxtEKTp-1607864427611) (C:\Users\24582\AppData\Roaming\Typora\typora-user-images\ image-20201212122246714.png)]

1.2 Delete data

语法:
	delete from 表名 [where 条件]
1.删除一条记录
delete from stu where 
id=2;
2.如果不加条件则会删除全部数据,但是表结构还在
写日志,速度慢,但安全性高,可以恢复
delete from 表名;
drop table 表名;-- 直接删除表结构以及数据
3.如果我们要删除所有记录,速度快,不写日志,不可以恢复
truncate table 表名; -- 先删除表,再给创建一张一样的表,无法数据恢复

注意:deletetruncate区别?

1.3 Modify data

语法:
update 表名 set 列名1 = 值1,列名2 = 值2……[where 条件]

-- 修改一个字
 update stu set sage = 20 where id=2;
-- 修改多个字段
update stu set  name='taoc',sage = 18,score = 99.0 where id=3;

-- 数据库默认编码格式UTF8 客户端gbk,
所有插入中文应该转化;

-- 注意:当修改含有null值的数据,不能用 = null,要用is null,is not null 判断

Guess you like

Origin blog.csdn.net/zjdzka/article/details/111145322