table of Contents
Three .MySQL database structures
IV. The basic operation of the database command
A basic database concepts
The role of the database
- So that data can be saved persistent, high reliability, high availability, data can quickly extract
The type of database storage
- Relational databases: stored value, character, string, Boolean value. mysql (oracle), sql server (Microsoft), oracle, db2 (IBM), access (Microsoft office), sybase
Note: The relational database is a database system based on the relational model, its basic concepts from the relational model. Relational model based on the theory of relational algebra above, the data structure using a simple two-dimensional data table can be used directly ER diagram showing, ER diagram entity contains (data objects), the relationship between the three elements and attributes
- Non-relational databases: storage of pictures, video, and voice. NOSQL (collectively), MongoDB, redis (memory database / cache database), memcache (memory database / cache database)
Note: memcache and redis same point: the heat storage database; different points of memcache and redis: redis can be done persistently saved, you can store objects
The basic idea of the database
- Data: descriptor record things, including numbers, text, graphics, images, sound, archival records, in order to "record" in the form of stored according to a uniform format
- Datasheet: recording different tissues together, to form a "table", it is used to store data specific
- Database: is a collection, storing data warehouse tables, data related to each other in a certain way organizations store
II. Non-relational database
Introduction of non-relational databases
- Non-relational database is also known as NoSQL, data is not stored in a relational model is based, does not require fixed table format, non-relational database as a complementary relational database, in the era of increasingly rapid development of the site plays a high efficiency and high performance.
- The advantage of non-relational databases: the demand for high concurrent read and write database; efficient mass data storage and access; high scalability and high availability requirements for the database
Storage of non-relational databases
- Key - value way to store based on the key, delete, change data.
- Storage column, the relevant data is stored in the column family
- How the document, the database consists of a series of data items, each data item has a name and a value corresponding to
- Graphically entity as a vertex, edge relationship, save data to a graphic
Non-relational database products
- redis data storage is a way to Key-value, the data is stored in memory, but will regularly write data to disk
- redis relative to the memcached has the following features: support memory caching, support for persistent, more data types, support for distributed clusters, support queue
Note: the cache before the database, using the session session sharing to improve the user experience
Three .MySQL database structures
- Oracle's MySQL is an open source relational database, to comply with the GPL, free to use and modify
- Has the following characteristics: high performance, service stable, open source, copyright-free, low-cost, multi-threaded, multi-user, based on C / S structure, safe and reliable
- Into MySQL Community Edition and Business Edition
- Specific operation to install MySQL-5.6 version
##解压mysql-5.6版数据包
tar xzvf mysql-5.6.26.tar.gz -C /opt
##安装环境包
yum install -y ncurses-devel autoconf cmake
##安装必要功能模块
cd /opt/mysql-5.6.26
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DEXTRA_CHARSETS=all \
-DSYSCONFIDIR=/etc \
-DMYSQL_DATADIR=/home/mysql/ \
-DMYSQL_UNIX_ADDR=/home/mysql/mysql.sock
##编译与安装
make && make install
##覆盖本机的数据库,和前面的配置文件的目录一致
cp support-files/my-default.cnf /etc/my.cnf
##优化服务控制
cp support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
chkconfig --add /etc/init.d/mysqld
chkconfig mysqld --level 35 on
##设置环境变量,将启动脚本放到系统中
echo "PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile
echo $PATH
##添加mysql用户
useradd -s /sbin/nologin mysql
chown -R mysql:mysql /usr/local/mysql/
##初始化
/usr/local/mysql/scripts/mysql_install_db \
--user=mysql \
--ldata=/var/lib/mysql \
--basedir=/usr/local/mysql \
--datadir=/home/mysql
ln -s /var/lib/mysql/mysql.sock /home/mysql/mysql.sock
##修改启动脚本
#指明工作路径
basedir=/usr/local/mysql
#数据存放位置
datadir=/home/mysql
##启动服务
service mysqld start
##给MySQL用户创建密码
mysqladmin -u root -p password "abc123"
IV. The basic operation of the database command
- View a list of database information
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql>
- View the data in the database table
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
28 rows in set (0.00 sec)
mysql>
- Structure of the display data table
mysql> describe student;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(4) | NO | PRI | NULL | |
| name | char(10) | NO | | NULL | |
| age | int(2) | NO | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql>
Five basic commands .SQL
- SQL language is Structured Query Language, standard language in the act as a relational database, a database for the maintenance and management, such as add or delete data search change
- SQL Category: DDL: data definition language; DML: Data Manipulation Language; DQL: data query language; DCL: Data Control Language
DDL, data definition language
- Create a database, create database database name;
mysql> create database school;
- Create a data table, create table table name (field definitions ...)
mysql> use school
Database changed
mysql> create table student (id int(4) not null,name char(10) not null,age int(2) not null,primary key(id));
NOTE: primary key primary key, unique, Nonemptiness; char, char; int, int
- Delete a library table
#删除表
mysql>drop table school.student;
#删除库
mysql>drop database school;
DML: Data Manipulation Language
- Use insert, insert new data
- insert into student(id,name,age) values(“1001”,“张三”,“20”)
mysql> use school
Database changed
mysql> insert into student(id,name,age) values("1001","张三","20");
Query OK, 1 row affected (0.08 sec)
##查询表
mysql> select * from student;
+------+--------+-----+
| id | name | age |
+------+--------+-----+
| 1001 | 张三 | 20 |
+------+--------+-----+
1 row in set (0.01 sec)
mysql>
- Use update, replace the original data
- update database name Table 1 Field name = set value of the conditional expression 1where
mysql> select * from student;
+------+--------+-----+
| id | name | age |
+------+--------+-----+
| 1001 | 张三 | 20 |
| 1002 | 李四 | 22 |
| 1003 | 王五 | 22 |
| 1004 | 赵六 | 19 |
+------+--------+-----+
4 rows in set (0.01 sec)
mysql> update student set name="田七" where id="1004";
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+------+--------+-----+
| id | name | age |
+------+--------+-----+
| 1001 | 张三 | 20 |
| 1002 | 李四 | 22 |
| 1003 | 王五 | 22 |
| 1004 | 田七 | 19 |
+------+--------+-----+
4 rows in set (0.00 sec)
mysql>
- Use delete, delete the specified data
- delete from table where conditional expression
mysql> select * from student;
+------+--------+-----+
| id | name | age |
+------+--------+-----+
| 1001 | 张三 | 20 |
| 1002 | 李四 | 22 |
| 1003 | 王五 | 22 |
| 1004 | 田七 | 19 |
+------+--------+-----+
4 rows in set (0.00 sec)
mysql> delete from student where name="张三";
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+------+--------+-----+
| id | name | age |
+------+--------+-----+
| 1002 | 李四 | 22 |
| 1003 | 王五 | 22 |
| 1004 | 田七 | 19 |
+------+--------+-----+
3 rows in set (0.00 sec)
mysql>
DQL: Data Query Language
- select statement, qualifying for querying data record from the specified table
- select Field 1, Field 2, ... from table where conditional expression
- When representation can be expressed in all fields *
mysql> select * from student;
+------+--------+-----+
| id | name | age |
+------+--------+-----+
| 1002 | 李四 | 22 |
| 1003 | 王五 | 22 |
| 1004 | 田七 | 19 |
+------+--------+-----+
3 rows in set (0.00 sec)
mysql> select id,name from student where name="李四";
+------+--------+
| id | name |
+------+--------+
| 1002 | 李四 |
+------+--------+
1 row in set (0.00 sec)
mysql>
DCL: Data Control Language
- Set user permissions (the user does not exist, create a new user)
- grant permission list on the database name to watch username @ Source Address [identified by 'password']
Note: If the user exists, represents the change password; if the user does not exist, on behalf of the new user
- View user's privileges
show grants for 用户名@来源地址
- Revoke a user's privileges
revoke 权限列表 on 数据库名.表名 from 用户名@来源地址