Mysql基礎語句+數據類型實例

先建一張表用來練習數據庫

create table class(
    id int primary key auto_increment,
    sname varchar(10) not null default '',
    gender char(1) not null default '',
    company varchar(20) not null default '',
    salary decimal(6,2) not null default 0.00,
    fanbu smallint not null default 0
)engine myisam charset utf8;複製代碼


一、insert語句(插入語句)

# 插入中文以前須要輸入 set names gbk;
insert into class
(sname,company, salary)
values
('劉備', '皇室成員', 15.28),
('孫策', '江東集團', 56.34),
('曹操', '宦官後裔', 88.56 );複製代碼


二、update語句(更新語句)

update class
set fanbu  = 20000
where id=3;複製代碼


三、delete語句(刪除語句)

delete from class where salary>80複製代碼


四、select語句(查詢語句基礎,後面學習複雜的查詢語句)

select sname, salary from class;
select * from class where id>3;
複製代碼


五、alter語句(增長列)

#增長一列
alter table class add score tinyint unsigned not null default 0複製代碼


# 在指定的地方增長一列,好比咱們在上圖的基礎上,指定在id列後面增長一列
alter table class add zengjia char(1) not null default '' after id;複製代碼


# 把新列加在最前面,能夠用first 例如
alter table class add fir char(1) not null default '' first;
複製代碼


# 刪除列 語法用drop,例如刪除fir列
alter table class drop fir;複製代碼


# 修改列類型 語法用modify 用法以下
alter table class modify zengjia char(4) not null default '';複製代碼


# 修改列名及列類型 語法用change 例如
alter table class change zengjia hahha char(3) not null default '';複製代碼


insert語句插入檢索的數據,須要注意不用寫values好比,將custnew表查詢的數據插入customers表中bash

insert into customers(
    cust_id,
    cust_email
) select (
    cust_id,
    cyst_email
)from cystnew複製代碼

六、數據類型

6.1數字類型

