数据库(Database)是按照 数据结构来组织、 存储和管理数据的建立在计算机存储设备上的仓库。简单来说是本身可视为 电子化的 文件柜——存储电子 文件的处所,用户可以对文件中的数据进行新增、截取、更新、删除等操作。
严格来说,数据库是长期储存在计算机内、有组织的、可共享的数据集合。数据库中的数据指的是以一定的数据模型组织、描述和储存在一起、具有尽可能小的 冗余度、较高的数据独立性和易扩展性的特点并可在一定范围内为多个用户共享。这种数据集合具有如下特点:尽可能不重复,以最优方式为某个特定组织的多种应用服务,其数据结构独立于使用它的应用程序,对数据的增、删、改、查由统一软件进行管理和控制。从发展的历史看,数据库是数据管理的高级阶段,它是由文件管理系统发展起来的。
MySQL是一个关系型数据库管理系统,原本是一个开放源码的关系数据库管理系统,原开发者为瑞典的MySQL AB公司,该公司于2008年被昇阳微系统(Sun Microsystems)收购。2009年,甲骨文公司(Oracle)收购昇阳微系统公司,MySQL成为Oracle旗下产品。MySQL在过去由于性能高、成本低、可靠性好,已经成为最流行的开源数据库,因此被广泛地应用在Internet上的中小型网站中。随着MySQL的不断成熟,它也逐渐用于更多大规模网站和应用。非常流行的开源软件组合LAMP或者LNMP中的“M”指的就是MySQL。
数据共享包含所有用户可同时存取数据库中的数据,也包括用户可以用各种方式通过接口使用数据,并提供数据享。
同文件系统相比,由于数据库实现了数据共享,从而避免了用户各自建立应用文件。减少了大量重复数据,减少了数据冗余,维护了数据的一致性。
数据的独立性包括逻辑独立性(数据库中数据库的 逻辑结构和 应用程序相互独立)和物理独立性(数据物理结构的变化不影响数据的逻辑结构)。
文件管理方式中,数据处于一种分散的状态,不同的用户或同一用户在不同处理中其文件之间毫无关系。利用数据库可对数据进行集中控制和管理,并通过 数据模型表示各种数据的组织以及数据间的联系。
主要包括:①安全性控制:以防止数据丢失、错误更新和越权使用;② 完整性控制:保证数据的正确性、有效性和相容性;③ 并发控制:使在同一时间 周期内,允许对数据实现多路存取,又能防止用户之间的不正常交互作用。
由 数据库管理系统提供一套方法,可及时发现故障和修复故障,从而防止数据被破坏。 数据库系统能尽快恢复数据库系统运行时出现的故障,可能是物理上或是逻辑上的错误。比如对系统的误操作造成的数据错误等。
mysql -u root -p
password:
注意防火墙策略要允许通过端口3306或者mysql服务
mysql -u root -h 192.168.60.60 -P 3306 -p
password:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql;
Database changed
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
mysql> describe user;
mysql> select * from user;
mysql> create database school;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| school |
| sys |
+--------------------+
5 rows in set (0.00 sec)
格式:create table 表名(字段1 字段类型 字段约束)存储引擎,字符集
字段类型:整数型int();浮点型double(双精度8字节)、float(单精度4字节);decimal(5,2)有效数字5位,小数2位;字符char;字符串char(固定长度)、varchar(可变长度)。
字段约束:非空not null;默认default ‘’;主键primary key;自增auto increment。
存储引擎:myisam;innodb。
字符集:utf8。
mysql> create table student (
id int(9) primary key not null,
name char(10) not null,
socre decimal(5,2) default '0');
Query OK, 0 rows affected (0.01 sec)
mysql> desc student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(9) | NO | PRI | NULL | |
| name | char(10) | NO | | NULL | |
| socre | decimal(5,2) | YES | | 0.00 | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
删除表
mysql> drop table comany.boss;
Query OK, 0 rows affected (0.01 sec)
删除库
mysql> drop database comany;
Query OK, 0 rows affected (0.00 sec)
方法一(在构建表结构时候加入数据)
mysql> insert into student(id,name,socre)values(170420101,'zhangqing',91);
方法二(在已有表中加入数据)
mysql> insert into student values(170420102,'yangwu',88);
方法三(在创建表的时候加入数据)
mysql> create table student (
id int(9) primary key not null,
name char(10) not null,
socre decimal(5,2) default '0'
)values(170420103,'zhangsan',60);
查看添加信息
mysql> select * from student;
+-----------+-----------+-------+
| id | name | socre |
+-----------+-----------+-------+
| 170420101 | zhangqing | 91.00 |
| 170420102 | yangwu | 88.00 |
+-----------+-----------+-------+
2 rows in set (0.00 sec)
mysql> create table teacher as select * from student where name='zhangqing';
Query OK, 1 row affected (0.01 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student |
| teacher |
+------------------+
2 rows in set (0.00 sec)
mysql> select * from teacher;
+-----------+-----------+-------+
| id | name | socre |
+-----------+-----------+-------+
| 170420101 | zhangqing | 91.00 |
+-----------+-----------+-------+
1 row in set (0.00 sec)
mysql> update teacher set socre=100 where id=170420101;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from teacher;
+-----------+-----------+--------+
| id | name | socre |
+-----------+-----------+--------+
| 170420101 | zhangqing | 100.00 |
+-----------+-----------+--------+
1 row in set (0.00 sec)
方法一(可以清空,可以逐个删除)
mysql> delete from student where name='zhangqing';
Query OK, 1 row affected (0.00 sec)
mysql> select * from student;
+-----------+--------+-------+
| id | name | socre |
+-----------+--------+-------+
| 170420102 | yangwu | 88.00 |
+-----------+--------+-------+
1 row in set (0.00 sec)
mysql> delete from school.teacher;
Query OK, 1 row affected (0.00 sec)
mysql> select * from school.teacher;
Empty set (0.00 sec)
mysql> desc school.teacher;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(9) | NO | | NULL | |
| name | char(10) | NO | | NULL | |
| socre | decimal(5,2) | YES | | 0.00 | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
方法二(清空表数据)
mysql> truncate table student;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from school.student;
Empty set (0.00 sec)
mysql> desc school.student;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(9) | NO | PRI | NULL | |
| name | char(10) | NO | | NULL | |
| socre | decimal(5,2) | YES | | 0.00 | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
用于存放临时的数据,不会长时间的存在,断开连接就会被删除,数据存放在内存之中,不在磁盘。
mysql> create temporary table tmp(
id int (5) not null primary key auto_increment,
name char(10) not null,
socre double(5) default '0'
)engine=innodb default charset=utf8;
mysql> desc tmp;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(5) | NO | PRI | NULL | auto_increment |
| name | char(10) | NO | | NULL | |
| socre | decimal(5,2) | YES | | 0.00 | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
方法一(分部克隆)
首先克隆表结构
mysql> create table test like student;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student |
| teacher |
| test |
+------------------+
然后克隆表中的数据
mysql> insert into test select * from student;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from test;
Empty set (0.00 sec)
方法二(一次性克隆)
mysql> create table test1 as select * from teacher;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student |
| teacher |
| test |
| test1 |
+------------------+
4 rows in set (0.00 sec)
mysql> desc test1;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(9) | NO | | NULL | |
| name | char(10) | NO | | NULL | |
| socre | decimal(5,2) | YES | | 0.00 | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> select * from test1;
Empty set (0.00 sec)
格式:grant 权限 on 数据库名.表名 to 用户名@来源地址 [identified by '密码'];
权限:查询select;增加insert;更新update;删除数据delete;执行execute;all所有。
数据库名和表名可以使用*表示所有库或者表。
mysql> grant select on *.* to 'tom'@'localhost' identified by 'admin123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
格式:reovke 权限 on 数据库名.表名 from 用户名@来源地址;
权限:查询select;增加insert;更新update;删除数据delete;执行execute;all所有。
//查询权限
mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
//撤销权限
mysql> revoke select on school.test from 'tom'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> create table student1(
id int(5) not null primary key auto_increment
) as select name,socre from student;
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| student |
| student1 |
| teacher |
+------------------+
3 rows in set (0.00 sec)
mysql> desc student1;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(5) | NO | PRI | NULL | auto_increment |
| name | char(10) | NO | | NULL | |
| socre | decimal(5,2) | YES | | 0.00 | |
+-------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
mysql> select * from student1;
+----+----------+-------+
| id | name | socre |
+----+----------+-------+
| 1 | lisi | 99.00 |
| 2 | zhangsan | 88.00 |
| 3 | tianqi | 77.00 |
| 4 | qianba | 78.00 |
+----+----------+-------+
4 rows in set (0.00 sec)
mysql> alter table class add teacher char;(默认char字符是1)
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
//修改字段类型
mysql> alter table class change teacher teacher char(10);
Query OK, 3 rows affected (0.13 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> alter table student1 add column course int(5) not null;
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc student1;
+--------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+--------------+------+-----+---------+----------------+
| id | int(5) | NO | PRI | NULL | auto_increment |
| name | char(10) | NO | | NULL | |
| socre | decimal(5,2) | YES | | 0.00 | |
| clas | char(50) | NO | | NULL | |
| course | int(5) | NO | | NULL | |
+--------+--------------+------+-----+---------+----------------+
5 rows in set (0.01 sec)
mysql> alter table student1 drop clas;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select * from student1;
+----+----------+-------+--------+
| id | name | socre | course |
+----+----------+-------+--------+
| 1 | lisi | 99.00 | 0 |
| 2 | zhangsan | 88.00 | 0 |
| 3 | tianqi | 77.00 | 0 |
| 4 | qianba | 78.00 | 0 |
+----+----------+-------+--------+
4 rows in set (0.00 sec)
mysql> alter table class rename to course;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| course |
| student1 |
+------------------+
2 rows in set (0.00 sec)