MySQL (1)-database classification, database management system, database operation

1 Classification of the database

1 Relational database (SQL)
MySQL, Oracle, SQL Server, SQLite, DB2, …
Relational databases establish the relationship between tables through foreign key associations

2 Non-relational databases (NOSQL)
Redis, MongoDB, …
Non-relational databases usually mean that data is stored in the database in the form of objects, and the relationship between objects is determined by the properties of each object

2Database Management System (DataBase Management System)

Database management software, scientifically organize and store data, and efficiently obtain and maintain data.
Insert picture description here
MySQL is a relational database management system . Relational databases store data in different tables instead of putting all data in a large warehouse. Increased speed and increased flexibility.

3 database operations

1 Operate the database
2 Operate the database table
3 Operate the data in the database table
Insert picture description here
Command line operation database

创建数据库 :  create database [if not exists] 数据库名;
删除数据库 : drop database [if exists] 数据库名;
查看数据库 : show databases;
使用数据库 : use 数据库名;
------------------------------------------------
create database if  exists mybatis1;
drop database if exists mybatis1;
show databases;
use mybatis1;

Insert picture description here

1 Column type of the database

1 value

One byte = 8bit
Insert picture description here

2 string

Insert picture description here

3 date and time

Insert picture description here

4null

2Data field attributes (emphasis)

1Unsigned (unsigned)

Unsigned
declares that the data column does not allow negative numbers
Insert picture description here

2 zero padding

Filled
with 0 Filled with 0 if there are not enough digits, such as int(3), 5 is 005

3Auto_InCrement

Automatic growth, every time you add a piece of data, automatically add 1 to the previous record number (default),
usually used to set the primary key, and it is an integer type. The
starting value and step size can be defined. The
current table setting step size (AUTO_INCREMENT=100): Only affect the current table
SET @@auto_increment_increment=5; affect all tables that use auto-increment (global)
Insert picture description here

4NULL and NOT NULL

The default is NULL, that is, no value is inserted into the column.
If set to NOT NULL, the column must have a value

5DEFAULT

The default is
used to set the default value.
For example, the gender field, the default is "male", otherwise it is "female"; if no value is specified for this column, the default value is the value of "male"

-- 目标 : 创建一个school数据库
-- 创建学生表(,字段)
-- 学号int 登录密码varchar(20) 姓名,性别varchar(2),出生日期(datatime),家庭住址,email
-- 创建表之前 , 一定要先选择数据库
CREATE TABLE IF NOT EXISTS `student` (
`id` int(4) NOT NULL AUTO_INCREMENT COMMENT '学号',
`name` varchar(30) NOT NULL DEFAULT '匿名' COMMENT '姓名',
`pwd` varchar(20) NOT NULL DEFAULT '123456' COMMENT '密码',
`sex` varchar(2) NOT NULL DEFAULT '男' COMMENT '性别',
`birthday` datetime DEFAULT NULL COMMENT '生日',
`address` varchar(100) DEFAULT NULL COMMENT '地址',
`email` varchar(50) DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

-- 查看数据库的定义
SHOW CREATE DATABASE school;
-- 查看数据表的定义
SHOW CREATE TABLE student;
-- 显示表结构
DESC student;  -- 设置严格检查模式(不能容错了)SET sql_mode='STRICT_TRANS_TABLES';

create table [if not exists] `表名`(
	`字段名` 列类型 [属性] [索引] [注释]
	......
)[表类型][字符集设置][注释]

show create table `student1`;

3 Types of data sheets

ENGINE=InnoDB(or MyISAM)
Insert picture description here
Applicable to MyISAM: saving space and faster
Applicable to InnoDB: security, transaction processing and multi-user operating data tables

// 查看数据库的存储位置
SHOW VARIABLES LIKE 'datadir';
C:\ProgramData\MySQL\MySQL Server 5.5\Data\

The MySQL data table is stored as a file in the disk
C:\ProgramData\MySQL\MySQL Server 5.5\Data
InnoDB type data table has only one *.frm file, and the ibdata1 file in the upper-level directory
MyISAM type data table corresponds to three files:

*. frm -- 表结构定义文件
*. MYD -- 数据文件 ( data )
*. MYI -- 索引文件 ( index )

Insert picture description here
Setting the data table character set
We can set different character sets for the database, data table, and data column. The setting method: Set
it through commands when creating, such as: CREATE TABLE table name () CHARSET = utf8;
if there is no setting, According to the parameter settings in the MySQL database configuration file my.ini

Insert picture description here

4 modify the database

Modify the table (ALTER TABLE)

修改表名 :ALTER TABLE `旧表名` RENAME AS `新表名`
添加字段 : ALTER TABLE `表名` ADD `字段名` 列属性
修改字段 :
ALTER TABLE `表名` MODIFY `字段名` 列类型[属性] //修改约束
ALTER TABLE `表名` CHANGE `旧字段名` `新字段名` 列属性[属性] //重命名
删除字段 :  ALTER TABLE `表名` DROP `字段名`

Delete data table

语法:DROP TABLE [IF EXISTS] `表名`
IF EXISTS为可选 , 判断是否存在该数据表
如删除不存在的数据表会抛出错误
1. 可用反引号(`)为标识符(库名、表名、字段名、索引、别名)包裹,以避免与关键字重名!中文也可以作为标识符!

2. 每个库目录存在一个保存当前数据库的选项文件db.opt。

3. 注释:
  单行注释 # 注释内容
  多行注释 /* 注释内容 */
  单行注释 -- 注释内容       (标准SQL注释风格,要求双破折号后加一空格符(空格、TAB、换行等))
   
4. 模式通配符:
  _   任意单个字符
  %   任意多个字符,甚至包括零字符
  单引号需要进行转义 \'
   
5. CMD命令行内的语句结束符可以为 ";", "\G", "\g",仅影响显示结果。其他地方还是用分号结束。delimiter 可修改当前对话的语句结束符。

6. SQL对大小写不敏感 (关键字)

7. 清除已有语句:\c

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/113919117