国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

數(shù)據(jù)庫(kù)SQL語(yǔ)言語(yǔ)法總結(jié)1---表操作

2019-11-08 20:53:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

一:創(chuàng)建表結(jié)構(gòu)

create table <表名> (<列名><數(shù)據(jù)類型>[列級(jí)完整性約束條件][,<列名><數(shù)據(jù)類型>[列級(jí)完整性約束條件]]….[,<表級(jí)完整性約束條件>])

其中約束條件包括not null —–非空unique—–唯一(當(dāng)一個(gè)表的其中一個(gè)屬性被設(shè)置為unique時(shí),插入兩個(gè)此屬性相同的記錄時(shí),第二個(gè)插入操作會(huì)被拒絕,以此來(lái)保證此屬性列在各記錄上的分量上唯一),PRimary key——–將一個(gè)屬性列設(shè)置為主碼primary key等同于 not null + unique

下面結(jié)合幾個(gè)例子幫助各位看官深入理解一下創(chuàng)建過(guò)程: 例1:創(chuàng)建一個(gè)customer表,包含customer _name(存儲(chǔ)字長(zhǎng)為20個(gè)char長(zhǎng)度),customer _street(存儲(chǔ)字長(zhǎng)為30個(gè)char長(zhǎng)度),customer _city(存儲(chǔ)字長(zhǎng)為30個(gè)char長(zhǎng)度)這三個(gè)屬性列, 其中customer _name為非空,customer _street必須是唯一的,且規(guī)定customer _name是這個(gè)表的主碼 create table customer( customer_name char(20) not null customer_street char(30) unique customer_city char(30), primary key (customer _name));

例2:創(chuàng)建一個(gè)branch表,包括branch_name(存儲(chǔ)字長(zhǎng)為15個(gè)char長(zhǎng)度且為非空),branch _city(存儲(chǔ)字長(zhǎng)為30個(gè)char長(zhǎng)度), assets(小數(shù))三個(gè)屬性列,其中branch _name是主碼 create table branch( branch_name char(15) not null, branch_city char(30), assets decimal, primary key(branch _name), check(assets >=0);) 最后一行語(yǔ)句是檢查約束語(yǔ)句,檢查assets>=0。當(dāng)對(duì)數(shù)據(jù)庫(kù)執(zhí)行插入操作時(shí),如assets這一列輸入的值<0則會(huì)被數(shù)據(jù)庫(kù)報(bào)錯(cuò)以此來(lái)保證assets>=0

例3:創(chuàng)建一個(gè)account表,其中包括account_number(存儲(chǔ)字長(zhǎng)為10個(gè)char長(zhǎng)度且非空且為主碼),branch _name(存儲(chǔ)字長(zhǎng)為15個(gè)char長(zhǎng)度且為外碼,此屬性列在branch表中為主碼),balance(存儲(chǔ)為int且>=0)三個(gè)屬性列 create table account( account_number char(10) not null, branch_name char(15), balance int, primary key(account_number), foreign key(branch_name), reference branch(branch_name), check(balance>=0));

例4:創(chuàng)建一個(gè)deposit表,包括customer_name(存儲(chǔ)字長(zhǎng)為20個(gè)char長(zhǎng)度且非空且為外碼,參照于customer表中的customer _name屬性列),account _number(存儲(chǔ)字長(zhǎng)為10個(gè)char長(zhǎng)度且非空且為外碼,參照于account表中的account _number屬性列)這兩個(gè)屬性列,其中customer _name和account _number共同作為deposit表的主碼 create table deposit( customer_name char(20) not null, account_number char(10) not null, primary key(customer_name,account _number), foreign key(customer_name) reference customer (customer _name), foreign key(account _number) reference account(account _number));

二:更新表結(jié)構(gòu)

alter table <表名> [add <新列名><數(shù)據(jù)類型>[列級(jí)完整性約束條件]] [drop <列名><完整性約束條件>] [modify <列名><數(shù)據(jù)類型>];

舉例: 如數(shù)據(jù)庫(kù)中覺(jué)得Sage使用int來(lái)存儲(chǔ)太浪費(fèi)了,需要把存儲(chǔ)字長(zhǎng)改小一點(diǎn):alter table Student modify Sage,small int 如需要?jiǎng)h除Sname這一屬性:alter table Student drop Sname 如需要增加Scome Date 這一屬性:alter table Student add Scome Date

三:整表刪除

drop table <表名> 如:刪除Student表 :drop table Studnent


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 文化| 南丹县| 深圳市| 应城市| 阿坝县| 伊川县| 厦门市| 如东县| 桂东县| 五家渠市| 茌平县| 云和县| 桂阳县| 乐陵市| 成安县| 盈江县| 双峰县| 湟源县| 柘城县| 凤庆县| 固始县| 茶陵县| 仁化县| 涪陵区| 周至县| 开原市| 中宁县| 淮安市| 金华市| 运城市| 英山县| 昭苏县| 广汉市| 榆树市| 明星| 成都市| 卢湾区| 浠水县| 巴东县| 浠水县| 锡林浩特市|