1. 程式人生 > >庫、表曾刪改查和存儲引擎

庫、表曾刪改查和存儲引擎

for aps gif isp int 技術分享 ESS 處理機制 serve

一、數據庫的增刪改查

增:create database xiaoyu;

技術分享圖片
mysql> create database xiaoyu;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
| xiaoyu             |
+--------------------+
6 rows in
set (0.00 sec)

刪:drop database xiaoyu;

技術分享圖片
mysql> drop database xiaoyu;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
+--------------------+
5 rows in
set (0.00 sec) mysql>

改:alter database xiaoyu charset utf8;

技術分享圖片
mysql> alter database xiaoyu charset utf8;
Query OK, 1 row affected (0.00 sec)

mysql> 

查:show create database xiaoyu;

技術分享圖片
mysql> show create database xiaoyu;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| xiaoyu   | CREATE DATABASE `xiaoyu` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in
set (0.04 sec) mysql>

查所有的數據庫:show databases;

技術分享圖片
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
| wordpress          |
| xiaoyu             |
+--------------------+
6 rows in set (0.00 sec)
查所有數據庫

二、表的增刪改查

曾:create table member(id int,name char);

刪:drop table member;

改:insert into member(id,name) values(1,‘y‘);

不寫給誰傳值就指定都傳,而且還是按照位置傳

查:show tables; #查看所有表

   show create table t1;#查看指定的表
   select * from t1 #查看表數據
   select id,name from t1  #查看表數據
engine = InnoDB
表的存儲引擎是InnoDB
mysql> show tables;
+------------------+
| Tables_in_xiaoyu |
+------------------+
| member |
+------------------+
1 row in set (0.00 sec)

mysql> show table();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near () at line 1
mysql> show create table member\G
*************************** 1. row ***************************
Table: member
Create Table: CREATE TABLE `member` (
`id` int(11) DEFAULT NULL,
`name` char(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> select * from member;
+------+------+
| id | name |
+------+------+
| 1 | y |
+------+------+
1 row in set (0.00 sec)

mysql> select * from member;
+------+------+
| id | name |
+------+------+
| 1 | y |
+------+------+
1 row in set (0.00 sec)

mysql> select id,mane from member;
ERROR 1054 (42S22): Unknown column mane in field list
mysql> select id,name from member;
+------+------+
| id | name |
+------+------+
| 1 | y |
+------+------+
1 row in set (0.00 sec)

關於存儲引擎:

一種類型表明的是一種存儲方式
存儲引擎:就是表的類型,不同的類型就會對應不同的處理機制去處理他
事務:就是要麽同時成功,要麽同時不成功

使用存儲引擎

方法1:建表時指定

存儲引擎
create table t1(id int) engine = innodb 會出現2個文件
create table t2(id int) engine = myisam 會出現3個文件(速度比上面的快),但是我們還是用innodb存儲 
create table t3(id int) engine = memory 只有表結構沒有表數據,是創建到內存中的
create table t4(id int) engine = blackhole  黑洞,吃數據(數據存進去就沒有了,存進去就沒有了)

方法2:在配置文件中指定默認的存儲引擎

/etc/my.cnf
[mysqld]
default-storage-engine=INNODB
innodb_file_per_table=1

庫、表曾刪改查和存儲引擎