MySQL 學習筆記 一

摘自:http://www.cnblogs.com/hoojo/archive/2011/06/20/2085390.html html

cmd    命令行輸入(若是沒配置環境變量要指定到具體目錄)
mysql -uroot -p (回車後輸入密碼,便可進入mysql) mysql

MySQL的結束符是「;」  sql

經常使用查詢:

1.顯示全部數據庫:
show databases;

2.刪除數據庫dbName:
drop database dbName;
數據庫

3.建立數據庫 dbName:
create database [if not exists] dbName;
中括號部分可選的,用於判斷該數據庫是否存在,不存在就建立。(SQL語句中不用寫 [] )
函數

4.切換,使用指定的數據庫dbName:
use dbName;
spa

5.顯示當前數據庫全部的表對象:
show tables;
命令行

6.顯示錶(tableName)的結構 describe (desc) :
desc tableName;
orm

7.建立一張表[if not exists] user :
create table [if not exists] user (
        --int 整型
        uId int,
        --小數
        uPrice decimal,
        --普通長度文本,default設置默認值
        uName varchar(255) default ‘zhangsan’,
        --超長文本
        uRemark text,
        --圖片
        uPhoto blob,
        --日期
        uBirthday datetime
);
htm

8.子查詢建表方法
部分列名匹配模式:
create table userInfo ( name varchar(20),sex char) as select name,sex from user;
列名和類型要對應
所有列名模式:
create table userInfo as select * from user;
直接將整個表的類型和數據備份到新的userInfo中
對象

9.添加表字段
添加單列:
alter table user add tel varchar(11) default '12345678901';
添加多列:
alter table user add (photo blob,birthday date);

10.修改表字段:
修改tel列:
alter table user modify tel varchar(15) default '';
修改tel 列的位置(必須設置類型 否則出錯)
alter table user modify tel varchar(15) first ;
修改tel列到指定的位置
alter table user modify tel varchar(15) after age;
修改多列:
alter table user modify tel varchar(11) first,modify name varchar(20) after age;
注意:alter modify不支持一次修改多個列,可是Oracle支持多列修改,可是MySQL能夠經過多個modify的方式完成。

11.刪除指定字段:
alter table user drop tel;

12.重命名錶數據:
重命名錶
alter table user rename to user;
字段重命名
alter table user change name uName varchar(50);
alter table user change sex uSex varchar(2) after uName;
若是須要改變列名建議使用 change ,若是須要改變數據類型和顯示位置可使用modify

13.刪除表
drop table user;
drop刪除表會刪除表結構,表對象將不存在數據中;數據也不會存在;表內的對象也不存在,如:索引、視圖、約束;
truncate table user;
truncate都被當成DDL出來,truncate的做用就是刪除該表裏的所有數據,保留表結構。至關於DDL中的delete語句,可是truncate比delete語句的速度要快得多。可是truncate不能帶條件刪除指定數據,只會刪除全部的數據。若是刪除的表有外鍵,刪除的速度相似於delete。但新版本的MySQL中truncate的速度比delete速度快。


約束

MySQL中約束保存在information_schema數據庫的table_constraints中,能夠經過該表查詢約束信息;
約束主要完成對數據的檢驗,保證數據庫數據的完整性;若是有相互依賴數據,保證該數據不被刪除。

經常使用五類約束:
not null:非空約束,指定某列不爲空
unique: 惟一約束,指定某列和幾列組合的數據不能重複
primary key:主鍵約束,指定某列的數據不能重複、惟一
foreign key:外鍵,指定該列記錄屬於主表中的一條記錄,參照另外一條數據
check:檢查,指定一個表達式,用於檢驗指定數據
MySQL不支持check約束,但可使用check約束,而沒有任何效果;
 
根據約束數據列限制,約束可分爲:
單列約束:每一個約束只約束一列
多列約束:每一個約束約束多列數據

