數據庫 MySQL語句
數據庫的基本操作:
-- 增、刪、改、查
-- 數據庫的存儲方式:表格
-- 數據庫的基本操作語句:
-- 啟動數據庫服務
net start mysql
-- 關閉數據庫服務
net stop mysql
-- 通過windows命令窗口連接數據庫
mysql -uroot -p123456
-- 查看數據庫
show databases
-- 創建數據庫
create database value
-- 刪除數據庫
drop database value
-- 查看創建數據庫
show create database value
-- 進入數據庫
use value
-- 使用資源文件
source c:/mysql.sql
-- 查看數據庫已有表
show tables
-- 指定數據庫查看表
show tables from databaseVaule
-- 查看表結構
desc tablesValue
-- 查看建表語句
show create table tablesValue
-- 創建表
create table tablesValue(
value int(numb) primary key auto_increment,
value double not null,
value varchar(numb),
value date,
value char unique
)
-- 添加數據
insert into tablesValue (value,value,value) values(XXX,XXX,XXX);
insert into tablesValue values(XXX,XXX,XXX,XXX,XXX);
/* insert into students (id,name,sno,birthday) values(1,"hnn","144215",0820)
insert into students values(2,"zhangsan",144216,0624); */
-- 查看表信息
select * from tablesValue
select value,value from tablesValue
-- 有條件查詢
select * from tablesValue where value=values
/*
select id,name,sno from students where sno = 144215
*/
-- 局限性查詢
select value,value from tablesValue
-- 局限性有條件查詢
select value,value from tablesValue where value=values
-- 多條件使用字符
and or xor not
/* select id,name,sno from students where sno = 144215
select id,name,sno from students where sno = 144215 and id = 1
select id,name,sno from students where sno = 144215 xor id = 1
select id,name,sno from students where sno = 144215 or sno = 144216
select id,name,sno from students where not sno = 144215 and not id = 2
*/
-- 清除重復記錄
select distinct value,value from tablesValue
/* select distinct sno from students
select sno from students */
-- 按照規定排序
select * from tablesValue order by value asc/desc
/* select * from students order by id desc
*/
-- 按照指定值查詢
select * from tablesValue where value in(XXXX,XXXX,XXXX)
/* select * from students where id in(1,2)
*/
-- 指定包含第幾條開始查詢多少條信息
select * from tablesValue limit x,y
/* select * from students limit 0,3
*/
-- 聚合函數
min() max() sum() avg() count()
對數據庫中數字類型的字段取最大值可以直接用:
SELECT MAX(field-name) FROM table-name WHERE conditions
而對於其它類型的字段要使用以下語句:
SELECT MAX(CAST(field-name AS UNSIGNED)) FROM table-name WHERE conditions
/* select count(*) from students
select count(id) from students
select max(sno) from students
select sum(id) from students
select avg(sno) from students */
-- 過濾聚合值的記錄行
* select * from tablesValue order by value asc/desc having sum(value)>100
/* select * from students group by id having sum(id)>1
select *,avg(sno),avg(id) from students group by sno having avg(sno)>2
http://www.cnblogs.com/yank/p/3672478.html */
-- 連接多個查詢結果
select.... union select ...
/* select sum(id) as sumset from students union select avg(sno) from students
*/
-- 對當前表添加一列
alter table tablesValue add column value varchar(12) not null
/* alter table students add column sname varchar(12) not null
*/
-- 刪除當前表中的某列
alter table tablesValue drop column value
/* alter table students drop column sname */
-- 修改當前列的定義
alter table tablesValue modify value varchar(11) not null
/* alter table students modify sname int(11) not null
列表不能為空 */
-- 修改列的定義和名字
alter table tablesValue change oldvalue newvalue char(10) not null
/* alter table students change id idd char(10) not null
*/
-- 添加主鍵
alter table tablesValue add primary key(id)
-- 刪除主鍵
alter table tablesValue drop primary key
-- 改變表名
alter table tablesValue rename tablesValue
rename table tablesValue to tablesValue
/* alter table students rename studenta
rename table studenta to students */
-- 刪除表
drop table tablesValue
-- 更新表數據
update tablesValue set value=XXXX
update tablesValue set value=XXXX,value=XXXX where value=XXXX
-- 刪除表數據
delete from tablesValue
delete from tablesValue where value=XXXX
-- 兩個表之間的關聯
select * from tablesValue,tablesValue
select * from tablesValue t1,tablesValue t2
select t1.value,t2.value from tablesValue t1,tablesValue t2
select t1.value,t2.value from tablesValue as t1,tablesValue as t2
-- 內連接
select * from tablesValue t1 join tablesValue t2 on t2.value = t1.value where t1.value = 10000
select * from tablesValue t1 inner join tablesValue t2 on t2.value = t1.value where t1.value = 10000
-- 外連接 左右無區別
select * from tablesValue t1 left outer join tablesValue t2 on t2.value = t1.value where t1.value = 10000
select * from tablesValue t1 right outer join tablesValue t2 on t2.value = t1.value where t1.value = 10000
-- 創建表外鍵
create table tablesValue(
value int(numb) primary key auto_increment,
value double not null,
value varchar(numb),
value date,
value char unique,
foreign key (value) references tablesValue(value)
)
alter table tablesValue add foreign key (value) references tablesValue(value)
alter table student add foreign key (Tno) references teacher(Tno) //後面的表的屬性是主鍵,前面的表的屬性不是主鍵。屬性的類型的長度必須一樣
-- 事務控制語句
start transaction
select * from tablesValue where value=1
delete from tablesValue where value=1
select * from tablesValue where value=1
rollback //否認上面四句對數據庫的更改,數據庫回到它未執行這四條語句前
select * from tablesValue where value=1
start transaction
select * from tablesValue where value=1
delete from tablesValue where value=1
select * from tablesValue where value=1
commit //同意上面四句對數據庫的更改
select * from tablesValue where value=1
-- 控制自動提交
set autocommit = off
set autocommit = on
set session autocommit = off
set session autocommit = on
-- 創建視圖
create view tablesValue as select value,value from tablesValue
select * from tablesValue
create view tablesValue (value,value) as select value,value from tablesValue
-- 修改視圖
alter view tablesValue (value,value) as select value,value from tablesValue
-- 刪除視圖
drop view tablesValue
-- 展現創建視圖
show create view tablesValue
數據庫 MySQL語句