1. 程式人生 > >mysql學習【第3篇】:資料庫之增刪改查操作 資料庫之表操作,資料操作

mysql學習【第3篇】:資料庫之增刪改查操作 資料庫之表操作,資料操作

資料庫之表操作,資料操作
注意的幾點:
1.如果你在cmd中書命令的時候,輸入錯了就用\c跳出

  2.\s檢視配置資訊

一、操作資料夾(庫)
增:create database db1 charset utf8;
刪:drop database db1;
改:alter database db1 charset gbk;
查:show databases; #檢視所有的資料庫
    show create database db1; #檢視db1資料庫
二、操作檔案(表)
複製程式碼
切換到資料夾下:use db1
增:create table t1(id int,name char(10)) engine=innodb;
刪:drop table t1;
改:alter table t1 add age int;
    alter table t1 modify name char(12);
查:show tables; #檢視所有表
    show create table t1; #檢視t1表
    desc t1;#查看錶結構
    show create table t1\G; #查看錶詳細結構,可加\G
    select * from t1; #檢視所有的表資料
複製程式碼
三、操作檔案的一行行內容(記錄)
複製程式碼
增:insert into db1.t1 values(1,'haiyan'),(2,'yaling'),(3,'xiaoxiao'); #如果t1不給引數,預設按照位置引數依次傳參
刪:delete from t1 where id = 2;
    #對於清空記錄有兩種方式,但是推薦後者
    delete from t1;
    truncate t1; #當資料量比較大的情況下,使用這種方式,刪除速度快
改:update t1 set name = 'SB' where id=3;
    update t1 set name= 'SB'  where name = 'xiaoxiao';
    alter table t7 modify id int primary key auto_increment;  修改id為主鍵並且自增
查:select * from t1; #檢視t1裡所有的資料
    select name from t1;  #檢視t1裡所有的name
    select id,name from t1; #檢視t1裡所有的id,name
複製程式碼
四、自增id的方法
複製程式碼
create table t5(id int primary key auto_increment,name char(10));
#create table t4(id int not null unique auto_increment,name char(10));  (不空且是唯一的)#這個和上面的是一回事
insert into xx(name) values ('haiyan1'),
                                ('haiyan2'),
                                ('haiyan3'),
                                ('haiyan4'),
                                ('haiyan5');
複製程式碼
五、拷貝表結構
複製程式碼
create table t7(id int,name char(10));
create table t8 select * from t7;  #拷貝表結果(如果有資料就把資料一起拷貝了)
create table t8 select * from t5 where 1=2; #拷貝表結構,不拷貝表資料(條件為假時,查不到任何記錄)
alter table t7 modify id int primary key auto_increment;  修改id為主鍵並且自增
insert into t7(name) values   ('egon1'),
                              ('egon1'),
                              ('egon1'),
                              ('egon1');
複製程式碼
6.delete from t7 where id = 1; #刪記錄(只是刪除一行當id=1的時候)
7.update t7 set name = '';#修改欄位對應的值

修改id為主鍵並且遞增

六、建立賬號
複製程式碼
8.select user()#檢視當前使用者
select * from mysql.user; 檢視所有的使用者
9.建立賬號 identifity create user 'haiyan'@'localhost' identified by '147852' # 名為haiyan的本機賬號 create user 'alex'@'%' identified by '123' #代表只要ip地址能拼通,那麼所有的使用者都可以遠端登入alex create user 'susan'@'192.168.20.%' identified by '123' #建立遠端賬號,只要是192.168.20.?開頭的ip都可以登入susan #如果你要遠端登入alex的賬戶,那麼客戶端得這樣登入 :mysql -h192.168.20.97 -ualex -p123
複製程式碼
七、資料庫的許可權操作
複製程式碼
#insert ,select ,update,delete #有這麼幾個可以設定許可權的操作,那麼我們先以select為例吧。
分四個級別:
級別1:對所有的庫,下的所有的表,下的所有的欄位
'''*.*代表所有的庫下的所有的表'''
同意select許可權開放,開放的是*.*的select許可權開放給使用者
grant select on *.* to 'zhang'@'localhost' identified by '123';  #讓建立使用者的時候賦予許可權
級別2:對db1庫,下的所有的表,下的所有的欄位
grant select on db1.* to 'wang'@'localhost' identified by '123';
級別3:對錶db1.t1,下的多有欄位
grant select on db1.t1 to 'li'@'localhost' identified by '123'; 級別4:對錶db1.t1,下的id,name,欄位 grant select (id ,name) on db1.t1 to 'zhao'@'localhost' identifitied by '123'; grant select (id ,name),update(name) on db1.t1 to 'zhao'@'localhost' identifitied by '123'; 修改完許可權後要記得重新整理許可權 flush privileges; 刪除許可權: revoke select on *.* from 'zhang'@'localhost' revoke select on db1.* from 'wang'@'localhost' revoke select on db1.t1 from 'li'@'localhost' revoke select (id ,name),update(name) on db1.t1 from 'zhao'@'localhost'
複製程式碼
1.建立本地使用者並賦予許可權

使用者本地登入,就無需IP地址了

2.建立使用者只要Ip能配通,所有的使用者都能登入

客戶登入

其他的都一樣,就不一一的說了

八、 解決亂碼問題

 
       
       
        複製程式碼
        
       
#1. 修改配置檔案
[mysqld]
default-character-set=utf8 
[client]
default-character-set=utf8 
[mysql]
default-character-set=utf8

#mysql5.5以上:修改方式有所改動
    [mysqld]
    character-set-server=utf8 collation-server=utf8_general_ci [client] default-character-set=utf8 [mysql] default-character-set=utf8 #2. 重啟服務 #3. 檢視修改結果: \s show variables like '%char%' 永久解決編碼問題
