N days explosive liver database - MySQL (1)

Understanding of database concepts

Database DB Warehouse for storing data
Database Management System DBMS Large-scale software for manipulating and managing databases

SQL is a programming language for operating relational databases, and defines a set of standards for operating relational databasesinsert image description here

Learn what SQL does

SQL is an ANSI standard computer language for accessing and manipulating database systems. SQL statements are used to retrieve and update data in the database.

SQL Executes queries against the database
SQL can retrieve data from the database
SQL can insert new records in the database
SQL can update data in the database
SQL can delete records from the database
SQL can create new databases
SQL can create new tables in the database
SQL can be used in Create stored procedures in the database
SQL can create views in the database
SQL can set the permissions of tables, stored procedures and views

SQL general syntax

1). SQL语句可以单行或多行书写,以分号结尾。
2). SQL语句可以使用空格/缩进来增强语句的可读性。
3). MySQL数据库的SQL语句不区分大小写,关键字建议使用大写。
4). 注释: 单行注释:-- 注释内容 或 # 注释内容 ;
	      多行注释:/* 注释内容 */

insert image description here

SQL classification

DDL:数据定义语言	
DML:数据操作语言
DQL:数据查询语言
DCL:数据控制语言

DDL database operations

Inquire

查询所有数据库	SHOW DATABASES;
查询当前数据库 SELECT DATAABASE();

insert image description here

create

CREATE DATABASE[IF NOT EXISTS] 数据库名[DEFAULTCHARSET字符集][COLLATE排	 序规则];
CREATE DATABASE IF NOT EXISTS 数据库名

Notice_If the database exists, it will not be created, if not, it will be created

Example:
CREATE DATABASE IF NOT EXISTS test;
CREATE DATABASE DEFAULT  CHARSET字符集
设置创建的字符集
Example:
CREATE DATABASE test DEFAULT  CHARSET utf8mp4;

delete

 DROP DATABASE[IF EXISTS]数据库名;
[IF EXISTS]如果存在则删除 可以防止报错
Example:
DROP DATABASE test;

insert image description here

use

USE 数据库名;

DDL-table operation-query

 查询当前数据库所有表 SHOW TABLES;
 查询表结构 DESC 表名;
 查询指定表的建表语句 SHOW CREATE TABLE 表名;

DDL-table operation-create

CREATE TABLE 表名(
字段1 字段1类型[COMMENT 字段1注释],
字段2 字段2类型[COMMENT 字段2注释],
字段3 字段3类型[COMMENT 字段3注释],
...
字段n 字段n类型[COMMENT 字段n注释],
)[COMMENT 表注释];

insert image description here

DDL-table operation-data type

数值类型 
整形
	TINYINT		1bt
	SMALLINT	3bt
	MEDIUMINT	8bt
	INT/INTEGER		4bt
	BIGINT		8 bt
浮点数
	FLOAT		4bt
	DOUBLE		8bt
	DECIMAL
字符串类型
	CHAR	定长字符串
	VARCHAR		变长字符串
	TINYBLOB	不超过255个字符的二进制数据
	TINYTEXT	短文本字符串
	BLOB	二进制长文本数据
	TEXT	长文本数据
	MEDIUMBLOB		二进制中等长度文本数据
	MEDIUMTEXT		中等长度文本数据
	LONGBLOB		二进制极大文本数据
	LONGTEXT		极大文本数据
 日期类型
	DATE	3	日期值					YYYY-MM-DD
	TIME	3	时间值或持续时间		HH:MM:SS
	YEAR	1	年份值					YYYY
	DATETIME	8	混合日期和时间值	YYYY-MM-DD HH:MM:SS
	TIMESTAMP	4	混合日期和时间值,时间戳YYYY-MM-DD HH:MM:SS

String comparison:

char(10)性能好于varchar(10)

insert image description here

DDL-table operation-modify

add field

ALTED TABLE表名ADD 字段名 类型(长度)[COMMENT 注释][约束];
举例:alter table emp add nickname varchar(20) comment'昵称';

modify data type

ALTED TABLE表名 MODIFY字段名 新数据类型(长度);
修改字段名和字段类型
ALTER TABLE 表名CHANGE旧字段名 新字段名 类型(长度)	[COMMENT 注释][约束];

example: Change the nickname field of the emp table to username, the type is varchanr(30)
alter table emp change nickname username varchar(30) comment'username';

delete field

ALTER TABLE 表名 DROR 字段名;
举例:删除username
	alter table emp drop username;

modify table name

ALTER TABLE 表名 RENAME TO 新表名;
举例:将emp表的表名修改为employee

delete table

CROP TABLE [IF EXISTS] 表名;

Drop the specified table and recreate it

TRUNCATE TABLE 表名;

insert image description here

Guess you like

Origin blog.csdn.net/weixin_74888502/article/details/131625577