mysql 學習筆記

來源:互聯網
上載者:User

標籤:mysql

Mysql 學習筆記
1、安裝 環境:
     Windows 7
     Mysql 5.5 從 http://www.mysql.com下載
     EMS Mysql Query 1.6.0.1
     本機IP:172.5.1.183
2、安裝Mysql,採用預設安裝即可
   目錄選擇在c:\mysql,不要修改預設目錄
3、啟動Mysql
   安裝為服務:c:\mysql\bin\mysqld --install
   啟動Mysql: net start mysql
   停止Mysql: net stop mysql
4、 系統建立的資料庫有mysql,test
   mysql儲存系統資料
   test資料庫用來測試
5、預設登陸方式
   在本機
     c:\mysql\bin\mysql -uroot
     c:\mysql\bin\mysql
   遠程
     mysql -h 172.5.1.183 -uroot
  
   這些是在mysql.user表中,系統預設存在4條資料
        use mysql
        select host,user,password from user;
        +-----------+------+----------+
 | host      | user | password |
 +-----------+------+----------+
 | localhost | root |          |
 | %         | root |          |
 | localhost |      |          |
 | %         |      |          |
 +-----------+------+----------+
  
   這些資料代表的意義:
     使用者名稱為root,密碼為空白的使用者可以從本機和任何遠程主機登陸
     任何使用者名稱,密碼為空白的使用者可以從本機登陸
     使用者名稱為空白,密碼為空白的使用者不可以從遠程登陸 (user中後面的欄位為N,所以無法登陸)
   修改root密碼
     mysql -uroot
     use mysql
     update user set password=PASSWORD("root") where user=‘root‘ and host=‘localhost‘
   下次就需要用mysql -uroot -proot才可以登陸
   在遠程或本機可以使用 mysql -h 172.5.1.183 -uroot 登陸,這個根據第二行的策略確定
   許可權修改生效:
  1)net stop mysql    net start mysql  2)c:\mysql\bin\mysqladmin flush-privileges
      3)登陸mysql後,用flush privileges語句
6、建立資料庫staffer
   create database staffer;
7、下面的語句在mysql環境在執行
   顯示使用者擁有許可權的資料庫       show databases;
   切換到staffer資料庫       use staffer;
   顯示當前資料庫中有許可權的表 show tables;
   顯示表staffer的結構   desc staffer;
8、建立測試環境
   1)建立資料庫staffer
     mysql> create database staffer
   2)建立表staffer,department,position,depart_pos
   
 create table s_position     (         id int not null auto_increment,         name varchar(20) not null default '經理', #設定預設值         description varchar(100),         primary key PK_positon (id)  #設定主鍵     );         create table department     (         id int not null auto_increment,         name varchar(20) not null default '系統部', #設定預設值         description varchar(100),         primary key PK_department (id)  #設定主鍵     );     create table depart_pos     (         department_id int not null,         position_id int not null,         primary key PK_depart_pos (department_id,position_id)  #設定複和主鍵     );     create table staffer     (         id int not null auto_increment primary key,  #設定主鍵         name varchar(20) not null default '無名氏',  #設定預設值         department_id int not null,         position_id int not null,         unique (department_id,position_id)  #設定唯一值     );

   3)刪除
     mysql>
        drop table depart_pos;
        drop table department;
        drop table s_position;
        drop table staffer;
        drop database staffer;
9、修改結構
   mysql>
   #表position增加列test
   alter table position add(test char(10));
   #表position修改列test
   alter table position modify test char(20) not null;
   #表position修改列test預設值
   alter table position alter test set default ‘system‘;
   #表position去掉test預設值
   alter table position alter test drop default;
   #表position去掉列test
   alter table position drop column test;
   #表depart_pos刪除主鍵
   alter table depart_pos drop primary key;
   #表depart_pos增加主鍵
   alter table depart_pos add primary key PK_depart_pos (department_id,position_id);
10、操作資料
   #插入表department
   insert into department(name,description) values(‘系統部‘,‘系統部‘);
   insert into department(name,description) values(‘公關部‘,‘公關部‘);
   insert into department(name,description) values(‘客服部‘,‘客服部‘);
   insert into department(name,description) values(‘財務部‘,‘財務部‘);
   insert into department(name,description) values(‘測試部‘,‘測試部‘);
   #插入表s_position
   insert into s_position(name,description) values(‘總監‘,‘總監‘);
   insert into s_position(name,description) values(‘經理‘,‘經理‘);
   insert into s_position(name,description) values(‘普通員工‘,‘普通員工‘);
   #插入表depart_pos
   insert into depart_pos(department_id,position_id)
   select a.id department_id,b.id postion_id
   from department a,s_position b;
   #插入表staffer
   insert into staffer(name,department_id,position_id) values(‘陳達治‘,1,1);
   insert into staffer(name,department_id,position_id) values(‘李文賓‘,1,2);
   insert into staffer(name,department_id,position_id) values(‘馬佳‘,1,3);
   insert into staffer(name,department_id,position_id) values(‘亢志強‘,5,1);
   insert into staffer(name,department_id,position_id) values(‘楊玉茹‘,4,1);
