------------I have no other, only familiar with you, humble like a fool, eager to learn like a hunger------------
DDL operations:
-- create student table
create table student(
sno number(4) not null,
sname varchar2(10) not null,
birthdday date
);
--Add two fields to the student table
alter table student add(sal number(7,2),wechat varchar2(20));
--Modify the length of the sname field
alter table student modify(sname varchar2(20));
--Add gender field
alter table student add(sax char(2));
-- modify the name of the field
alter table student rename column sax to sex;
--Add a primary key constraint to the table
alter table student add constraint pk_student_sno primary key(sno);
--Add a unique constraint to the sname in the table
alter table student add constraint uk_student_sname unique(sname);
--Add a check constraint to the sex field in the table
alter table student add constraint ck_student_sex check(sex in('男','女'));
--Add a grade number field to the table
alter table student add(gid number(4));
--Create grade table main table
create table grade(
gradeId number(4) not null primary key,
gradename varchar2(10) not null
);
-- create foreign key constraints
alter table student add constraint fk_student_grade_gid foreign key (gid)
references grade(gradeId);
------------------------------------------------------
The sequence is somewhat like the self-incrementing column of mysql and the identity column of sqlserver
-- create sequence
create sequence sq_studet_sno -- the name of the sequence
start with 10 -- the starting value of the serial number
Increment by 10 -- step size increment value for each sequence number
maxvalue 9999999999 -- the maximum value of the serial number
cycle --nocycle whether to restart the cycle sequence number when the maximum value is reached
cache 20 --是否缓存序列号,默认是20个,假如步长是10,缓存20就是200个数,可以用nocache
--修改序列名称
rename sq_student_no to sq_student_sno
--查询创建的序列,系统默认视图
select * from user_sequences;
--使用序列 nextval
select sq_student_sno.nextval from dual;
--查看当前序列的值 currval
select sq_student_sno.currval from dual;
--模拟向grade表中使用序列新增数据
insert into grade(gradeid,gradename)
values(sq_student_sno.nextval,'三年级');
--查询年级
select * from grade;