SQL語句的基本操作

來源:互聯網
上載者:User

--建立資料庫
create database Etp;


--串連資料庫
connect to Etp;


--中斷連線
disconnect Etp;


--查看當前資料庫下有哪些表
list tables;


--建表
create table studentInfo(
 stuno char(5) not null,
 stuname varchar(8),
 stubirth date
);


--查看錶結構
describe table studentinfo;


--新增表欄位
alter table studentinfo add stutel int;
alter table studentinfo add abc int;


--修改欄位類型
alter table studentinfo alter column stutel set data type char(11);


--刪除欄位
alter table studentinfo drop column abc;


--增加一個非空約束
alter table studentinfo alter column stuname set not null;

 

--重構表
reorg table studentinfo;

 

--增加一個唯一約束
alter table studentinfo alter column stutel set not null;
alter table studentinfo add constraint un_stutel unique(stutel);

 

--添加檢查約束
alter table studentinfo add column stuAge int;
alter table studentinfo add constraint ch_stuAge check(stuAge > 0 and stuAge <150);

 

--添加主鍵約束
alter table studentinfo add constraint pk_stuno primary key(stuno);

 

--刪除表
drop table studentinfo;

 

--建立表的同時添加約束方式1
create table studentinfo(
 stuNo int not null,
 stuName varchar(8) not null,
 stuAge int,
 stuTel char(8),
 constraint pk_stuNo primary key(stuNo),
 constraint un_stuName unique(stuName),
 constraint ch_stuAge check(stuAge >=0 and stuAge <150)
);

 

--建立表的同時添加約束方式2
create table studentinfo(
 stuNo int not null primary key,
 stuName varchar(8) not null unique,
 stuAge int check(stuAge >=0 and stuAge <150),
 stuTel char(8)
);

 

--添加主外鍵
--新增班級表
create table classInfo(
 classId int not null primary key,
 className varchar(20)
);

 


--建表的同時添加外鍵
create table studentinfo(
 stuNo int not null,
 stuName varchar(8) not null,
 stuBirth date not null,
 stuAge int,
 stuTel char(8),
 fclassId int,
 stuBirth date not null,
 constraint pk_stuNo primary key(stuNo),
 constraint un_stuName unique(stuName),
 constraint ch_stuAge check(stuAge >=0 and stuAge <150),
 constraint fk_fcalssId foreign key(fclassid) references classInfo(classId)
);


-- 自增
create table studentinfo(
 stuNo int not null generated always as identity(start with 1 ,increment by 1),
 stuName varchar(8) not null,
 stuAge int,
 stuTel char(8),
 fclassId int,
 stuBirth date not null,
 constraint pk_stuNo primary key(stuNo),
 constraint un_stuName unique(stuName),
 constraint ch_stuAge check(stuAge >=0 and stuAge <150),
 constraint fk_fcalssId foreign key(fclassid) references classInfo(classId)
);

  • 1
  • 2
  • 下一頁

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.