一、數(shù)據(jù)庫常用命令
1?創(chuàng)建數(shù)據(jù)庫
??? 1.1?創(chuàng)建mydb1數(shù)據(jù)庫,使用默認字符集
??? create database mydb1;
???
??? 1.2?創(chuàng)建數(shù)據(jù)庫mydb2,字符集使用utf8
??? create database mydb2 character set utf8
???
??? 1.3?創(chuàng)建一個使用utf-8數(shù)據(jù)庫mydb3,并帶有校對規(guī)則
??? create database mydb3
character set utf8 collate utf8_general_ci
?
2?刪除數(shù)據(jù)庫
drop database
if exists
mydb2
?
3?查看所有的數(shù)據(jù)庫
show databases;
?
4?指定使用某個數(shù)據(jù)庫
use mydb2;
?
5?查看當前處于哪個數(shù)據(jù)庫
show tables;
?
二、MySQL數(shù)據(jù)類型
1?整數(shù)類型
??? 1.1?bit(M)
??? M為允許存儲M位值,M默認為1,最大值為64
??? 例子:
??? create table test1(
??? id bit(1)
??? );
?
??? 1.2?tinyint
??? tinyint默認是有符號,有符號范圍是-128 至 127
??? create table test2(
??? id tinyint
??? );
??? tinyint 無符號,無符號范圍是0至255
??? create table test2(
??? id tinyint unsigned
??? );
?
??? 1.3?smallint 是兩個字節(jié)表示的
??? 有符號范圍:-
2
15
至
2
15-1
?
???? 無符號范圍:0 至
2
16
-1
?
??? 1.4?decimal 和 numeric 是變長
?
2?浮點(小數(shù))類型
??? float 占4個字節(jié)
??? float[(m,d)]
??? m:表示整數(shù)有效位數(shù)
??? d:表示小數(shù)點后面位數(shù)
?
??? 2.1?float 不指名小數(shù)保留位數(shù)
??? create table test4(
??? id float
??? );
??? insert into test4 values (77.7777);
?
???
備注:float沒有指定整數(shù)位和小數(shù)位,則插入什么數(shù)據(jù)則顯示什么數(shù)據(jù)
?
??? 2.2?float指名小數(shù)點保留位數(shù)
??? create table test5(
??? id float(5,1)
??? );
??? insert into test4 values(77.7777);
????
??? 存入的數(shù)據(jù)做四舍五入的辦法處理
???
??? 2.3?double 占8個字節(jié),處理方式與float類似
?
??? 2.4?numeric[(m,d)]
??? m:表示整數(shù)有效位數(shù)
??? d:表示小數(shù)點后面位數(shù)
??? create table test6(
??? id numeric
??? );
??? 沒有指明小數(shù)點后面保留位數(shù),則該字段則存放整數(shù)
????
????
??? create table test7(
??? id numeric(7,2) # 表示有效位為7位,小數(shù)點有兩位
??? );
??? 指明小數(shù)點后面保留位數(shù),則該字段則存放小數(shù)
?
3、字符串類型
char(m) ??? |
0<=m<=255 |
文本 |
varchar(m) |
0<=m<=65532 |
文本 |
binary(m) |
0<=m<=255 |
二進制 |
blob |
2 16 |
二進制 |
text |
2 16 |
文本 |
mediumblob |
2 24 |
二進制 |
mediumtext |
2 24 |
文本 |
longblob |
2 32 |
二進制 |
longtext |
2 32 |
文本 |
ENUM |
65535 |
? |
SET |
63 |
? |
?
?
?
4、日期類型
?????mysql> select now() from dual;
??? +---------------------+
??? | now()?????????????? |
??? +---------------------+
??? | 2014-11-10 11:52:44 |
??? +---------------------+
???
備注:now()是獲取數(shù)據(jù)庫當前時間,dual是一個虛擬表,用于測試。
?
???
4.1?date —— 年月日
??? create table test9(
??? birthday date
??? );
?
mysql>? create table test9(
??? ->???? birthday date
??? ->???? );
Query OK, 0 rows affected (0.17 sec)
mysql> insert into test9 values(now());
Query OK, 1 row affected, 1 warning (0.12 sec)
mysql> select * from test9;
+------------+
| birthday?? |
+------------+
| 2014-11-11 |
+------------+
1 row in set (0.00 sec)
????
???
4.2?datetime —— 年月日時分秒
??? create table test10(
??? birthday datetime
??? );
????
????
???
4.3?timestamp —— 年月日時分秒
???
它和datetime最大的區(qū)別是,當你update某條記錄的時候,該列的值就自動更新
。
??? create table test12(
??? name varchar(64),
??? comein datetime,
??? birthday timestamp
??? );
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