11、 查詢及刪除操作
   #顯示系統部的人員和職位
   select a.name,b.name department_name,c.name position_name
   from staffer a,department b,s_position c
   where a.department_id=b.id and a.position_id=c.id and b.name=‘系統部‘;
   #顯示系統部的人數
   select count(*) from staffer a,department b
   where a.department_id=b.id and b.name=‘系統部‘
   #顯示各部門的人數
   select count(*) cou,b.name
   from staffer a,department b
   where a.department_id=b.id
   group by b.name;
   #刪除客服部
   delete from department where name=‘客服部‘;
   #將財務部修改為財務一部
   update department set name=‘財務一部‘ where name=‘財務部‘;
12、 備份和恢複
    備份資料庫staffer
     c:\mysql\bin\mysqldump -uroot -proot staffer>e:\staffer.sql
     得到的staffer.sql是一個sql指令碼,不包括建庫的語句,所以你需要手工
     建立資料庫才可以匯入
    恢複資料庫staffer,需要建立一個空庫staffer
     c:\mysql\bin\mysql -uroot -proot staffer<staffer.sql
     如果不希望後來手工建立staffer,可以
     c:\mysql\bin\mysqldump -uroot -proot --databases staffer>e:\staffer.sql
     mysql -uroot -proot >e:\staffer.sql
     但這樣的話系統種就不能存在staffer庫,且無法匯入其他名字的資料庫,
     當然你可以手工修改staffer.sql檔案
13、 從文本向資料庫匯入資料
    1)使用工具c:\mysql\bin\mysqlimport
    這個工具的作用是將檔案匯入到和去掉副檔名名字相同的表裡,如
    staffer.txt,staffer都是匯入到staffer表中
    常用選項及功能如下
 -d or --delete 新資料匯入資料表中之前刪除資料資料表中的所有資訊
 -f or --force 不管是否遇到錯誤,mysqlimport將強制繼續插入資料
 -i or --ignore mysqlimport跳過或者忽略那些有相同唯一
 關鍵字的行, 匯入檔案中的資料將被忽略。
 -l or -lock-tables 資料被插入之前鎖住表,這樣就防止了,
 你在更新資料庫時,使用者的查詢和更新受到影響。
 -r or -replace 這個選項與-i選項的作用相反;此選項將替代
 表中有相同唯一關鍵字的記錄。
 --fields-enclosed- by= char
 指定文字檔中資料的記錄時以什麼括起的, 很多情況下
 資料以雙引號括起。 預設的情況下資料是沒有被字元括起的。
 --fields-terminated- by=char
 指定各個資料的值之間的分隔 符,在句號分隔的檔案中,
 分隔字元是句號。您可以用此選項指定資料之間的分隔字元。
 預設的分隔字元是跳格符(Tab)
 --lines-terminated- by=str
 此選項指定文字檔中行與行之間資料的分隔字串
 或者字元。 預設的情況下mysqlimport以newline為行分隔字元。
 您可以選擇用一個字串來替代一個單個的字元:
 一個新行或者一 個斷行符號。
 mysqlimport命令常用的選項還有-v 顯示版本(version),
            -p 提示輸入密碼(password)等。
   這個工具有個問題,無法忽略某些列,這樣對我們的資料匯入有很大的麻煩,雖然
   可以手工設定這個欄位,但會出現莫名其妙的結果,我們做一個簡單的樣本
   我們定義如下的depart_no.txt,儲存在e盤,間隔為定位字元\t
 10 10
 11 11
 12 24 
 執 行如下命令
    c:\mysql\bin\mysqlimport -uroot -proot staffer e:\depart_pos.txt
    在這裡沒有使用列的包圍符號,分割採用預設的\t,因為採用別的符號會有問題,
    不知道是不是windows的原因
    2)Load Data INFILE file_name into table_name(column1_name,column2_name)
      這個命令在mysql>提示符下使用,優點是可以指定列匯入,樣本如下
      c:\mysql\bin\mysql -uroot -proot staffer
      mysql>load data infile "e:/depart_no.txt" into depart_no(department_id,position_id);
   
    這兩個工具在Windows下使用都有問題,不知道是Windows的原因還是中文的問題,
    而且不指定的列它產生了空值,這顯然不是我們想要的,所以謹慎使用這些工具

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.