一、not null約束
非空約束用於確保當前列的值不爲空值,非空約束只能出如今表對象的列上。
Null類型特徵:
全部的類型的值均可以是null,包括int、float等數據類型
空字符串「」是不等於null,0也不等於null
create table temp(
        id int not null,
        name varchar(255) not null default ‘abc’,
        sex char null
)
上面的table加上了非空約束,也能夠用alter來修改或增長非空約束
增長非空約束
alter table temp
modify sex varchar(2) not null;
取消非空約束
alter table temp modify sex varchar(2) null;
取消非空約束,增長默認值
alter table temp modify sex varchar(2) default ‘abc’ null;

 二、 unique
惟一約束是指定table的列或列組合不能重複,保證數據的惟一性。雖然惟一約束不容許出現重複的值,可是能夠爲多個null
同一個表能夠有多個惟一約束,多個列組合的約束。在建立惟一約束的時候,若是不給惟一約束名稱,就默認和列名相同。
惟一約束不只能夠在一個表內建立,並且能夠同時多表建立組合惟一約束。
MySQL會給惟一約束的列上默認建立一個惟一索引;
create table temp (
        id int not null,
        name varchar(25),
        password varchar(16),
        --使用表級約束語法,
        constraint uk_name_pwd unique(name, password)
);
表示用戶名和密碼組合不能重複
添加惟一約束
alter table temp add unique(name, password);
alter table temp modify name varchar(25) unique;
刪除約束
alter table temp drop index name;
 
三、    primary key
主鍵約束至關於惟一約束+非空約束的組合,主鍵約束列不容許重複,也不容許出現空值;若是的多列組合的主鍵約束,
那麼這些列都不容許爲空值,而且組合的值不容許重複。
每一個表最多隻容許一個主鍵,創建主鍵約束能夠在列級別建立,也能夠在表級別上建立。MySQL的主鍵名老是PRIMARY,
當建立主鍵約束時,系統默認會在所在的列和列組合上創建對應的惟一索引。
列模式:
create table temp(
    /*主鍵約束*/
    id int primary key,
    name varchar(25)
);
 
create table temp2(
    id int not null,
    name varchar(25),
    pwd varchar(15),
    constraint pk_temp_id primary key(id)
);
 
組合模式:
create table temp2(
    id int not null,
    name varchar(25),
    pwd varchar(15),
    constraint pk_temp_id primary key(name, pwd)
);
 
alter刪除主鍵約束
alter table temp drop primary key;
 
alter添加主鍵
alter table temp add primary key(name, pwd);
 
alter修改列爲主鍵
alter table temp modify id int primary key;
 
設置主鍵自增
create table temp(
        id int auto_increment primary key,
        name varchar(20),
        pwd varchar(16)
);
auto_increment自增模式,設置自增後在插入數據的時候就不須要給該列插入值了。
 
四、    foreign key 約束
外鍵約束是保證一個或兩個表之間的參照完整性,外鍵是構建於一個表的兩個字段或是兩個表的兩個字段之間的參照關係。
也就是說從表的外鍵值必須在主表中能找到或者爲空。
當主表的記錄被從表參照時,主表的記錄將不容許刪除,若是要刪除數據,須要先刪除從表中依賴該記錄的數據,
而後才能夠刪除主表的數據。還有一種就是級聯刪除子表數據。
注意:外鍵約束的參照列,在主表中引用的只能是主鍵或惟一鍵約束的列,假定引用的主表列不是惟一的記錄,
那麼從表引用的數據就不肯定記錄的位置。同一個表能夠有多個外鍵約束。
建立外鍵約束:
主表
create table classes(
        id int auto_increment primary key,
        name varchar(20)
);
從表
create table student(
        id int auto_increment,
        name varchar(22),
        constraint pk_id primary key(id),
        classes_id int references classes(id)
);
 
一般先建主表,而後再建從表,這樣從表的參照引用的表才存在。
表級別建立外鍵約束:
create table student(
        id int auto_increment primary key,
        name varchar(25),
        classes_id int,
        foreign key(classes_id) references classes(id)
);
上面的建立外鍵的方法沒有指定約束名稱,系統會默認給外鍵約束分配外鍵約束名稱,命名爲student_ibfk_n,
其中student是表名,n是當前約束從1開始的整數。
 
