數(shù)據(jù)庫了解及MySQL學習(持續(xù)更新)
引用請標明出處:http://blog.csdn.net/callon_h/article/details/51882146
數(shù)據(jù)庫基本了解:百度 ”database wiki“ 你會得到數(shù)據(jù)庫的發(fā)展史:
1960s, navigational DBMS(database management system);
1970s, relational DBMS;
Late 1970s, SQL DBMS;
1980s, on the desktop;
1990s, object-oriented;
2000s, NoSQL and NewSQL;
根據(jù)原理性和操作方法的不同大致分為(純屬個人理解):
1.關系型數(shù)據(jù)庫
2.面向對象數(shù)據(jù)庫
3.NoSQL 和 NewSQL(個人只了解了NoSQL)
下面就其操作和原理進行說明:
其中“關系”的理解為一張二維表,行為元組或記錄,列為字段。
其操作可使用SQL語句,完全就邏輯層面對表進行增刪改查,并且保持數(shù)據(jù)的一致性。后面將會說到MySQL,到時候大家看看操作就明白了。
這是在面向對象語言的基礎上發(fā)展而來的,它的思想也是面向對象的思想,下面就其中一種,開放源碼的db4o數(shù)據(jù)庫,來講述其最簡單的操作:
First we create a class to hold our data. It looks like this:
package com.db4odoc.f1.chapter1;
public class Pilot {
private String name;
private int points;
public Pilot(String name,int points) {
this.name=name;
this.points=points;
}
public int getPoints() {
return points;
}
public void addPoints(int points) {
this.points+=points;
}
public String getName() {
return name;
}
public String toString() {
return name+"/"+points;
}
}
Opening the database
// accessDb4o
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
.newConfiguration(), DB4OFILENAME);
try {
// do something with db4o
} finally {
db.close();
}
Storing objects(增)
// storeFirstPilot
Pilot pilot1 = new Pilot("Michael Schumacher", 100);
db.store(pilot1);
System.out.println("Stored " + pilot1);
// storeSecondPilot
Pilot pilot2 = new Pilot("Rubens Barrichello", 99);
db.store(pilot2);
System.out.println("Stored " + pilot2);
Retrieving objects(查)
// retrievePilotByName
Pilot proto = new Pilot("Michael Schumacher", 0);
ObjectSet result = db.queryByExample(proto);
listResult(result);
// retrievePilotByExactPoints
Pilot proto = new Pilot(null, 100);
ObjectSet result = db.queryByExample(proto);
listResult(result);
Updating objects(改)
// updatePilot
ObjectSet result = db.queryByExample(new Pilot("Michael Schumacher", 0));
Pilot found = (Pilot) result.next();
found.addPoints(11);
db.store(found);
System.out.println("Added 11 points for " + found);
retrieveAllPilots(db);
Deleting objects(刪)
// deleteFirstPilotByName
ObjectSet result = db.queryByExample(new Pilot("Michael Schumacher", 0));
Pilot found = (Pilot) result.next();
db.delete(found);
System.out.println("Deleted " + found);
retrieveAllPilots(db);
// deleteSecondPilotByName
ObjectSet result = db.queryByExample(new Pilot("Rubens Barrichello", 0));
Pilot found = (Pilot) result.next();
db.delete(found);
System.out.println("Deleted " + found);
retrieveAllPilots(db);
Full source:
package com.db4odoc.f1.chapter1;
import java.io.*;
import com.db4o.*;
import com.db4odoc.f1.*;
public class FirstStepsExample extends Util {
final static String DB4OFILENAME = System.getProperty("user.home")
+ "/formula1.db4o";
public static void main(String[] args) {
new File(DB4OFILENAME).delete();
accessDb4o();
new File(DB4OFILENAME).delete();
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
.newConfiguration(), DB4OFILENAME);
try {
storeFirstPilot(db);
storeSecondPilot(db);
retrieveAllPilots(db);
retrievePilotByName(db);
retrievePilotByExactPoints(db);
updatePilot(db);
deleteFirstPilotByName(db);
deleteSecondPilotByName(db);
} finally {
db.close();
}
}
public static void accessDb4o() {
ObjectContainer db = Db4oEmbedded.openFile(Db4oEmbedded
.newConfiguration(), DB4OFILENAME);
try {
// do something with db4o
} finally {
db.close();
}
}
public static void storeFirstPilot(ObjectContainer db) {
Pilot pilot1 = new Pilot("Michael Schumacher", 100);
db.store(pilot1);
System.out.println("Stored " + pilot1);
}
public static void storeSecondPilot(ObjectContainer db) {
Pilot pilot2 = new Pilot("Rubens Barrichello", 99);
db.store(pilot2);
System.out.println("Stored " + pilot2);
}
public static void retrieveAllPilotQBE(ObjectContainer db) {
Pilot proto = new Pilot(null, 0);
ObjectSet result = db.queryByExample(proto);
listResult(result);
}
public static void retrieveAllPilots(ObjectContainer db) {
ObjectSet result = db.queryByExample(Pilot.class);
listResult(result);
}
public static void retrievePilotByName(ObjectContainer db) {
Pilot proto = new Pilot("Michael Schumacher", 0);
ObjectSet result = db.queryByExample(proto);
listResult(result);
}
public static void retrievePilotByExactPoints(ObjectContainer db) {
Pilot proto = new Pilot(null, 100);
ObjectSet result = db.queryByExample(proto);
listResult(result);
}
public static void updatePilot(ObjectContainer db) {
ObjectSet result = db
.queryByExample(new Pilot("Michael Schumacher", 0));
Pilot found = (Pilot) result.next();
found.addPoints(11);
db.store(found);
System.out.println("Added 11 points for " + found);
retrieveAllPilots(db);
}
public static void deleteFirstPilotByName(ObjectContainer db) {
ObjectSet result = db
.queryByExample(new Pilot("Michael Schumacher", 0));
Pilot found = (Pilot) result.next();
db.delete(found);
System.out.println("Deleted " + found);
retrieveAllPilots(db);
}
public static void deleteSecondPilotByName(ObjectContainer db) {
ObjectSet result = db
.queryByExample(new Pilot("Rubens Barrichello", 0));
Pilot found = (Pilot) result.next();
db.delete(found);
System.out.println("Deleted " + found);
retrieveAllPilots(db);
}
}
NoSQL數(shù)據(jù)庫:
就MongoDB來進行說明,它和XML數(shù)據(jù)庫一樣,為一種基于文檔的結構化數(shù)據(jù)庫,或者說半結構化的數(shù)據(jù)庫:
mongoDB有3元素:數(shù)據(jù)庫,集合和文檔。集合對應于關系型數(shù)據(jù)庫中的“表”,文檔采用K-V(鍵值對)格式存儲,相當于關系型數(shù)據(jù)庫的“行”。
參考http://www.cnblogs.com/huangxincheng/archive/2012/02/18/2356595.html
安裝數(shù)據(jù)庫不會在此多加贅述,我們直接從它的增刪改查來看它的優(yōu)勢:
1.insert(增)
在數(shù)據(jù)庫有了的基礎上,這里取集合名為“person”,并進行插入:
2.find(查)
我們將數(shù)據(jù)插入后,肯定是要find出來,不然插了也白插,這里要注意兩點:
① “_id”: 這個字段是數(shù)據(jù)庫默認給我們加的GUID,目的就是保證數(shù)據(jù)的唯一性。
② 嚴格的按照Bson的形式書寫文檔,不過也沒關系,錯誤提示還是很強大的。
3.update(改)
update方法的第一個參數(shù)為“查找的條件”,第二個參數(shù)為“更新的值”,學過C#,相信還是很好理解的。
4.remove(刪)
remove中如果不帶參數(shù)將刪除所有數(shù)據(jù),在mongodb中是一個不可撤回的操作,三思而后行。
經(jīng)過這幾步之后,對NoSQL會有一定的了解的,記住,它的一行為文檔,采用鍵值對存儲,正是因為這種存儲方式,才使它的格式不想關系型數(shù)據(jù)庫那么固定和死板。所謂半結構化數(shù)據(jù)庫的”半結構化“,比較好理解的例子是簡歷,每個人簡歷寫的東西都或多或少不會一樣,這樣的存儲在關系型數(shù)據(jù)庫里是不好做的。
http://dev.mysql.com/downloads/
選擇 MySQL Community Server (GPL)
記得點擊 No thanks, just start my download.
下載完成后,找個簡單的目錄解壓即可,如我的解壓完是
E:mysql-5.7.13-win32
在解壓的根目錄下會出現(xiàn)my-default.ini這個文件,我們復制粘貼它產(chǎn)生一個副本,并改名為my.ini,如果沒有my-default.ini就新建吧,這個是一個配置文件,我們需要簡單修改它:
[client]
port=3306
default-character-set=utf8
[mysqld]
port=3306
character_set_server=utf8
basedir=E:mysql-5.7.13-win32
主要有這些參數(shù)就夠了,其中basedir必須設置正確,否則后面會有問題的。
然后配置Path環(huán)境變量,為了能在cmd命令行的任何目錄下都能使用mysql或mysqld命令。
由于5.7+的MySQL版本的根目錄沒有data目錄,所以需要使用命令(最好以管理員身份運行終端):
mysqld --initialize --user=mysql --console
記住這個密碼,這個密碼由于是隨機產(chǎn)生的,終端上看著‘(’‘)’和‘<’‘>’很像,很容易輸錯誤,本文這個就很奇葩的為:
gepFqdJ(e0>P
最后使用
mysqld -install
完成服務的安裝。
啟動服務:
計算機->管理->服務和應用程序->服務
找到名為“MySQL”的服務,啟動即可。
登錄采用:
mysql -u root -p
此時安裝部分完成!
MySQL語句都以’;’作為結束。
第一次使用語句:
show databases;
結果出現(xiàn)了:
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
嘗試了很多方法,最后解決的是:
alter user 'root'@'localhost' identified by 'root';
MySQL常用操作:
1.修改密碼:
mysqladmin -u用戶名 -p舊密碼 password 新密碼
例:
mysqladmin -uroot -pgepFqdJ(e0>P password root
或者登陸進數(shù)據(jù)庫后使用:
mysql> SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘新密碼’);
例:
mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('root');
2.顯示數(shù)據(jù)庫列表:
show databases;
3.創(chuàng)建數(shù)據(jù)庫:
create database 數(shù)據(jù)庫名;
例:
create database callon;
4.使用數(shù)據(jù)庫:
use 數(shù)據(jù)庫名;
例:
use callon;
5.顯示數(shù)據(jù)庫表:
show tables;
6.創(chuàng)建表:
create table 表名 (字段設定列表);
例:
create table target (Shape varchar(255), Color varchar(255), Name varchar(255));
7.顯示數(shù)據(jù)庫表結構:
desc 表名;
例:
desc target;
8.顯示表中記錄:
select * from 表名;
例:
select * from target;
9.增加表中記錄:
Insert into 表名 [(字段1 , 字段2 , ….)] values (值1 , 值2 , …..);
例:
insert into target value ('Square', 'Yellow', 'cup');
10.增加單列:
alter table 表名 add 列名 數(shù)值類型 default 默認值;
例:
alter table target add Size varchar(11);
11.改表中記錄:
由于剛剛增加了Size,并且沒有給出默認值,那么我們之前insert的那一項的Size字段將是NULL,在此基礎上:
update 表名 set 字段1名=字段1值 where 字段2名=字段2值;
其中,字段1為需要改的字段,字段2為查找字段,例:
update target set Size='Middle' where Name='cup';
之后又執(zhí)行了增記錄操作:
insert into target value ('Cylinder', 'Yellow', 'cup', 'Middle');
insert into target value ('Cylinder', 'Green', 'cup', 'Middle');
insert into target value ('Square', 'White', 'book', 'Middle');
insert into target value ('Square', 'Green', 'book', 'Middle');
insert into target value ('Square', 'Green', 'book', 'Small');
12.刪除表中記錄:
delete from 表名 where 字段2=字段2值;
其中,字段2為查找字段,例:
delete from target where Size='Middle' and Color='Green';
13.查找表中指定記錄:
select * from 表名 where 字段2=字段2值;
其中,字段2為查找字段,例:
select * from target where Name='book';
C++調用MySQL及常用操作:
使用軟件Visual Studio 2010,建立項目和源文件不多贅述,直接開始配置部分,項目名->屬性->C/C++:
項目名->屬性->鏈接器:
至此,配置完成,直接源代碼:
#include "mysql.h"
#include
int main()
{
MYSQL *con;
MYSQL_RES *result;
MYSQL_ROW row;
char dbuser[30] = "root";
char dbpasswd[30] = "root";
char dbip[30]="localhost";
char dbname[30] = "callon";
char tablename[30] = "target";
int ret,t;
char *query=NULL;
con = mysql_init((MYSQL *)0);
/*連接MYSQL數(shù)據(jù)庫*/
if(con !=NULL && mysql_real_connect(con, dbip, dbuser, dbpasswd, dbname,3306,NULL,0))
{
printf("connection is OK!n");
if(!mysql_select_db(con, dbname))
{
printf("Select is OK!n");
con->reconnect = 1;
}
else
{
printf("Unable to select the database!n");
}
}
else
{
printf("Unable to connect the database!n");
}
/*MYSQL的增、增、改、查、刪*/
/*增1*/
query = "insert into target value('Circle', 'Red', 'ball', 'Middle')";
ret = mysql_real_query(con,query,strlen(query));
if(ret)
{
printf("Error making query: %s !!!n",mysql_error(con));
}
else
{
printf("insert is OK!n");
}
/*增2*/
query = "insert into target value('Circle', 'Blue', 'ball', 'Middle')";
ret = mysql_real_query(con,query,strlen(query));
if(ret)
{
printf("Error making query: %s !!!n",mysql_error(con));
}
else
{
printf("insert is OK!n");
}
/*改*/
query = "update target set Size='Big' where Name='ball' and Color='Blue'";
ret = mysql_real_query(con,query,strlen(query));
if(ret)
{
printf("Error making query: %s !!!n",mysql_error(con));
}
else
{
printf("update is OK!n");
}
/*查*/
query = "select * from target where Name='ball' and Color='Blue'";
ret = mysql_real_query(con,query,strlen(query));
if(ret)
{
printf("Error making query: %s !!!n",mysql_error(con));
}
else
{
printf("find is OK!n");
}
result = mysql_store_result(con);
while(row = mysql_fetch_row(result))
{
for(t=0;t
運行結果: