關(guān)于mysql基礎(chǔ)知識(shí)的介紹
2024-07-24 12:40:56
供稿:網(wǎng)友
一、啟動(dòng)與退出 1、進(jìn)入MySQL: 啟動(dòng)MySQL Command Line Client(MySQL的DOS界面),直接輸入安裝時(shí)的密碼即可。此時(shí)的提示符是:mysql> 或打開(kāi)終端,輸入SQL語(yǔ)句: mysql –uroot –p123 2、退出MySQL: quit或exit 二、庫(kù)操作 1、創(chuàng)建數(shù)據(jù)庫(kù) 命令:create database <數(shù)據(jù)庫(kù)名> 例如:建立一個(gè)名為xhkdb的數(shù)據(jù)庫(kù) mysql> create database xhkdb; 2、顯示所有的數(shù)據(jù)庫(kù) 命令:show databases (注意:最后有個(gè)s) mysql> show databases; 3、刪除數(shù)據(jù)庫(kù) 命令:drop database <數(shù)據(jù)庫(kù)名> 例如:刪除名為 xhkdb的數(shù)據(jù)庫(kù) mysql> drop database xhkdb; 4、連接數(shù)據(jù)庫(kù) 命令: use <數(shù)據(jù)庫(kù)名> 例如:如果xhkdb數(shù)據(jù)庫(kù)存在,嘗試存取它: mysql> use xhkdb; 屏幕提示:Database changed 5、當(dāng)前選擇(連接)的數(shù)據(jù)庫(kù) mysql> select database(); 6、當(dāng)前數(shù)據(jù)庫(kù)包含的表信息: mysql> show tables; (注意:最后有個(gè)s) 7、創(chuàng)建用戶并賦予取予權(quán)利: grant all privileges on dbname.* to username@localhost identified by ‘pwd123′; 給localhost域的用戶username管理dbname數(shù)據(jù)庫(kù)的所有權(quán)利,密碼為pwd123。 三、表操作,操作之前應(yīng)連接某個(gè)數(shù)據(jù)庫(kù) 1、建表 命令:create table <表名> ( <字段名1> <類型1> [,..<字段名n> <類型n>]); mysql> create table MyClass( > id int(4) not null primary key auto_increment, > name char(20) not null, > sex int(4) not null default '0', > degree double(16,2)); 2、獲取表結(jié)構(gòu) 命令: desc 表名,或者show columns from 表名 mysql> desc MyClass; mysql> show columns from MyClass; 3、刪除表 命令:drop table <表名> 例如:刪除表名為 MyClass 的表 mysql> drop table MyClass; 4、插入數(shù)據(jù) 命令:insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )] 例如,往表 MyClass中插入二條記錄, 這二條記錄表示:編號(hào)為1的名為Tom的成績(jī)?yōu)?6.45, 編號(hào)為2 的名為Joan 的成績(jī)?yōu)?2.99, 編號(hào)為3 的名為Wang 的成績(jī)?yōu)?6.5. mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59); 5、查詢表中的數(shù)據(jù) 1)、查詢所有行 命令: select <字段1,字段2,...> from < 表名 > where < 表達(dá)式 > 例如:查看表 MyClass 中所有數(shù)據(jù) mysql> select * from MyClass; 2)、查詢前幾行數(shù)據(jù) 例如:查看表 MyClass 中前2行數(shù)據(jù) mysql> select * from MyClass order by id limit 0,2; 6、刪除表中數(shù)據(jù) 命令:delete from 表名 where 表達(dá)式 例如:刪除表 MyClass中編號(hào)為1 的記錄 mysql> delete from MyClass where id=1; 7、修改表中數(shù)據(jù): update 表名 set 字段=新值,… where 條件 mysql> update MyClass set where id=1; 8、在表中增加字段: 命令:alter table 表名 add字段 類型 其他; 例如:在表MyClass中添加了一個(gè)字段passtest,類型為int(4),默認(rèn)值為0 mysql> alter table MyClass add passtest int(4) default '0' 9、更改表名: 命令:rename table 原表名 to 新表名; 例如:在表MyClass名字更改為YouClass mysql> rename table MyClass to YouClass; 更新字段內(nèi)容 update 表名 set 字段名 = 新內(nèi)容 update 表名 set 字段名 = replace(字段名,'舊內(nèi)容','新內(nèi)容'); 文章前面加入4個(gè)空格 update article set content=concat(' ',content); 四、字段類型介紹 1.INT[(M)] 型: 正常大小整數(shù)類型 2.DOUBLE[(M,D)] [ZEROFILL] 型: 正常大小(雙精密)浮點(diǎn)數(shù)字類型 3.DATE 日期類型:支持的范圍是1000-01-01到9999-12-31。MySQL以YYYY-MM-DD格式來(lái)顯示DATE值,但是允許你使用字符串或數(shù)字把值賦給DATE列 4.CHAR(M) 型:定長(zhǎng)字符串類型,當(dāng)存儲(chǔ)時(shí),總是是用空格填滿右邊到指定的長(zhǎng)度 5.BLOB TEXT類型,最大長(zhǎng)度為65535(2^16-1)個(gè)字符。 6.VARCHAR型:變長(zhǎng)字符串類型 五、數(shù)據(jù)庫(kù)備份 1.導(dǎo)出整個(gè)數(shù)據(jù)庫(kù) mysqldump -u 用戶名 -p --default-character-set=latin1 數(shù)據(jù)庫(kù)名 > 導(dǎo)出的文件名(數(shù)據(jù)庫(kù)默認(rèn)編碼是latin1) mysqldump -u wcnc -p smgp_apps_wcnc > wcnc.sql 2.導(dǎo)出一個(gè)表 mysqldump -u 用戶名 -p 數(shù)據(jù)庫(kù)名 表名> 導(dǎo)出的文件名 mysqldump -u wcnc -p smgp_apps_wcnc users> wcnc_users.sql 3.導(dǎo)出一個(gè)數(shù)據(jù)庫(kù)結(jié)構(gòu) mysqldump -u wcnc -p -d –add-drop-table smgp_apps_wcnc >d:wcnc_db.sql -d 沒(méi)有數(shù)據(jù) –add-drop-table 在每個(gè)create語(yǔ)句之前增加一個(gè)drop table 4.導(dǎo)入數(shù)據(jù)庫(kù) 常用source 命令 進(jìn)入mysql數(shù)據(jù)庫(kù)控制臺(tái), 如mysql -u root -p mysql>use 數(shù)據(jù)庫(kù) 然后使用source命令,后面參數(shù)為腳本文件(如這里用到的.sql) mysql>source d:wcnc_db.sql