数据库课堂笔记

第一章节

(1)用SQL语言创建数据库、建表。建表时为各表建立关键字、设置外码,数据暂不录入.
建库:

```
create database school;

建表:


create table students
(sid char(10) primary key,
sname char(30) not null,
email char(30),
grade int
);

create table teachers
(tid char(10) primary key,
tname char(30) not null,
email char(30),
salary int
);
create table courses
(cid char(10) primary key,
cname char(30) not null,
hour int
);

设置外码:

create table choices
(no int primary key,
sid char(10) not null,
tid char(10),
cid char(10) not null,
score int,
constraint fksid foreign key (sid) references students(sid),
constraint fktid foreign key (tid) references teachers(tid),
constraint fkcid foreign key (cid) references courses(cid),
);

2) 为students表、courses建立按主键増序排列的索引

create index stusid on students(sid asc);
create index coucid on courses(cid asc);

3) 删除course上的索引

drop index courses.coucid;

students表中增加一个“出生日期”字段,类型为日期时间型

alter table students
add “出生日期” datetime;

5) 删除students中的“出生日期”字段

alter table students
drop column “出生日期”;

6) 删除SCHOOL数据库中的students表

alter table choices
drop constraint fksid;
drop table students;

摘要

  1. create database<数据库名>
    功能:创建数据库
  2. create table <表名>
    ( <列名><数据类型> [列级完整性约束条件]
    [,<列名><数据类型> [列级完整性约束条件]]
    [,<列级完整性约束条件>] );
    功能:创建一张新的表
  3. create [unique] index <索引名> on<表名>(<列名>);
    功能:创建表的索引
  4. drop index 表名.索引名
    功能:取消表的索引
  5. alter table <表名>add <列名><数据类型>;
    alter table<表名> drop column<列名>;
    功能:表中新增列及列数据类型和删除列
  6. drop table<表名>cascade;
    功能:删除数据库中的表
  7. drop database <数据库名>
    功能:删除数据库
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值