MySQL資料庫學習【第三篇】增刪改查操作

來源:互聯網
上載者:User

標籤:blog   set   一起   配置   database   刪除速度   pre   問題   技術   

注意
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.建立帳號 identifitycreate user ‘haiyan‘@‘localhost‘ identified by ‘147852‘ # 名為haiyan的本機帳號create user ‘alex‘@‘%‘ identified by ‘123‘ #代表只要ip地址能拼通,那麼所有的使用者都可以遠程登入alexcreate 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. 查看修改結果:\sshow variables like ‘%char%‘永久解決編碼問題
 show variables like ‘char%‘;  查看編碼 

 

 

 

MySQL資料庫學習【第三篇】增刪改查操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.