複製程式碼  show variables like 'char%';  檢視編碼 
注意的幾點:
1.如果你在cmd中書命令的時候,輸入錯了就用\c跳出

  2.\s檢視配置資訊

一、操作資料夾(庫)
增:create database db1 charset utf8;
刪:drop database db1;
改:alter database db1 charset gbk;
查:show databases; #檢視所有的資料庫
    show create database db1; #檢視db1資料庫
二、操作檔案(表)
複製程式碼
切換到資料夾下:use db1
增:create table t1(id int,name char(10)) engine=innodb;
刪:drop table t1;
改:alter table t1 add age int;
    alter table t1 modify name char(12);
查:show tables; #檢視所有表
    show create table t1; #檢視t1表
    desc t1;#查看錶結構
    show create table t1\G; #查看錶詳細結構,可加\G
    select * from t1; #檢視所有的表資料
複製程式碼
三、操作檔案的一行行內容(記錄)
複製程式碼
增:insert into db1.t1 values(1,'haiyan'),(2,'yaling'),(3,'xiaoxiao'); #如果t1不給引數,預設按照位置引數依次傳參
刪:delete from t1 where id = 2;
    #對於清空記錄有兩種方式,但是推薦後者
    delete from t1;
    truncate t1; #當資料量比較大的情況下,使用這種方式,刪除速度快
改:update t1 set name = 'SB' where id=3;
    update t1 set name= 'SB'  where name = 'xiaoxiao';
    alter table t7 modify id int primary key auto_increment;  修改id為主鍵並且自增
查:select * from t1; #檢視t1裡所有的資料
    select name from t1;  #檢視t1裡所有的name
    select id,name from t1; #檢視t1裡所有的id,name
複製程式碼
四、自增id的方法
複製程式碼
create table t5(id int primary key auto_increment,name char(10));
#create table t4(id int not null unique auto_increment,name char(10));  (不空且是唯一的)#這個和上面的是一回事
insert into xx(name) values ('haiyan1'),
                                ('haiyan2'),
                                ('haiyan3'),
                                ('haiyan4'),
                                ('haiyan5');
複製程式碼
五、拷貝表結構
複製程式碼
create table t7(id int,name char(10));
create table t8 select * from t7;  #拷貝表結果(如果有資料就把資料一起拷貝了)
create table t8 select * from t5 where 1=2; #拷貝表結構,不拷貝表資料(條件為假時,查不到任何記錄)
alter table t7 modify id int primary key auto_increment;  修改id為主鍵並且自增
insert into t7(name) values   ('egon1'),
                              ('egon1'),
                              ('egon1'),
                              ('egon1');
複製程式碼
6.delete from t7 where id = 1; #刪記錄(只是刪除一行當id=1的時候)
7.update t7 set name = '';#修改欄位對應的值

修改id為主鍵並且遞增

六、建立賬號
複製程式碼
8.select user()#檢視當前使用者
select * from mysql.user; 檢視所有的使用者
9.建立賬號 identifity create user 'haiyan'@'localhost' identified by '147852' # 名為haiyan的本機賬號 create user 'alex'@'%' identified by '123' #代表只要ip地址能拼通,那麼所有的使用者都可以遠端登入alex create user 'susan'@'192.168.20.%' identified by '123' #建立遠端賬號,只要是192.168.20.?開頭的ip都可以登入susan #如果你要遠端登入alex的賬戶,那麼客戶端得這樣登入 :mysql -h192.168.20.97 -ualex -p123
複製程式碼
七、資料庫的許可權操作
複製程式碼
#insert ,select ,update,delete #有這麼幾個可以設定許可權的操作,那麼我們先以select為例吧。
分四個級別:
級別1:對所有的庫,下的所有的表,下的所有的欄位
'''*.*代表所有的庫下的所有的表'''
同意select許可權開放,開放的是*.*的select許可權開放給使用者
grant select on *.* to 'zhang'@'localhost' identified by '123';  #讓建立使用者的時候賦予許可權
級別2:對db1庫,下的所有的表,下的所有的欄位
grant select on db1.* to 'wang'@'localhost' identified by '123';
級別3:對錶db1.t1,下的多有欄位
grant select on db1.t1 to 'li'@'localhost' identified by '123'; 級別4:對錶db1.t1,下的id,name,欄位 grant select (id ,name) on db1.t1 to 'zhao'@'localhost' identifitied by '123'; grant select (id ,name),update(name) on db1.t1 to 'zhao'@'localhost' identifitied by '123'; 修改完許可權後要記得重新整理許可權 flush privileges; 刪除許可權: revoke select on *.* from 'zhang'@'localhost' revoke select on db1.* from 'wang'@'localhost' revoke select on db1.t1 from 'li'@'localhost' revoke select (id ,name),update(name) on db1.t1 from 'zhao'@'localhost'
複製程式碼
1.建立本地使用者並賦予許可權

使用者本地登入,就無需IP地址了

2.建立使用者只要Ip能配通,所有的使用者都能登入

客戶登入

其他的都一樣,就不一一的說了

八、 解決亂碼問題

 
 
 
  複製程式碼
  
 
#1. 修改配置檔案
[mysqld]
default-character-set=utf8 
[client]
default-character-set=utf8 
[mysql]
default-character-set=utf8

#mysql5.5以上:修改方式有所改動
    [mysqld]
    character-set-server=utf8 collation-server=utf8_general_ci [client] default-character-set=utf8 [mysql] default-character-set=utf8 #2. 重啟服務 #3. 檢視修改結果: \s show variables like '%char%' 永久解決編碼問題
複製程式碼  show variables like 'char%';  檢視編碼