创建表:
create table student(
id number(8) primary key not null,
name varchar(20) not null,
address varchar2(50) default '地址不详'
);
插入数据:
insert into student(id,name) values(111,'名字');
删除数据:
delete from student where id=111;
更新数据:
update student set name='名字' where name='姓名';
查询数据:
select * from student;
添加新列:
alter table student add(sex varchars(4));
添加约束
alter table student add constraint ck_student_sex check(sex='男' OR sex='女');
添加外键约束:
alter table stu1 add constraint fk_stu1_id foreign key(id) references stu2(id);
删除约束
ALTER TABLE INFOS DROP CONSTRAINT CK_INFOS_UNIQUE;
修改数据类型:
alter table student modify(id number(4));
修改列名:
alter table student rename column sex to gender;
删除列:
alter table student drop column gender;
修改表名:
alter table newstudent rename to student;
模糊查询:
select * from student where sname like '张_%';
创建序列用于数据自增长:
create sequence myclass_id; 使用:myclass_id.nextval
增长步:
alter sequence myclass_id increment by 1;
删除序列:
drop sequence myclass_id;
通过查询从已有表创建另一个表:
create table 表名 as select <query>;
定义注册用户的存储过程
DELIMITER $$
CREATE PROCEDURE register(IN userName VARCHAR(20),IN userPass VARCHAR(20),OUT userId INT)
BEGIN
INSERT INTO UserInfo(userName,userPass)VALUES(userName,userPass);
SELECT LAST_INSERT_ID() INTO userId;
END $$
使用触发器实现用户注册业务
DELIMITER $$
CREATE TRIGGER tr_ register AFTER INSERT ON userinfo FOR EACH ROW
BEGIN
IN
SERT INTO userdetails(id) VALUES(NEW.id);
END $$;