指定約束名稱:
create table student(
        id int auto_increment primary key,
        name varchar(25),
        classes_id int,
        /*指定約束名稱*/
        constraint fk_classes_id foreign key(classes_id) references classes(id)
);
 
多列外鍵組合,必須用表級別約束語法:
create table classes(
        id int,
        name varchar(20),
        number int,
        primary key(name, number)
);
create table student(
        id int auto_increment primary key,
        name varchar(20),
        classes_name varchar(20),
        classes_number int,
        /*表級別聯合外鍵*/
        foreign key(classes_name, classes_number) references classes(name, number)
);
 
刪除外鍵約束:
alter table student drop foreign key student_ibfk_1;
alter table student drop foreign key fk_student_id;
 
增長外鍵約束
alter table student add foreign key(classes_name, classes_number) references classes(name, number);
 
自引用、自關聯(遞歸表、樹狀表)
create table tree(
        id int auto_increment primary key,
        name varchar(50),
        parent_id int,
        foreign key(parent_id) references tree(id)
);
 
級聯刪除:刪除主表的數據時,關聯的從表數據也刪除,則須要在創建外鍵約束的後面增長on delete cascade
或on delete set null,前者是級聯刪除,後者是將從表的關聯列的值設置爲null。
create table student(
        id int auto_increment primary key,
        name varchar(20),
        classes_name varchar(20),
        classes_number int,
        /*表級別聯合外鍵*/
        foreign key(classes_name, classes_number) references classes(name, number) on delete cascade
);
 
五、    check約束
MySQL可使用check約束,但check約束對數據驗證沒有任何做用。
create table temp(
        id int auto_increment,
        name varchar(20),
        age int,
        primary key(id),
/*check約束*/
check(age > 20)
);
上面check約束要求age必須大於0,但沒有任何做用。可是建立table的時候沒有任何錯誤或警告。

索引


索引是存放在模式(schema)中的一個數據庫對象,索引的做用就是提升對錶的檢索查詢速度,
索引是經過快速訪問的方法來進行快速定位數據,從而減小了對磁盤的讀寫操做。
索引是數據庫的一個對象,它不能獨立存在,必須對某個表對象進行依賴。
提示:索引保存在information_schema數據庫裏的STATISTICS表中。
 
建立索引方式:
自動:當表上定義主鍵約束、惟1、外鍵約束時,該表會被系統自動添加上索引。
手動:手動在相關表或列上增長索引,提升查詢速度。
 
刪除索引方式:
自動:當表對象被刪除時,該表上的索引自動被刪除
手動:手動刪除指定表對象的相關列上的索引
索引相似於書籍的目錄,能夠快速定位到相關的數據,一個表能夠有多個索引。
 
建立索引:
create index idx_temp_name on temp(name);
 
組合索引:
create index idx_temp_name$pwd on temp(name, pwd);
 
刪除索引:
drop index idx_temp_name on temp;
 

視圖


視圖就是一個表或多個表的查詢結果,它是一張虛擬的表,由於它並不能存儲數據。
視圖的做用、優勢:
限制對數據的訪問
讓複雜查詢變得簡單
提供數據的獨立性
能夠完成對相同數據的不一樣顯示
    
建立、修改視圖
create or replace view view_temp
as
    select name, age from temp;
一般咱們並不對視圖的數據作修改操做,由於視圖是一張虛擬的表,它並不存儲實際數據。若是想讓視圖不被修改,能夠用with check option來完成限制。
create or replace view view_temp
as
    select * from temp
with check option;
 
修改視圖:
alter view view_temp
as
    select id, name from temp;
 
刪除視圖:
drop view view_temp;
 
顯示建立語法:
show create view v_temp;


 DML語句

DML主要針對數據庫表對象的數據而言的,通常DML完成:
插入新數據
修改已添加的數據
刪除不須要的數據

