【JavaEE】第零章(2020.03.06)模式 表 索引

SQL语句是解释执行的,不是编译执行的

1.创建数据库

create database student

2.使用数据库(use)

use student

3.删除数据库(drop)

drop database student

4.在数据库中创建模式/架构

create schema s_t

5.删除模式

drop schema s_t
创建基本表

1.创建student表

create table student
(
	sno char(9) primary key,--先写变量名后写数据类型;primary key 规定了谁是主码。也可单独写 primary key(sno)
	sname char(10) not null,
	ssex char(2),
	ssage int,--整型固定四个字节
	sdept varchar(10)--最后一行不能有逗号
);

2.创建course表

create table course
(
	cno char(4) primary key,
	cname char(10) unique,--表示唯一,在这列上不允许有重复
	cpno char(4),
	ccredit smallint,
	foreign key (cpno) references course(cno)
);

3.创建sc表

create table sc
(
	sno char(9),--因为student表中的sno是9
	cno char(4),
	grade smallint,
	primary key (sno,cno),--两个都是主码,所以不能在单个后面直接加primary key
	foreign key (sno) references student(sno),
	foreign key (cno) references course(cno),
);

4.删除基本表

drop table student /*被sc引用,不能删除*/
drop table course  /*被sc引用,不能删除*/
drop table sc      /*可以删,没有被任何表引用*/

5.修改基本表

  • a.增加一列类型是datetime的s_entrance(入学时间)
alter table student
add s_entrance datetime  /*增加的时候不需要column*/
  • b.删除列s_entrance
alter table student
drop column s_entrance
  • c.将列grade的数据类型改成int
alter table sc
alter column grade int /*修改的时候需要column*/

6.添加唯一性约束

alter table student
add unique(sname)

7.为cname添加非空约束

alter table course 
alter column cname char(20) not null  /*已经有了唯一性约束,所以这行语句不成功*/

8.创建索引

create unique index stusno on student (sno asc) /*如果不写的话,默认是升序(asc)*/
create unique index scno on sc(sno asc,cno desc)/*学号升序,课程号降序(做次要关键字)*/
create index scgrade on sc (grade desc) /*创建普通索引*/
create clustered index scgradel on sc (grade asc)

9.删除索引

drop index sc.scno
drop index sc.scgrade

10.删除聚集索引

--drop index sc.[PK__sc__905C05331BDEA880]
/*这个索引是和约束联系在一起的,要想删除,必须先删除约束 右键——设计——cno右键——索引/键——删除——存盘*/

--create clustered index scsno on sc(sno asc)
--drop index sc.scsno
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值