• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Jiang's C++ Space

            創(chuàng)作,也是一種學(xué)習(xí)的過程。

               :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::

            20060407發(fā)表于blog.csdn.net20090424重新編輯】

            關(guān)系型數(shù)據(jù)庫模型由Codd博士在1970年提出
            SQL
            成為ANSI標(biāo)準(zhǔn)是在1986

            最基本查詢
            select * from table1 where name=
            Lincoln;
            select col1,col2 from table1;

            無重復(fù)查詢
            select distinct amount from checks;
            (distinct
            在一個語句中只能用一次,放在所有字段之前)

            MySQL
            的一些常用命令
            show databases; 顯示所有數(shù)據(jù)庫
            show tables;
            顯示當(dāng)前數(shù)據(jù)庫的表
            use db1;
            當(dāng)前數(shù)據(jù)庫切換到db1
            describe table1;
            顯示table1表的表結(jié)構(gòu)

            運(yùn)用表達(dá)式和別名
            select item, wholesale+0.15 from price;
            select item, (wholesale+0.15) retail from price;
            select * from friends where state<=
            la;
            select * from friends where firstname<>
            al;
            select * from parts where location like
            %back%;
            select * from friends where st like
            C_;
            select firstname||lastname entirename from friends; (mysql
            中不能用)
            (mysql
            可以用select concat(name1, ’ ’, name2) name from friends;)
            select lastname from vacation where years<=5 and leavetaken>20;
            select * from vacation where lastname not like
            B%;

            集合運(yùn)算,MySQL不一定能用
            select name from softball union select name from football;
            合集
            select name from softball union all select name from football;
            全合集
            select * from football intersect select * from softball;
            交集
            select * from football minus select * from softball;
            差集

            In
            Between運(yùn)算符
            select * from friends where areacode in(100,381,204);
            select * from price where wholesale between 0.25 and 0.75;

            內(nèi)置函數(shù)的使用
            count 
            求總數(shù)
            sum
            求總和
            avg
            求平均
            max
            求最大
            min
            求最小
            variance  
            標(biāo)準(zhǔn)方差
            stddev
            標(biāo)準(zhǔn)差
            add_month 
            給日期類型增加一月
            add_date  
            給日期類型增加一天
            last_day  
            返回月份的最后一天
            month_between
            日期相差的月份
            sysdate   
            系統(tǒng)時間
            abs
            絕對值
            ceil  
            “天花板”
            floor 
            “地板”
            exp
            指數(shù)函數(shù)
            log
            上面的反函數(shù)
            pow a^b
            sign  
            10負(fù)-1
            sqrt  
            根號
            chr
            轉(zhuǎn)換成字符
            concat
            連接字符串
            initcap   
            首字符大寫,其他小寫
            lower 
            小寫
            upper 
            大寫
            lpad  
            左填充,原始,長度,字符
            rpan  
            右填充
            ltrim 
            左剪切
            rtrim 
            右剪切
            trim  
            剪切
            replace   
            字符替換,原始,替換,替換成
            substr
            獲取子字符串,原始,起始,長度
            instr 
            查找字符串,原始,查找,開始,序號
            length
            字符串長度
            to_char   
            轉(zhuǎn)換成字符串
            to_number 
            轉(zhuǎn)換成數(shù)字

            子句的運(yùn)用
            (Where, Group by, Order by, Having) 
            select * from checks where amount>100;
            select * from checks order by check;
            可以加上asc代表升序
            select * from checks order by payee desc;
            select * from checks order by remarks, payee;
            select * from checks order by 1;
            select payee, sum(amount), count(payee) from checks group by payee;
            select sum(amount), count(payee) from checks group by payee, remarks;
            where
            之句中不允許用合計函數(shù),因此下面的語句是錯誤的。
            select team, avg(salary) from orgchart where avg(salary)<3800 group by team;
            這時候需要having子句:
            select team, avg(salary) from orgchart group by team having avg(salary)<3800;
            …… having team in(pr,research);

            交叉連接(笛卡爾積)
            select * from table1, table2;
            select o.orderedon, o.name, o.partnum, p.partnum, p.description
                from orders o, part p
                where o.partnum=p.partnum;

            內(nèi)部連接
            select p.partnum, p.price, o.name, o.partnum
                from part p inner join orders o on orders.partnum=54;
            除了使用了“on”來代替“where”之外,和交叉連接沒多少區(qū)別。
            外部連接

            左連接:左邊的表全部顯示
            select p.partnum, p.description, p.price, o.name, o.partnum
                from part p left outer join orders o on o.partnum=54;

            右連接:右邊的表全部顯示
            select p.partnum, p.description, p.price, o.name, o.partnum
                from part p right outer join orders o on o.partnum=54;

            子查詢(MySQL不支持)
            select * from orders
            where partnum=(select partnum from part where description like
            ROAD%);

            下面是較復(fù)雜查詢示例:
            select o.name, o.orderedon, o.quantity * p.price total
                from orders o, part p
                where o.partnum=p.partnum and o.quantity * p.price > 
                (select avg(o.quantity * p.price)
                    from orders o, part p
                    where o.partnum=p.partnum);

            Exist
            使用(判斷集合是否存在)
            select name, orderedon from orders where exists
                (select * from orders where name=
            Mostly harmless)


            數(shù)據(jù)庫三范式
            1
            、數(shù)據(jù)集合分成多張表而不只是一張大表,分成的每張表都有主鍵;主鍵
            2、找出僅僅依賴于主鍵的列,將其存儲在另一個表中;僅依賴的列抽出
            3、從一個表中刪除不依賴于主鍵的列。不依賴的列抽出
            缺點(diǎn):降低性能。

            建立數(shù)據(jù)庫
            很難吧?很難!不難吧?不難。僅僅告訴你最簡單情況:
            create database PAYMENTS;

            建立表
            create table bills(name char(30), amount number, account_id number);
            create table empname(id number not null, ename char(30));
            create table newtable as select * from oldtable;

            改變表:增加列
            alter table emp add new_col_name char(20);

            改變表:修改列
            alter table emp modify new_col_name char(21);

            改變表:改列名(Oracle 9i Release 2才能用)
            alter table emp rename column new_col_name to old_col_name;

            改變表:刪除列
            alter table emp drop column old_col_name;

            刪除表
            drop table tablename;

            刪除數(shù)據(jù)庫
            drop database databasename;

            主鍵、非空、唯一約束
            主鍵primary key
            非空not null
            唯一unique 除了排序,和primary key功能一致
            create table emp
                (emp_id char(9) primary key,
                emp_name varchar2(40) not null,
                phone number(10) null unique);

            外鍵約束foreign key

            create table emp_pay
                (emp_id char(9) not null,
                position varchar2(15) not null,
                pay_rate number(4,2) not null);
            alter table emp_pay add constraint emp_id_fk foreign key(emp_id) 
            references emp(emp_id);

            校驗(yàn)約束check
            create table emp
                (emp_id char(9) not null primary key,
                emp_name varchar2(40) not null,
                emp_rate number(4,2) not null,
                zip number(5) not null);
            alter table emp add constraint chk_zip check(emp_zip = '46234');
            alter table emp add constraint chk_zip check(emp_zip in ('47634', '13451'));
            alter table emp add constraint chk_zip check(emp_rate < 12.5);
            emp_rate
            為校驗(yàn)約束名。

            刪除約束
            alter table emp drop constraint emp_no_constraint;
            emp_no_constraint
            為約束名。

            更新記錄
            update collection set worth = 555, price = 666 where itemid = 1110;

            刪除記錄
            delete from collection where itemid = 1113;

            事務(wù)處理
            ……
            commit;
            ……
            rollback;
            ……
            savepoint save_it;
            ……
            rollback to savepoint save_it;

            日期時間
            掌握兩個函數(shù)就可以了。
            to_char(empdate, 'YYYY/MM/DD HH24:MI:SS');
            date轉(zhuǎn)變成字符串:“2004/11/12 16:30:02
            to_date('1981/11/12 00:03:16', 'YYYY/MM/DD HH24:MI:SS');
            將字符串“1981/11/12 00:03:16”轉(zhuǎn)變成日期類型。
            還有要注意的事項(xiàng),在中文Oracle中,'26-JAN-03'并不被認(rèn)為是合法的日期,
            '26-1
            -03'才是合法的,真別扭,也蠻郁悶的。

            建立視圖
            create view debts as select * from bills;

            刪除視圖
            drop view debts;

            建立索引
            create index empno_index on emp(empno);

            刪除索引
            drop index empno_index;

            創(chuàng)建用戶
            create user jguogang identified by mypassword;

            授予用戶角色
            grant connect to jguogang;
            grant resource to jguogang;
            grant dba to jguogang;

            刪除用戶的角色
            revoke resource from jguogang;

            給予系統(tǒng)特權(quán)
            grant alter any type to public;
            其中“alter any type”為一種系統(tǒng)特權(quán),將被授予全部用戶。
            grant drop any trigger to jguogang;
            將系統(tǒng)特權(quán)“drop any trigger”授予用戶“jguogang”。
            grant create user to connect;
            將系統(tǒng)特權(quán)“create user”授予角色“connect”。
            收回系統(tǒng)特權(quán)
            revoke alter any type from public;
            給予對象特權(quán)
            grant select on emp to jack;
            授予用戶“jack”:對“emp”的“select”對象特權(quán)。
            grant select, update(salary) on emp to jill;
            授予用戶“jill”:對“emp”的“select”、“salary”列的“update”對象特權(quán)。
            收回對象特權(quán)
            revoke select on emp from jack;

            字典
            字典:我是誰
            select * from user_users;
            字典:我們是誰
            select * from all_users;
            字典:我能干什么
            select * from user_sys_privs;
            字典:我充當(dāng)什么角色
            select * from user_role_privs;
            字典:我擁有什么表、視圖和“SEQUENCE
            select * from user_catalog;
            字典:我可訪問什么表、視圖和“SEQUENCE”(多)
            select * from all_catalog;
            字典:我擁有什么對象
            select * from user_object;
            字典:我可訪問什么對象(多)
            select * from all_catalog;
            ……關(guān)于字典,暫時就介紹那么點(diǎn)了。

            注釋
            表的注釋
            comment on table emp is 'Employee';
            列的注釋
            comment on column emp.ename is '
            名字';
            列出所有包含注釋的表
            select * from user_tab_comments where comments is not null;
            系統(tǒng)時間取得
            select sysdate from dual;

            20050906
            筆記
            查看未提交的事務(wù)
            select * from v$transaction;
            select * from v$locked_object;
            通過locked_object視圖獲知被鎖定的對象的IDobject_id
            那么通過下面的語句能獲知到底哪個對象被鎖定
            select * from sys.all_objects t where object_id = ###;
            alter system kill session 30;

            修改用戶密碼相關(guān)
            select username,password from dba_users;
            alter user aaa identified by aaaspwd;

            posted on 2009-04-24 15:52 Jiang Guogang 閱讀(563) 評論(0)  編輯 收藏 引用 所屬分類: Knowledge
            天天综合久久久网| 久久丫精品国产亚洲av| 午夜精品久久久久久影视777 | 国产精品久久久久乳精品爆 | 国产亚洲美女精品久久久| 国产成人久久777777| 久久受www免费人成_看片中文| 精品久久久久久中文字幕大豆网| 久久亚洲国产成人精品性色| 久久综合九色综合97_久久久 | 青青草原精品99久久精品66| 国产伊人久久| 狠狠色丁香久久婷婷综合五月| 精品久久久久久无码国产| 久久人爽人人爽人人片AV| 亚洲第一永久AV网站久久精品男人的天堂AV | 久久99精品久久久久久| 久久香综合精品久久伊人| 久久夜色tv网站| 久久国产色AV免费看| 狠狠色狠狠色综合久久| 久久精品国产欧美日韩99热| 久久精品视屏| 热久久国产欧美一区二区精品 | 免费无码国产欧美久久18| 精品久久久久久无码中文野结衣 | 久久99九九国产免费看小说| 欧美粉嫩小泬久久久久久久 | 日韩一区二区三区视频久久| 女人香蕉久久**毛片精品| 久久国产乱子伦精品免费强| 97r久久精品国产99国产精| 精品久久久久久| 亚洲午夜精品久久久久久人妖| 99麻豆久久久国产精品免费 | 91精品国产综合久久久久久| 亚洲欧洲久久久精品| 亚洲中文字幕伊人久久无码| 久久国产综合精品五月天| 久久久久女教师免费一区| 成人综合久久精品色婷婷|