一、    insert into 插入語句
insert into temp values(null, ‘jack’, 25);
主鍵自增能夠不插入,因此用null代替
指定列
insert into temp(name, age) values(‘jack’, 22);
在表面後面帶括號,括號中寫列名,values中寫指定列名的值便可。當省略列名就表示插入所有數據,
注意插入值的順序和列的順序須要保持一致。
Set方式插入,也能夠指定列
insert into temp set id = 7, name = 'jason';
MySQL中外鍵的table的外鍵引用列能夠插入數據能夠爲null,不參照主表的數據。
使用子查詢插入數據
insert into temp(name) select name from classes;
多行插入
insert into temp values(null, ‘jack’, 22), (null, ‘jackson’ 23);

二、    update 修改語句 (set 不能省略)
update主要完成對數據的修改操做,能夠修改一條或多條數據。修改多條或指定條件的數據,須要用where條件來完成。
修改全部數據
update temp set name = ‘jack2’;
全部的數據的name會被修改,若是修改多列用「,」分開
update temp set name = ‘jack’, age = 22;
修改指定條件的記錄須要用where
update temp set name = ‘jack’ where age > 22;
當數據時null 時 用is null 選擇
update temp set age=0 where age is null; 

三、    delete 刪除語句
刪除table中的數據,能夠刪除全部,帶條件能夠刪除指定的記錄。
刪除全部數據
delete from temp;
刪除指定條件數據
delete from temp where age > 20;

 select 查詢、function 函數

select查詢語句用得最普遍、功能也最豐富。能夠完成單條記錄、多條記錄、單表、多表、子查詢等。 一、    查詢某張表全部數據 select * from temp; *表明全部列,temp表明表名,不帶條件就查詢全部數據   二、    查詢指定列和條件的數據 select name, age from temp where age = 22; 查詢name和age這兩列,age 等於22的數據。   三、    對查詢的數據進行運算操做 select age + 2, age / 2, age – 2, age * 2 from temp where age – 2 > 22;   四、    concat函數,字符串鏈接  select concat(name, ‘-eco’) from temp; concat和null進行鏈接,會致使鏈接後的數據成爲null concat和( 之間不要有空格;不會影響數據庫裏的值,僅是返回的數據和指定的字符串鏈接了 五、    as 對列重命名 select name as ‘名稱’ from temp; as也能夠省略不寫,效果同樣 若是重命名的列名出現特殊字符,如「‘」單引號,那就須要用雙引號引在外面 select name as 「名’稱」 from temp;   六、    也能夠給table去別名 select t.name Name from temp as t;   七、    查詢常量 相似於SQL Server select 5 + 2; select concat('a', 'bbb');   八、    distinct 去掉重複數據 select distinct id from temp; 多列將是組合的重複數據 select distinct id, age from temp;   九、    where 條件查詢 大於>、大於等於>=、小於<、小於等於<=、等於=、不等於<> 均可以出如今where語句中 select * from t where a > 2 or a >= 3 or a < 5 or a <= 6 or a = 7 or a <> 0;   十、    and 而且 select * from temp where age > 20 and name = ‘jack’; 查詢名稱等於jack而且年齡大於20的   十一、    or 或者 知足一個便可 select * from tmep where name = ‘jack’ or name = ‘jackson’;   十二、    between v and v2 大於等於v且小於等於v2 select * form temp where age between 20 and 25;    1三、    in 查詢 能夠多個條件 相似於or select * from temp where id in (1, 2, 3); 查詢id在括號中出現的數據   1四、    like 模糊查詢 查詢name以j開頭的 select * from temp where name like ‘j%’;   查詢name包含k的 select * from temp where name like ‘%k%’;   escape轉義 select * from temp where name like ‘\_%’ escape ‘\’; 指定\爲轉義字符,上面的就能夠查詢name中包含「_」的數據   1五、    is null、is not null 查詢爲null的數據 select * from temp where name is null; 查詢不爲null的數據 select * from temp where name is not null;   1六、    not select * from temp where not (age > 20); 取小於等於20的數據 select * from temp where id not in(1, 2);   1七、    order by 排序,有desc、asc升序、降序 select * from temp order by id; 默認desc排序 select * from temp order by id asc; 多列組合 select * from temp order by id, age;
相關文章
相關標籤/搜索