--创建数据库
create database Etp;
--连接数据库
connect to Etp;
--断开连接
disconnect Etp;
--查看当前数据库下有哪些表
list tables;
--建表
create table studentInfo(
<wbr><wbr>stuno char(5) not null,<br><wbr><wbr>stuname varchar(8),<br><wbr><wbr>stubirth date<br>
);</wbr></wbr></wbr></wbr></wbr></wbr>
--查看表结构
describe table studentinfo;
--新增表字段
alter table studentinfo add stutel int;
alter table studentinfo add abc int;
--修改字段类型
alter table studentinfo alter column stutel set data type char(11);
--删除字段
alter table studentinfo drop column abc;
--增加一个非空约束
alter table studentinfo alter column stuname set not null;
<wbr><wbr></wbr></wbr>
--重构表
reorg table studentinfo;
<wbr><wbr></wbr></wbr>
--增加一个唯一约束
alter table studentinfo alter column stutel set not null;
alter table studentinfo add constraint un_stutel unique(stutel);
<wbr><wbr></wbr></wbr>
--添加检查约束
alter table studentinfo add column stuAge int;
alter table studentinfo add constraint ch_stuAge check(stuAge > 0 and stuAge <150);
<wbr><wbr></wbr></wbr>
--添加主键约束
alter table studentinfo add constraint pk_stuno primary key(stuno);
<wbr><wbr></wbr></wbr>
--删除表
drop table studentinfo;
<wbr><wbr></wbr></wbr>
--创建表的同时添加约束方式1
create table studentinfo(
<wbr><wbr>stuNo int not null,<br><wbr><wbr>stuName varchar(8) not null,<br><wbr><wbr>stuAge int,<br><wbr><wbr>stuTel char(8),<br><wbr><wbr>constraint pk_stuNo primary key(stuNo),<br><wbr><wbr>constraint un_stuName unique(stuName),<br><wbr><wbr>constraint ch_stuAge check(stuAge >=0 and stuAge <150)<br>
);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr></wbr></wbr>
--创建表的同时添加约束方式2
create table studentinfo(
<wbr><wbr>stuNo int not null primary key,<br><wbr><wbr>stuName varchar(8) not null unique,<br><wbr><wbr>stuAge int check(stuAge >=0 and stuAge <150),<br><wbr><wbr>stuTel char(8)<br>
);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr></wbr></wbr>
--添加主外键
--新增班级表
create table classInfo(
<wbr><wbr>classId int not null primary key,<br><wbr><wbr>className varchar(20)<br>
);</wbr></wbr></wbr></wbr>
<wbr><wbr></wbr></wbr>
--建表的同时添加外键
create table studentinfo(
<wbr><wbr>stuNo int not null,<br><wbr><wbr>stuName varchar(8) not null,<br><wbr><wbr>stuBirth date not null,<br><wbr><wbr>stuAge int,<br><wbr><wbr>stuTel char(8),<br><wbr><wbr>fclassId int,<br><wbr><wbr>stuBirth date not null,<br><wbr><wbr>constraint pk_stuNo primary key(stuNo),<br><wbr><wbr>constraint un_stuName unique(stuName),<br><wbr><wbr>constraint ch_stuAge check(stuAge >=0 and stuAge <150),<br><wbr><wbr>constraint fk_fcalssId foreign key(fclassid) references classInfo(classId)<br>
);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
-- 自增
create table studentinfo(
<wbr><wbr>stuNo int not null generated always as identity(start with 1 ,increment by 1),<br><wbr><wbr>stuName varchar(8) not null,<br><wbr><wbr>stuAge int,<br><wbr><wbr>stuTel char(8),<br><wbr><wbr>fclassId int,<br><wbr><wbr>stuBirth date not null,<br><wbr><wbr>constraint pk_stuNo primary key(stuNo),<br><wbr><wbr>constraint un_stuName unique(stuName),<br><wbr><wbr>constraint ch_stuAge check(stuAge >=0 and stuAge <150),<br><wbr><wbr>constraint fk_fcalssId foreign key(fclassid) references classInfo(classId)<br>
);</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
--先建表再添加外键
alter table studentinfo add constraint fk_classId foreign key(fclassid) references classInfo(classId);
--从系统表中查询约束名
select constname, tabname, refkeyname, reftabname, colcount, deleterule, updaterule from syscat.references;
--插入
insert into classinfo values(1,'ETP-1');
insert into studentInfo values(1,'徐越',20,'12345',1,'1995-01-21');
--不是全部插入则需要写列名
insert into studentinfo(stuNo,stuName,stuTel) values(2,'wj','111');
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><br>
-- 有自增长的列要写清楚列名<br>
insert into studentinfo(stuName,stuAge,stuTel,fclassid,stuBirth) values('徐越',20,'12345',1,'1995-01-21');<br>
insert into studentinfo(stuName,stuAge,stuTel,fclassid,stuBirth) values('tom',22,'12345',2,'1995-01-21');</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
--更新
update studentinfo set stuBirth = '1990-02-21' where stuName='xy';
update studentinfo set stuBirth = '1990-02-21',stuAge = 21 where stuName='xy';
--删除
deleted from studentinfo where stuName='xy';
--查询
select * from studentinfo where stuName='xy';
select stuName,stuAge from studentinfo;
--别名查询
select stuName as 姓名,stuAge as 年龄 from studentinfo;
select s.stuName as 姓名,s.stuAge as 年龄 from studentinfo s;
--运算查询
select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s;
--串联运算查询
select stuName||stuAge from studentinfo;
--and 和 or
select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s where s.stuName='xy' and s.stuAge=20;
select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s where s.stuName='xy' or s.stuAge=20;
--null
select * from studentinfo where stuAge is null;<wbr><wbr><br>
select * from studentinfo where stuAge is not null;<wbr><wbr></wbr></wbr></wbr></wbr>
<wbr><wbr></wbr></wbr>
--between and 包括边界 相当于>=和<=s
select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s where s.stuAge between 10 and 20
--in
select * from studentinfo<wbr><wbr> where stuName in ('xy','wj');<br>
select * from studentinfo<wbr><wbr> where stuName not in ('xy','wj');</wbr></wbr></wbr></wbr>
--模糊查询 like%,%表示多个字符
select * from studentinfo where stuName like 'x%'
<wbr><wbr></wbr></wbr>
--模糊查询 like_ , _表示单个字段
select * from studentinfo where stuName like 'x_';
<wbr><wbr></wbr></wbr>
--排序 order by
select * from studnetinfo order by fclassid desc;
select * from studnetinfo order by fclassid asc;
--distinct去掉重复
select distinct stuAge as 年龄 from studentinfo;
--group by,使用的时候,select 后面只能加2种字段: 1.group by 后面出现的,2.聚合函数
select fclassId as 班级号,count(stuName) as 学生个数 from studentinfo group by fclassid;
-having 在分组的基础上过滤,出现顺序where-group by-having
select fclassId as 班级号,count(stuName) as 学生个数 from studentinfo group by fclassid having count(StuName)>=2
分享到:
相关推荐
这是我在网上复制下来总结的,包括了sql语句基本操作和对应每一个操作的例子,对入门的你一定有帮助
关于sql语句的基本操作,能够更好的应用sql语句还有创建用户和对用户授权等基本的sql语句操作。
Java知识点总结大全(七) -- 数据库SQL语句基本操作关注后面会分享面向对象,io,集合,多线程,网络,sql的总结
sql语句基本操作[文].pdf
Oracle数据库 中的sql语句基本操作详细解释 有助初学者 学习
适合初学者。适合初学者。适合初学者。适合初学者。适合初学者。适合初学者。适合初学者。适合初学者。适合初学者。适合初学者。适合初学者。
sql基本操作语句,包括增添,修改,删除,查询等。运行过,可以使用。
数据库基本----SQL语句大全学会数据库是很实用D~~记录一些常用的sql语句...有入门有提高有见都没见过的...好全...收藏下... 其实一般用的就是查询,插入,删除等语句而已....但学学存储过程是好事...以后数据方面的...
结合实例讲述sql语句 是我们能够更好的理解sql的用法 和基本的句式
sql语句基本操作,技巧,提升,同步复制技术,经典操作(加密,分区,存储过程,触发器,基本函数等)。发布服务器配置。
sql基本操作语句,个人总结的。如果对你有所帮助,请下载。
基本的sql语句 标准的范例 简单的代码 可以直接引用,适合基本操作
mysql数据库的基本操作语句,自己整理的,喜欢的话后期还会有mysql其他方面的整理。
本文章主要是关于常见的SQL语句熟悉,看后能够比较清楚的熟悉SQL的一些基本操作。
mfc 操作数据库 sql基本语句 代码大全 里面有十几个例子,有源代码!
最基本的SQL语句,掌握了这4个最基本的语句,对于后面SQL的学习非常好
文章目录SQL语句基操(三)一、单表查询2、选择表中的若干元组。(2) 查询满足条件的元组④字符匹配[例3.29] 查询学号为201215121的学生的详细情况。[例3.30] 查询所有姓刘学生的姓名、学号和性别。[例3.31] 查询姓”...
文章目录SQL语句基操(五)——依旧是查询(一)嵌套查询4.带有EXISTS谓词的子查询[例 3.60] 查询所有选修了1号课程的学生姓名[例 3.61] 查询没有选修1号课程的学生姓名。[例 3.62] 查询选修了全部课程的学生姓名[例 ...
自己学习并整理的一点基本sql操作语句,包括建表 插值
10、说明:几个简单的基本的sql语句 选择:select * from table1 where 范围 插入:insert into table1(field1,field2) values(value1,value2) 删除:delete from table1 where 范围 更新:update table1 set field1...