SQL——创建、管理表(DDL语句)

CREATE

--创建包含指定列和数据类型的表

create table tab
(no int,
 name varchar(50),
 age int);

--按照已存在的表创建表

create table tab
as
select name,age from student;

--通过create复制表结构
create table tab
as
select * from student
where 1=2;

ALTER

--增加列
alter table student 
add hobby varchar(200);

--删除列
alter table student
drop column age;

--重命名列
alter table student 
rename column name to username;

--更改列数据类型
alter table student
modify tname varchar2(40);

--表重命名
rename student to person;

DROP

--删除表
drop table student;

约束

/*
1.primary key 设置主键
2.not null    设置不可为空
3.check       检查约束
4.unique      唯一
5.references  设置外键关联(一个表中的 FOREIGN KEY 指向另一个表中的 PRIMARY KEY)
*/
create table student
(
  sid int constraint student_PK primary key,
  sname varchar2(20) constraint student_name_notnull not null,
  gender varchar2(2) constraint student_gender check (gender in ('男','女')),
  email varchar2(20) constraint student_email_unique unique
                     constraint studnet_email_notnull not null,
  deptno int constraint student_fk references dept(deptno) on delete set null
);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值