MySQL(六)常用语法和数据类型
閱讀MySQL語(yǔ)法時(shí),需要注意的規(guī)則:
①符號(hào)用來(lái)指出幾個(gè)選擇中的一個(gè),比如:null | not null表示或者給出null或者給出not null;
②包含在方括號(hào)中的關(guān)鍵字或子句(如[like this])是可選的;
③既沒(méi)有列出所有的MySQL語(yǔ)句,也沒(méi)有列出每一條子句和選項(xiàng)。
?
一、常用語(yǔ)法
1、alter table:更新已存在表的模式
alter table tablename
( ? ? ?add column datatype [null | not null] [constarints],
? ? ? ?change column columns datatype [null | not null] ?[contarints].
? ? ? ?drop column,);
2、commit:將事務(wù)處理寫到數(shù)據(jù)庫(kù)
commit;
3、create index:在一個(gè)或多個(gè)列上創(chuàng)建索引
create index indexname?on tablename (column ?[asc | desc],...);
4、create procedure:創(chuàng)建存儲(chǔ)過(guò)程
create procedure procedurename( [parameters] ) ?begin ......?end;
5、create table:創(chuàng)建新數(shù)據(jù)庫(kù)表
create table tablename
( ? ? ? ??column datatype [null | not null] [constraints],
? ? ? ? ? ?column datatype [null | not null] [constraints],
? ? ? ? ? ?......);
6、create user:向系統(tǒng)中添加新的用戶賬戶
create user username[@hostname]?
[identified by [passwrod] 'password'];
7、create view:創(chuàng)建一個(gè)或多個(gè)表上的新視圖
create [or replace] view viewname?as?select ...;
8、delete:從表中刪除一行或多行
delete from tablename [where ...];
9、drop:永久刪除數(shù)據(jù)庫(kù)對(duì)象(表、視圖、索引等)
drop database | index | procedure | table | trigger | user | view itemname;
10、insert:給表增加一行
insert into tablename [(columns,...)] values(values,...)
11、insert select:插入select結(jié)果到一個(gè)表
insert into tablename[(columns,...)]
select columns,... from tablename,...
[where ...];
12、rollback:撤銷一個(gè)事務(wù)處理塊
rollback [ to saveprointname];
13、savepoint:為使用rollback語(yǔ)句設(shè)立保留點(diǎn)
savepoint sql;
14、select:從一個(gè)或多個(gè)表(視圖)中檢索數(shù)據(jù)
select columnname,... from tablename,...
[where ...]
[union ...]
[group by ...]
[having ...]
[order by ...]
15、start transaction:一個(gè)新的事務(wù)處理塊的開(kāi)始
start transaction;
16、update:更新表中的一行或多行
uptable tablename set columname = value, ... [where ...];
?
二、數(shù)據(jù)類型
數(shù)據(jù)類型:定義列中可以存儲(chǔ)什么數(shù)據(jù)以及該數(shù)據(jù)實(shí)際怎樣存儲(chǔ)的基本規(guī)則,其用于以下幾個(gè)目的:
? ? ? ? ? ? ? ?①允許限制可存儲(chǔ)在列中的數(shù)據(jù)
? ? ? ? ? ? ? ?②允許在內(nèi)部更有效的存儲(chǔ)數(shù)據(jù)
? ? ? ? ? ? ? ?③允許變換排序順序(作為數(shù)值數(shù)據(jù)類型,數(shù)值才能正確排序)
1、串?dāng)?shù)據(jù)類型
該類型為最常用的數(shù)據(jù)類型,用來(lái)存儲(chǔ)串(比如名字、地址等);有兩種串類型,分別是定長(zhǎng)串和變長(zhǎng)串
定長(zhǎng)串:接受長(zhǎng)度固定的字符串,其長(zhǎng)度實(shí)在創(chuàng)建表時(shí)指定的;定長(zhǎng)列不允許多余指定的字符數(shù)目,它們分配的存儲(chǔ)空間與指定的一樣多(比如char)
變長(zhǎng)串:存儲(chǔ)可變長(zhǎng)度的文本,有些變長(zhǎng)數(shù)據(jù)類型具有最大定長(zhǎng),有些是完全變長(zhǎng)的,不論哪種,指定的數(shù)據(jù)得到保存即可(靈活)
PS:MySQL處理定長(zhǎng)列比變長(zhǎng)列快速的很多,且MySQL不許云對(duì)變長(zhǎng)列(或一個(gè)列的可變部分)進(jìn)行索引
串?dāng)?shù)據(jù)類型表:
PS:①不管是用任何形式的串?dāng)?shù)據(jù)類型,串值都必須括在引號(hào)內(nèi)(通常使用單引號(hào))
? ? ? ?②如果數(shù)值是計(jì)算(求和平均等)中使用的數(shù)值,應(yīng)存儲(chǔ)在數(shù)值數(shù)據(jù)類型列中;如果作為字符串(可能只包含數(shù)字)使用,則應(yīng)保存在串?dāng)?shù)據(jù)類型列中;
?
2、數(shù)值數(shù)據(jù)類型
數(shù)值數(shù)據(jù)類型存儲(chǔ)數(shù)值,MySQL支持多種數(shù)值數(shù)據(jù)類型,每種存儲(chǔ)的數(shù)值具有不同的取值范圍;
數(shù)值數(shù)據(jù)類型表:
PS:所有數(shù)值數(shù)據(jù)類型(除bit和bollean)都可以有符號(hào)或者無(wú)符號(hào),有符號(hào)數(shù)值列可以存儲(chǔ)正或負(fù)的數(shù)值,無(wú)符號(hào)數(shù)值列只能存儲(chǔ)正數(shù),默認(rèn)情況為有符號(hào)(與串不同,數(shù)值不應(yīng)該在括號(hào)內(nèi))
?
3、日期和時(shí)間數(shù)據(jù)類型
日期和時(shí)間數(shù)據(jù)類型表:
?
4、二進(jìn)制數(shù)據(jù)類型
二進(jìn)制數(shù)據(jù)類型可存儲(chǔ)任何數(shù)據(jù)(甚至包括二進(jìn)制信息),如圖像、多媒體、字處理文檔等
二進(jìn)制數(shù)據(jù)類型表:
總結(jié)
以上是生活随笔為你收集整理的MySQL(六)常用语法和数据类型的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MySQL(五)汇总和分组数据
- 下一篇: MySQL(七)联结表