類型 大小 範圍(有符號) 範圍(無符號) 用途
TINYINT 1 字節 (-128,127) (0,255) 小整數值
SMALLINT 2 字節 (-32 768,32 767) (0,65 535) 大整數值
MEDIUMINT 3 字節 (-8 388 608,8 388 607) (0,16 777 215) 大整數值
INT或INTEGER 4 字節 (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整數值
BIGINT 8 字節 (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 極大整數值

提示,這裏插入一則關於寬度的解釋,例如學習

alter table class add age1 tinyint(1) not null default 0;複製代碼

這裏的tinyint(1),裏面的1是什麼意思,1就是寬度,但在這裏沒有任何效果ui

還有就是zerofill你知道什麼意思嗎,其實這個寬度必須跟zerofill組合起來纔有做用spa

alter table class add snum smallint(5) zerofill not null default 0;複製代碼


這裏能夠看到snum這一個字段default默認是5個0,咱們加一些數據進去,看看效果3d

insert into class (sname, snum) values ('廖化', 15);複製代碼


6.2 小數類型

Float(M,D) decimal(M,D)code

M表明總位數,D表明小數右邊的位數,好比decimal(4,2),表明總共4位,小數兩位,整數就是4-2 =2位cdn

接下來練習一下,創建一個新表blog

create table salary(
    sname varchar(20) not null default '',
    gongzi float(6,2)
)engine myisam charset utf8複製代碼


插入兩條數據圖片

insert into salary 
values
('張三', -9999.99),
('李四', 9999.99)複製代碼


咱們看看浮點數能不能zerofill和unsigned

alter table salary add bonus float(5, 2) unsigned not null default 0.00;複製代碼


插入數據

# (unsigned也能做用於小數,因此報錯了)
insert into salary (sname, bonus) values ('王五', -888.88);
複製代碼


MySQL數據類型 含義
float(m,d) 單精度浮點型 8位精度(4字節) m總個數,d小數位
double(m,d) 雙精度浮點型 16位精度(8字節) m總個數,d小數位

Decimal(M,D) M+2 未打包的浮點數,用法相似於FLOAT和DOUBLE,若是在ASP中使用到Decimal數據類型,直接從數據庫讀出來的Decimal可能須要先轉換成Float或Double類型後再進行運算。

6.3 比較float和decimal(decimal更精確)

好比js裏,0.1+0.2 == 0.3 返回的是false,這就是float的缺陷

#建表
create table account (
    id int not null default 0,
    acc1 float(9,2) not null default 0.00,
    acc2 decimal(9,2) not null default 0.00
)engine myisam charset utf8;複製代碼


# 建表以後,插入數據,來作比較
insert into account 
values
(1, 1234567.23, 1234567.23);
# 如下圖片的結果,就出現了問題,float咱們存的是1234567.23,可是取的時候變成了1234567.25
# decimal就不會出現這個問題複製代碼



6.2 字符類型

類型 大小 用途
CHAR 0-255字節 定長字符串
VARCHAR 0-65535 字節 變長字符串
BLOB 0-65 535字節 二進制形式的長文本數據
TEXT
0-65 535字節 長文本數據

這是最多見的4種字符類型

6.2.1比較一下char和varchar類型

# 建表
# char(N),不夠N個長度,用空格在尾部補夠N個長度,浪費了尾部,可是取出的時候,會刪掉空格
# varchar(N),不用空格補齊,但列內容前,有1-2個字節來標誌該列的內容長度
# 它們的N都是表示的字符,不是字節
create table test(
    ca char(6) not null default '',
    vca varchar(6) not null default ''
)engine myisam charset utf8;複製代碼


# 接着插入數據,來作比較
insert into test values
('aa ', 'aa ')
select concat(ca, '!'), concat(vca, '!') from test;
# 注意下圖,其中char和!之間沒有空格,可是varchar和!之間有空格
# 緣由在於,char存的時候,若是長度是6個字符,你只存了1個字符,那麼剩下空的5個字符就用空白補齊
# 可是補齊以後,取的時候,會自動刪掉後面的空格
# 因此咱們存aa加一個空格的時候,後面那個空格在取的時候就被自動刪掉了
# 可是varchar不會存在這個問題複製代碼


6.2.2 text類型

# 注意text通常用來存文章、新聞內容,聲明text列時,沒必要給默認值,給了會報錯
create table test2(
    article text not null default ''
)複製代碼


# 從新建test2這張表
create table test2(
    article text
);複製代碼


6.2.3 blob的意義

# 在上面test2表的基礎上,增長一個blob類型的列
# blob通常用來存圖像,音頻等二進制信息,用於防止字符集問題形成信息丟失
alter table test2 add img blob;複製代碼


6.3 日期時間類型

MySQL數據類型 含義
date 日期 '2008-12-2'(3個字節)
time 時間 '12:25:36'(3個字節)
datetime 日期時間 '2008-12-2 22:06:44'
timestamp 自動存儲記錄修改時間
year 存儲年份

6.3.1 date類型

# date 範圍 1000-01-01 到 9999-12-31
# 建表
create table test3(
    star varchar(20) not null default '',
    birth date not null default '0000-00-00'
)engine myisam charset utf8;複製代碼


# 插入數據
insert into test3
values
('小張', '1961-02-23');複製代碼


6.3.2 time類型

# 建新列
# time範圍是-838:59:59和'838:59:59'
alter table test3 add sign time not null default '00:00:00';複製代碼


# 插入數據
insert into test3
(star, sign)
values
('rereww','25:10:15');複製代碼


6.3.3 datetime類型

# 建表
# 實際上,通常咱們用時間戳來存年月日+時分秒這個時間
create table test4(
    sname varchar(20) not null default '',
    logintime datetime not null default '0000-00-00 00:00:00'
)engine myisam charset utf8;複製代碼


# 插入數據
insert into test4
values
('張三', '2001-10-12 15:33:19');複製代碼


6.3.4 timesstamp類型

# 建表
create table test5(
    ts timestamp default CURRENT_TIMESTAMP,
    id int
)engine myisam charset utf8;複製代碼


# 插入數據
insert into test5
(id)
values
(1),(2),(3);
# 注意timestamp自動填充當前時間複製代碼


6.3.5 year類型

# 建表
# year 範圍1911-2155(因此通常不用這個類型存年份,還不如用字符串呢)
create table test6 (
    thing varchar(20) not null default '',
    ya year not null default '0000'
)engine myisam charset utf8;複製代碼
相關文章
相關標籤/搜索