資料庫 MySQL語句

來源:互聯網
上載者:User

標籤:incr   列表   delete   更改   加一列   ase   外鍵   自動   ima   

 資料庫的基本操作:
--     增、刪、改、查

-- 資料庫的儲存方式:表格

-- 資料庫的基本動作陳述式:
--     啟動資料庫服務
     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語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.