1. 程式人生 > >資料庫基礎知識總結

資料庫基礎知識總結

資料庫編碼經驗總結:
在命令列中顯示中文時,先將操作碼轉換為gbk碼,然後在進行插入資料的操作。(否則會亂碼)
用php新增中文,必須以utf8的格式輸入,
php統一使用utf8插入和查詢資料,這和命令列中的操作碼無關。
1.如何啟動資料庫:
1).我的電腦右鍵-> 管理 -> 服務 -> mysql
1). net start mysql


2.如何關閉資料庫:
1).我的電腦右鍵-> 管理 -> 服務 -> mysql
2). net stop mysql


3.連結資料庫:
1).mysql -uroot -h127.0.0.1 -p
password:root


4.斷開資料庫
quit;


5.檢視資料庫
show databases;
①檢視指定資料庫的資訊:show create database 資料庫名稱;


6.選擇資料庫
use 資料庫名;


7.檢視資料表
show tables;


8.檢視資料表中的資料
select * from 資料表名;


限制返回一條或者零條資訊:
select * from words limit 0,1;


返回資料表中的一條資訊
select * from 資料表名 where condition;


9.建立資料表
create table 資料表名(變數名 型別 主鍵 自增量,變數名……);
例如:
mysql> create table user1(id int primary key auto_increment,
    -> name varchar(32) not null,
    -> password varchar(64) not null,
    -> email varchar(128) not null,
    -> age tinyint not null
    -> )
    -> ;
Query OK, 0 rows affected (0.06 sec)


10.向資料表中新增資料
insert into 資料表名 (變數名1,變數名2,變數名3,……) values('','','');
案例:
insert into user1 (name,password,email,age) values('zs',md5('123456'),'
[email protected]
',30);
insert into user1 (name,password,email,age) values('ls',md5('123456'),'[email protected]',40);
insert into user1 (name,password,email,age) values('ww',md5('123456'),'[email protected]',50);


其中md5能實現對數字的加密處理。


11.刪除表中的個別資料
delete from 資料表名 where condition;


12.修改欄位中的變數值
update 資料表名 set 變數名='' where condition;


13.檢視資料庫字符集
show variables like '%char%';


14.刪除資料庫
drop database 資料庫名;


15.刪除表
drop table 資料表;


16.清空表內容
delete from test01 ;


17.更改資料庫字元編碼
set Variable_name=(utf-8/gbk);


18.檢視當前的資料在哪個資料庫中
\s


19.看錶的結構
desc 資料表名;


20.資料庫的四種語言
DDL(data definition language)資料庫定義語言
包括:
create 
alter 
drop 
truncate 
comment 
rename


DML(data manipulation language)資料操作語言
包括:
selete 
insert 
update 
delete 
merge 
call e
xplain plan 
lock table


DCL(data control language)資料庫控制語言
grant(授權)
revoke(取消授權)


TCL(Transaction Control Language)事務控制語言


DQL(data query language)資料查詢語言


21.模糊查詢:
select * from 資料表名 where condition(變數名) like %變數值%;
* 可以指定為變數名


22.從資料庫中取出制定數量,位置的資料
select * from 資料表名 limit 0,6;
0表示從第一條開始取
6表示去6條資料


23.計算資料庫中總共有多少條資料
select count(*) from 資料表名;
或者:
select count(id) from 資料表名;


24.資料庫的自我複製
insert into 資料表名 (資料1,資料2……)select 資料1,資料2…… from 資料表名;
例如:
insert into emp (name,grade, email, salary) select name,grade,email,salary from emp;





資料庫編碼經驗總結:
在命令列中顯示中文時,先將操作碼轉換為gbk碼,然後在進行插入資料的操作。(否則會亂碼)
用php新增中文,必須以utf8的格式輸入,
php統一使用utf8插入和查詢資料,這和命令列中的操作碼無關。
1.如何啟動資料庫:
1).我的電腦右鍵-> 管理 -> 服務 -> mysql
1). net start mysql


2.如何關閉資料庫:
1).我的電腦右鍵-> 管理 -> 服務 -> mysql
2). net stop mysql


3.連結資料庫:
1).mysql -uroot -h127.0.0.1 -p
password:root


4.斷開資料庫
quit;


5.檢視資料庫
show databases;
①檢視指定資料庫的資訊:show create database 資料庫名稱;


6.選擇資料庫
use 資料庫名;


7.檢視資料表
show tables;


8.檢視資料表中的資料
select * from 資料表名;


限制返回一條或者零條資訊:
select * from words limit 0,1;


返回資料表中的一條資訊
select * from 資料表名 where condition;


9.建立資料表
create table 資料表名(變數名 型別 主鍵 自增量,變數名……);
例如:
mysql> create table user1(id int primary key auto_increment,
    -> name varchar(32) not null,
    -> password varchar(64) not null,
    -> email varchar(128) not null,
    -> age tinyint not null
    -> )
    -> ;
Query OK, 0 rows affected (0.06 sec)


10.向資料表中新增資料
insert into 資料表名 (變數名1,變數名2,變數名3,……) values('','','');
案例:
insert into user1 (name,password,email,age) values('zs',md5('123456'),'
[email protected]
',30);
insert into user1 (name,password,email,age) values('ls',md5('123456'),'[email protected]',40);
insert into user1 (name,password,email,age) values('ww',md5('123456'),'[email protected]',50);


其中md5能實現對數字的加密處理。


11.刪除表中的個別資料
delete from 資料表名 where condition;


12.修改欄位中的變數值
update 資料表名 set 變數名='' where condition;


13.檢視資料庫字符集
show variables like '%char%';


14.刪除資料庫
drop database 資料庫名;


15.刪除表
drop table 資料表;


16.清空表內容
delete from test01 ;


17.更改資料庫字元編碼
set Variable_name=(utf-8/gbk);


18.檢視當前的資料在哪個資料庫中
\s


19.看錶的結構
desc 資料表名;


20.資料庫的四種語言
DDL(data definition language)資料庫定義語言
包括:
create 
alter 
drop 
truncate 
comment 
rename


DML(data manipulation language)資料操作語言
包括:
selete 
insert 
update 
delete 
merge 
call e
xplain plan 
lock table


DCL(data control language)資料庫控制語言
grant(授權)
revoke(取消授權)


TCL(Transaction Control Language)事務控制語言


DQL(data query language)資料查詢語言


21.模糊查詢:
select * from 資料表名 where condition(變數名) like %變數值%;
* 可以指定為變數名


22.從資料庫中取出制定數量,位置的資料
select * from 資料表名 limit 0,6;
0表示從第一條開始取
6表示去6條資料


23.計算資料庫中總共有多少條資料
select count(*) from 資料表名;
或者:
select count(id) from 資料表名;


24.資料庫的自我複製
insert into 資料表名 (資料1,資料2……)select 資料1,資料2…… from 資料表名;
例如:
insert into emp (name,grade, email, salary) select name,grade,email,salary from emp;