create table student(
stuid char(10),
name varchar2(20),
stuname varchar2(20),
stuage number);
-- 一、建立用戶
create user shaoxin identified by a123456;
-- 爲用戶受權
-- 二、連接權限
grant connect to shaoxin;
-- 操做權限
grant create to shaoxin;
grant alter to shaoxin;
-- 三、總的操做權限
grant resource to shaoxin;
-- 四、登陸數據庫
conn shaoxin/a123456@orcl;
-- 數據庫實例(一個數據庫就是一個數據庫實例)
-- 數據庫方案(一個用戶就是一個數據庫方案)
-- 五、刪除用戶
drop user xiaozhang;
-- 管理員建立指定普通用戶下的表
create table shaoxin.utab(
username varchar2(20),
pwd varchar2(20)
);
-- 插入表數據
insert into utab values('sx','123');
-- 練習
-- 建立迷你版圖書館裏系統的圖書表
create table bookuser(
id number(3);
username varchar2(20),
password varchar2(20)
);
create table book(
bookid char(10),
bookname varchar2(20),
statu number(1),
takebook date,
takecount number(2)
);
-- 顯示錶信息 desc tablename
-- 日期類型 date 關鍵字to_date('實際參數','數據格式');其中分鐘的關鍵字變成了mi
-- 增長字段 alter table tablename add (columnname 字段類型)
-- 修改字段的長度 alter table tablename modify(columnname 新的字段長度)
-- 修改字段類型的時候(要保證不能有數據)alter table tablename modify(column 新的字段類型);
-- 刪除一個字段 alter tabel tablename drop column 要刪除的字段名;
-- 修改表名 rename oldtablename to newtablename;
-- 刪除表 drop table tablename;數據庫
-- 約束
-- not null(不能爲空) ; unique (惟一,能夠爲空); primary key (主鍵);check ; foreign;
-- 一、建表的同時添加約束,
-- not null
-- check (column in ('',''))枚舉類型,只能插入舉例出的數據
-- check (column in (number <= column and number>=column))第二種檢查方式,數據必須在指定範圍內
-- 默認值 default value;
-- alter table customer modify address default '南昌';ide
-- 雙主鍵primay key (column1,column2)函數
-- 外鍵約束 foreign
-- 方式一: references tablename(column)
-- 方式二: alter table student add constraint s_t_fk foreign key(stuid) references teacher(tid);ui
-- 主鍵約束 alter table student add constraint s primary key (sid)
-- 惟一性約束 alter table student add constraint sn unique(stuname)
-- 檢查約束 alter table student add constraint ss check(stusalary >= 3000)排序
-- 非空約束 alter table student modify age not null;ip
--test
-- 商品
create table goods(
goodsId number primary key,
goodsName varchar2(20),
unitprice number check(unitprice > 0),
gcategory number(2),
provider varchar2(20)
);
-- 客戶
create table customer(
custId number primary key,
name varchar2(20) not null,
address varchar2(20),
email varchar2(20) unique,
sex varchar2(20) default '男' check(sex='男' or sex='女'),
cardId number(12)
);
--購買
create table purchase(
custId number references customer(custId),
goodsId number references goods(goodsId),
nums number check(nums>=1 and nums<=30),
primary key(custId,goodsId)
);
//換種添加主鍵及外鍵約束的方式
create table purchase(
custId number ,
goodsId number ,
nums number check(nums>=1 and nums<=30)
);
alter table purchase add constraint pk primary key(custId,goodsId);
alter table purchase add constraint purchase_customer_fk foreign key(custId) references customer(custId);
alter table purchase add constraint purchase_goods_fk foreign key(goodsId) references goods(goodsId);ci
-- homework
-- 寵物表
create table category(
catId varchar2(10) primary key,
name varchar2(80) not null,
descn varchar2(255) not null
);
insert into category(catId,name,descn) values('1','小貓','山地野貓');
insert into category(catId,name,descn) values('2','中華田園犬','中國特有的土狗');
insert into category(catId,name,descn) values('3','豬','專門用於生產精華火腿的豬');
-- 產品表
create table product(
productId varchar2(10) primary key,
category varchar2(10) not null,
name varchar2(80) not null,
descn varchar2(255) not null
);
alter table product add constraint product_category_fk foreign key(category) references category(catId);
insert into product(productId,category,name,descn) values('1','1','貓類','山地野貓,生命長度大概十年');
insert into product(productId,category,name,descn) values('2','2','狗類','中華田園犬,中國特有的土狗');
insert into product(productId,category,name,descn) values('3','3','豬類','豬,專門用於生產精華火腿的豬');
insert into product(productId,category,name,descn) values('4','2','豬類','豬,專門用於生產精華火腿的豬');
insert into product(productId,category,name,descn) values('5','1','貓類','山地野貓,生命長度大概十年');
insert into product(productId,category,name,descn) values('6','2','狗類','中華田園犬,中國特有的土狗');
insert into product(productId,category,name,descn) values('7','3','豬類','豬,專門用於生產精華火腿的豬');
insert into product(productId,category,name,descn) values('8','2','豬類','豬,專門用於生產精華火腿的豬');
insert into product(productId,category,name,descn) values('9','2','豬類','豬,專門用於生產精華火腿的豬');
insert into product(productId,category,name,descn) values('10','1','貓類','山地野貓,生命長度大概十年');
insert into product(productId,category,name,descn) values('11','2','狗類','中華田園犬,中國特有的土狗');
insert into product(productId,category,name,descn) values('12','3','豬類','豬,專門用於生產精華火腿的豬');
insert into product(productId,category,name,descn) values('13','2','豬類','豬,專門用於生產精華火腿的豬');
-- 產品明細表
create table item(
itemId varchar2(10) primary key,
productId varchar2(10) not null,
listprice number(10,2) not null,
unitcost number(10,2) not null,
supplier integer not null,
status varchar2(2) not null,
attr1 varchar2(80) not null,
attr2 varchar2(80) not null,
attr3 varchar2(80) not null,
attr4 varchar2(80) not null,
attr5 varchar2(80) not null
);
alter table item add constraint item_product_fk foreign key(productId) references product(productId);字符串
alter table item add constraint item_supplier_fk foreign key(supplier) references supplier(suppid);
alter table item add constraint item_inventory_fk foreign key(itemId) references inventory(itemid);
alter table item add constraint uniq unique(supplier);字符串處理
insert into item(itemId,productid,listprice,unitcost,supplier,status,attr1,attr2,attr3,attr4,attr5) values(
'1','1','1200.67','1200',0001,'0','0','0','0','0','0');
insert into item(itemId,productid,listprice,unitcost,supplier,status,attr1,attr2,attr3,attr4,attr5) values(
'2','2','1524.33','1524',0002,'0','0','0','0','0','0');
insert into item(itemId,productid,listprice,unitcost,supplier,status,attr1,attr2,attr3,attr4,attr5) values(
'3','3','2131.67','2131',0003,'0','0','0','0','0','0');
-- 供應商信息表
create table supplier(
suppid integer primary key,
name varchar2(80) not null,
status varchar2(2) not null,
addr1 varchar2(80) not null,
addr2 varchar2(80) not null,
city varchar2(80) not null,
state varchar2(80) not null,
zip varchar2(5) not null,
phone varchar2(80) not null
);
insert into supplier values(1,'長城',1,'中國','江西','南昌',1,1,'18679654629');
insert into supplier values(2,'長城',1,'中國','江西','南昌',1,1,'18679654629');
insert into supplier values(3,'長城',1,'中國','江西','南昌',1,1,'18679654629');
-- 庫存表
create table inventory(
itemid varchar2(10) not null,
qty integer not null
);
insert into inventory values(1,0001);
insert into inventory values(2,0002);
insert into inventory values(3,0003);
alter table inventory add constraint ipk primary key(itemid);qt
-- 查詢
select * from category;
select * from product;
select * from item;
select * from supplier;
select * from inventory;
-- 列車車次查詢
create table lieche(
startcity varchar2(20),
endcity varchar2(20),
checi varchar2(20)
);
insert into lieche values('上海','北京','K001');
insert into lieche values('上海','南昌','G001');
insert into lieche values('上海','昆明','S001');
insert into lieche values('上海','南京','K032');
insert into lieche values('上海','甘肅','SE01');
insert into lieche values('南昌','北京','G201');
insert into lieche values('崑山','北京','G401');
insert into lieche values('杭州','北京','SG01');
insert into lieche values('深圳','北京','F013');
insert into lieche values('贛州','北京','G021');
-- 對於****模糊查詢單****的時候like是第一選擇,而當要插入字段的時候由於存在null要麼選擇回滾要麼選擇拼接字符串
select * from lieche where startcity like '%%' and endcity like'%%' and checi like'%%';
select li2.* from (select rownum rn,li.* from lieche li where rownum<=3*pageNo) li2 where rn>(pageNo-1)*3;
select li2.* from (select rownum rn,li.* from lieche li where rownum<=3*2) li2 where rn>(2-1)*3;
-- 經常使用函數:
-- 字符串處理函數lower轉爲小寫upper轉爲大寫
select lower(checi) from lieche;
-- 數字函數round(value,p) 四捨五入
-- trunc(value,p)首位p位數字
-- 日期函數to_date(sysdate),to_date(日期型數據,格式)其中sysdate是系統的當前時間可直接插入數據類型爲date
-- to_char(sysdate,格式)將日期型轉換爲字符串,這個能夠顯示完整數據,定義的時候數據類型爲char(20)類型
-- to_timestamp(日期型數據,格式)時間戳
-- 鏈接查詢
-- 內鏈接查詢
-- 一、等值鏈接
select item.listprice 產品價格,product.name 產品名稱,item.unitcost 標準價格 from
item,product,category where item.productid=product.productid and product.productid=category.catid;
--改爲等值鏈接:
select item.listprice 產品價格,product.name 產品名稱,item.unitcost 標準價格 from
item join product on item.productid=product.productid join category on product.productid=category.catid;
-- 二、非等值鏈接
-- 三、自鏈接
select i1.listprice 產品價格 from item i1 join item i2 on i1.listprice > i2.listprice and i2.itemid =1s
-- 四、左鏈接
select item.listprice 產品價格,product.name 產品名稱,item.unitcost 標準價格 from
item left join product on item.productid=product.productid ;
-- 五、右鏈接
select item.listprice 產品價格,product.name 產品名稱,item.unitcost 標準價格 from
item right join product on item.productid=product.productid ;
-- 分組查詢
-- count
select count(*) from item;
-- max
select max(listprice) from item;
-- min
select min(listprice) from item;
-- avg
select avg(listprice) from item;
-- sum
select sum(listprice) from item;
-- group by
-- 帶有group by的查詢中select 列表中指定的列要麼是group by 子句中指定的列,要麼包含聚合函數
-- 前十條數據中分組後的商品數量的在3條以上的分組狀況;
-- 分組以前要用where 分組以後要用having 而最後排序
select name,count(*) from product where rownum<=10 group by name having count(*)>=3 order by count(*) ;
-- select pro2.* from (select pro.name,count(*) from product pro where rownum<=10) pro2 group by name having count(*)>=3 order by count(*);
-- 課堂練習-- 主表myproductcreate table myproduct (productid number primary key,productname varchar2(20) unique,price number not null);insert into myproduct values(1,'HP1200打印機',2000);insert into myproduct values(2,'LX360兼容機',4800);insert into myproduct values(3,'IBM 350筆記本',11000);insert into myproduct values(4,'BM 360筆記本',12000);-- 字表 salescreate table sales(productid number references myproduct(productid),clientname varchar2(20),productnumber number,salesprice number);--一、 爲表添加主鍵alter table sales add sid number primary key;--二、 添加數據insert into sales values(2,'華亞諮詢',10,4500,1);insert into sales values(1,'華亞諮詢',25,1800,2);insert into sales values(3,'聯想集團',10,11000,3);insert into sales values(2,'聯想集團',30,4500,4);insert into sales values(1,'聯想集團',20,1800,5);insert into sales values(3,'北大方正',40,10000,6);insert into sales values(3,'諾基亞',20,10500,7);-- 三、查詢單筆數量大於15的客戶信息select clientname 客戶名稱,productnumber 購買數量,salesprice 銷售價格信息 from sales where productnumber>15;-- 四、查詢全部商品的所有銷售金額select productname,sum(salesprice * productnumber) from myproduct mpro,sales sa where mpro.productid=sa.productid group by mpro.productname;-- 只關注總金額就不須要關聯select sum(salesprice * productnumber) from sales sa group by sa.productid;--五、查詢客戶姓名、對應的客戶銷售金額select clientname,sum(salesprice*productnumber) from sales group by clientname ;--六、查詢產品價格大於"LX360兼容機"的全部產品信息select * from myproduct mpro where mpro.price > (select price from myproduct where productname='LX360兼容機' );--七、 查詢購買過商品"IBM 350筆記本"的商品名稱、客戶名稱、購買數量select productname,clientname,productnumber from myproduct mpro,sales sa where mpro.productid = (select productid from myproduct where productname='IBM 350筆記本') and mpro.productid=sa.productid ;-- 八、把客戶「華亞諮詢」名稱更改成新致軟件update sales set clientname='新致軟件' where clientname='華亞諮詢';