MySQL为关系型数据库(Relational Database Management System), 这种所谓的"关系型"可以理解为"表格"的概念, 一个关系型数据库由一个或数个表格组成, 如图所示的一个表格:

python之路——MySQL数据库

  • 表头(header): 每一列的名称;
  • 列(row): 具有相同数据类型的数据的集合;
  • 行(col): 每一行用来描述某个人/物的具体信息;
  • 值(value): 行的具体信息, 每个值必须与该列的数据类型相同;
  • 键(key): 表中用来识别某个特定的人\物的方法, 键的值在当前列中具有唯一性。

2 MySQL的安装配置以及服务的启停

windows下MySQL安装以及配置
   1、直接下载安装文件,双击安装文件一步一步进行操作即可。
   2、在安装文件夹下找到my-small.ini配置文件, 将其重命名为my.ini,    在[client]与[mysqld]下均添加一行,

      即字符编码设置:default-character-set = gbk
   3、设置环境变量
   MySQL服务的启动、停止与卸载

Windows命令行下MySQL服务的启动、停止与卸载:
   1、启动: net start MySQL
   2、停止: net stop MySQL
   3、卸载: sc delete MySQL

Linux下MySQL安装与配置
   1、源码安装:
   2、在线安装
      Ubuntu(apt-get install mysql-server mysql-client)
      centOS/redhat(yum install mysql)
   3、字符编码设置以及环境变量设置
   
Linux下MySQL服务的启动、停止与卸载:
   1、启动与停止:/etc/init.d/mysql start/stop
   2、卸载:
      (1)源码安装--删除安装文件或执行卸载脚本
      (2)在线安装--执行RPM或APT卸载命令

3 MySQL脚本以及数据类型   

   3.1 MySQL脚本
   与常规的脚本语言类似, MySQL也具有一套对字符、单词以及特殊符号的使用规定, MySQL通过执行SQL语句来完成对数据库的操作, 该脚本由一条或多条MySQL语句(SQL语句 + 扩展语句)组成, 保存时脚本文件后缀名一般为.sql。在控制台下, MySQL客户端也可以对语句进行单句的执行而不用保存为.sql文件。
   语句——MySQL语句是组成MySQL脚本的基本单位, 每条语句能完成特定的操作, 他是由SQL标准语句 + MySQL扩展语句组成。
   函数——MySQL函数用来实现数据库操作的一些高级功能, 这些函数大致分为以下几类:字符串函数、数学函数、日期时间函数、搜索函数、加密函数、信息函数。
   3.2 MySQL数据类型
   MySQL有三大类数据类型, 分别为数字、日期/时间、字符串, 这三大类中又更细致的划分了许多子类型:
   数字类型
        整数: tinyint、smallint、mediumint、int、bigint
        浮点数: float、double、real、decimal
   日期和时间: date、time、datetime、timestamp、year
   字符串类型:
        字符串: char、varchar
        文本: tinytext、text、mediumtext、longtext
        二进制(可用来存储图片、音乐等): tinyblob、blob、mediumblob、longblob

4 MySQL基本操作

(1)启动、登陆数据库
启动:net start mysql(/etc/init.d/mysql start)
登陆:mysql -h 地址 -P 端口 -u 用户名 -p 密码
登陆本机:mysql -u'主机名或ip地址' -p
跳过权限验证登录:mysqld --skip-grant-tables

(2)库基本操作
查看:show databases;
使用:use [databasename];
创建:create database [name];

(3)表基本操作
查看:show tables;
创建:
create table students
    (
        id int  not null auto_increment primary key,
        name char(8) not null,
        sex char(4) not null,
        age tinyint unsigned not null,
        tel char(13) null default "-"
    );  

 
(4)数据操作    
插入:insert into students(name,sex,age,tel) values('alex','man',18,'151515151')
删除:delete from students where id =2;
修改:update students set name = 'sb' where id =1;
查询:select * from students

 1 #创建数据库jumpserver,设置字符编码为utf8
 2 create database jumpserver character set = utf8;
 3 #使用数据库
 4 use jumpserver
 5 
 6 #创建数据表host_group(主机组表)
 7 create table host_group
 8     (
 9         group_id int auto_increment primary key,
10         name char(20) not null,
11         description text
12     );
13 
14 #创建数据表user_info(用户表)
15 create table user_info
16     (
17         user_id int auto_increment primary key,
18         user_name char(20) not null,
19         user_group_id int not null,
20         foreign key(user_group_id) references host_group(group_id)    
21     );
22 
23 #创建数据表host_info(主机表)
24 create table host_info
25     (
26         host_id int auto_increment primary key,
27         host_name char(50) not null,
28         host_type char(50) not null,
29         address char(15) not null,
30         cpu_count smallint,
31         cpu_type char(20),
32         disk_count smallint,
33         size_perdisk int,
34         host_description text
35     );
36 
37 #创建数据表host_group_relation(主机与主机组关系表)
38 create table host_group_relation
39     (
40     id int,
41     host_id int not null,
42     group_id int not null,
43     foreign key(host_id) references host_info(host_id),
44     foreign key(group_id) references host_group(group_id),
45     primary key(host_id,group_id)
46     );
47 
48 #向表host_group中插入3条数据
49 insert into host_group values
50 (1,'admin','管理员,可操作所有机器,执行任何操作'),
51 (2,'developer','程序开发人员,只能操作应用服务器'),
52 (3,'dba','数据库管理员,只能操作数据库服务器');    
53 
54 #向表user_info中插入3条数据   
55 insert into user_info values
56 (1,'alex',1),(2,'eric',2),(3,'tony',3);
57 
58 #向表host_info中插入3条数据   
59 insert into host_info (host_id,host_name,host_type,address) values
60 (1,'host1','web','192.168.1.171'),
61 (2,'host2','application','192.168.1.172'),
62 (3,'host3','db','192.168.1.173');
63 
64 #向表host_group_relation中插入5条数据   
65 insert into host_group_relation values
66 (1,1,1),
67 (2,2,1),
68 (3,3,1),
69 (4,2,2),
70 (5,3,3);
数据库操作实例

相关文章:

  • 2021-11-21
  • 2021-04-02
  • 2021-10-02
  • 2022-01-23
  • 2022-02-28
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-22
  • 2021-11-22
  • 2021-12-11
  • 2022-12-23
  • 2022-02-21
  • 2021-12-24
相关资源
相似解决方案