摘自:http://www.cnblogs.com/hoojo/archive/2011/06/20/2085390.html html
cmd 命令行輸入(若是沒配置環境變量要指定到具體目錄)
mysql -uroot -p (回車後輸入密碼,便可進入mysql) mysql
MySQL的結束符是「;」 sql
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速度快。
經常使用五類約束:
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的時候沒有任何錯誤或警告。
一、 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;