2020-08-19

SQL学习篇(1)


一、表的创建方法

创建表格:

create table 表名(

列名 数据类型 约束条件,

列名2 数据类型,

...

列名n 数据类型

);

例如创建一个学生表

create table stu(
    stuid number,
    sname varchar2(12),
    age integer,
    high number(4,2),
    birth date,
    mobile char(11)
);

数据类型

integer整数
number整数或小数
varchar2(最大字长)不定长字符串
char(固定长度)固定长度字符串
date时间日期
BLOB二进制文件,图片、视频、音乐..

约束条件

主键primary key一个表只有且仅有一个
非空not null一定要有,可以重复
唯一unique可以没有但不能重复
检查check数据有输入限制
外键约束foreign keyA表的数据,必须来源于B表

创建一个有约束条件的表格

create table stu_2(
    stuid number primary key,
    sname varchar2(12) not null,
    age integer check(age>0 and age<100),
    high number(4,2) check(high>1 and high<2),
    birth date,
    mobile char(11) unique
);

创建一个有外键约束的表格: 学生表的班级信息,是来自于班级表的子表外键的数据,必须来自于父表主键的数据

1. 创建一个班级表

create table class_info(
    cid number primary key,
    cname varchar2(20)
);

2. 创建学生表,添加外键约束

create table stu_3(
    stuid number primary key,
    sname varchar2(12) not null,
    age integer check(age>0 and age<100),
    high number(4,2) check(high>1 and high<2),
    birth date,
    mobile char(11) unique,
    class_id number,
    foreign key(class_id) references class_info(cid)
);

二、表的删除和修改

删除表格:
drop table 表名;
drop table stu_2;


学生表
create table stu(
    stuid number,
    sname varchar2(12),
    age integer,
    high number(4,2),
    birth date,
    mobile char(11)
);

修改表格的数据:alter
alter table 表名 列的操作;

新增列
alter table 表名 add 新列名 数据类型 约束条件;
alter table stu add ssex char(3) check(ssex='男' or ssex='女');

删除列
alter table 表名 drop column 列名;
alter table stu drop column high;

修改列的属性
alter table 表名 modify 列名 新的数据类型;
alter table stu modify sname varchar2(100);

修改列的名字
alter table 表名 rename column 旧列名 to 新列名;
alter table stu rename column mobile to phone;

修改表名:
alter table 表名 rename to 新表名;
约束条件的结构修改:
新增约束:
新增主键
alter table 表名 add constraint 主键名 primary key(列名);
alter table stu_info add constraint pk_stuid primary key(stuid);

新增唯一
alter table 表名 add constraint 唯一约束名 unique(列名); 
alter table stu_info add constraint uni_phone unique(phone);

新增检查
alter table 表名 add constraint 检查约束名 check(条件);
alter table stu_info add constraint ck_age check(age>0 and age<100);

新增非空
alter table 表名 add constraint 检查约束名 check(列名 is not null);
alter table stu_info add constraint nn_sname check(sname is not null);

新增外键
准备下前提条件和数据:
1.给stu_info表增加一个班级id的列
alter table stu_info add class_id number;

2.创建一个班级表
create table class_info(
cid number primary key,
cname varchar2(20)
);

3.给学生表的班级id增加外键约束
alter table 表名 add constraint 外键名 foreign key(外键列名) references 另外一个表名(主键列名);
alter table stu_info add constraint fk_classid_cid foreign key(class_id) references class_info(cid);

删除约束
alter table 表名 drop constraint 约束名字;
alter table stu_info drop constraint sys_c0011065;

三、表的复制

复制另一个表格的结构和数据:
create table 表名 as select * from 另一个表名;
create table emp as select * from scott.emp;

复制表格的时候,只复制表结构,不复制表格的数据:
create table 表名 as select * from 另一个表名 where 1=2;
create table emp2 as select * from scott.emp;

四、表的插入更新

往表格中添加数据:
insert into 表名 values(值1,值2...);
insert into class_info values(1001,'一年一班');

指定列添加信息:
insert into 表名(列名) values(值);
insert into class_info(cid) values(1003);
insert into stu_info(stuid,sname,class_id) values(1,'lilei',1002);

复制表格的内容:
insert into 表名 select * from 另外一个表名
insert into salgrade select * from scott.salgrade;

更改表格的内容:
update 表名 set 列=新值 where 列=旧值;
update class_info set cname='一年三班' where cid=1003;

五、数据库删除操作比较

删除表格的数据:

delete from 表名; 删除所有信息

truncate table 表名; 删除所有信息,删除速度更快

delete from 表名 where 列=值; 删除具体的行信息

 

delete和truncate的区别是什么?

1.delete是DML语句,所以需要提交操作;truncate是DDL语句,不需要提交操作

2.truncate删除效率更高,因为delete是以行为单位删除数据的

3.delete在删除的时候是有缓存的,一般会在内存中缓存30秒左右的时间

4.delete删除数据可以筛选行

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值