建表和主键操作

1.创建表:create table student(

                  s_name nvarchar2(20),

                  s_sex nchar(2),

                  s_age int

               );

消除重复  select distinct

删除表  drop table student;

查看表  select * from  student;

插入数据

  insert into student values('张三','男',12);或者student(字段名)

查询插入

多表插入

查看表结构 desc student;

删除数据 delete from student where s_name='张三';

修改表名 rename student to stt;

删除字段 alter table student drop column s_name;

修改数据 update student set name='李四' where name='张三'

修改表中的字段名 alter table student rename column s_name to s_name2;

给表加备注 comment on table student is '你是谁';

查看表的备注信息 select *from user_tab_comments where TABLE_NAME='STUDENT';

添加字段 alter table student add address nvachar2(10);

修改字段                             modify 

复制表 create table stud3 as select * from student;

2.列操作

给表salary_grades添加虚拟列,虚拟列

       ALTER TABLE salary_grades

                  ADD (average_salary AS ((low_salary + high_salary)/2));

修改列的大小

ALTER TABLE order_status2

MODIFY status VARCHAR2(15);

修改数字列精度

ALTER TABLE order_status2

MODIFY id NUMBER(5);

修改数据类型

ALTER TABLE order_status2

MODIFY status CHAR(15);

修改默认值

ALTER TABLE order_status2

MODIFY last_modified DEFAULT SYSDATE - 1;

3.主键和外键

--为表添加主键

create table student31

(s_id int primary key,  --字段类型后直接加上主键关键词即可

 s_name nvarchar2(20),

 s_age int

);

insert into student31 values(1,'zhang',18);

insert into student31 values(2,'li',20);

 

--表建立好后,如何添加主键

--alter table student31 add constraint 主键约束名  主键关键词(字段名);

alter table student31 add constraint pk_s_id primary key(s_id);

 

--举例: 学生表和课程表建立外键

create table stu1

(s_id  int,

 s_name nvarchar2(20),

 c_id int 

);

create table course1 

(c_id int,

 c_name varchar2(20)

);

--给course表添加主键

alter table course1 add constraint pk_c_id1 primary key(c_id);

--给student表添加主键

alter table stu1 add constraint pk_s_id primary key(s_id);

 

--在学生表中建立一个外键,通过去引用课程表中的主键 

alter table stu1 add constraint fk_c_id foreign key(c_id) references course1(c_id);

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值