基本的SQL语句

      JAVA 2007-5-20 13:21

--创建名为STUDENT的表
create table student(
--设置STUDENTID为主键(唯一约束 PRIMARY KEY)不为空(非空约束 NOT NULL)
studentID number primary key not null
--NUMBER总长度为5,小数点后占2位
yuwen number(5,2))

--把表名STUDENT修改为FRIEND
rename student to friend

--查询表STUDNET的所有内容(*所有)
select * from student

--ID为表STUDENT新列的别名
select studentid as id from student

--字符串截取函数
select substr('abcdefg',1,5) from student

--将语文为NULL的记录用0做替换nvl(yuwen,0)
select yuwen from student where nvl(yuwen,0)=nvl(null,0)

--查询STUDENT表中语文为NULL的记录
select * from student where yuwen is null

--查询STUDENT表中语文不为NULL的记录
select * from student where yuwen is not null

--查询系统日期,关键字SYSDATE
select sysdate from student

--当月的最后一天
select LAST_DAY(sysdate) value from student

--本星期的某一天(比如星期日)
select Next_day(sysdate,7) value from student

--日期值中的月份进行了格式调整(返回日期再加上12月)
Select add_months(sysdate,12) from student

--日期格式化
select to_char(sysdate,'yyyy-mm-dd') from student    

--时间格式化
select to_char(sysdate,'hh24-mi-ss') from student    

--删除表STUDENT
drop table student     

--删除表STUDENT中STUDENTID>4的记录
delete from student where studentid>4     

--删除表STUDENT中所有的记录→可以事物回滚
delete student     

--删除表STUDENT中所有的记录→不可以事物回滚
truncate table student     

--在STUDNET表中添加一列
alter table student add phone varchar2(20)     

--在STUDNET表中添加多列
alter table student add (phone varchar2(20),address varchar2(50))     

--删除PHONE列
alter table student drop column phone     

--把STUDENT表中STUDENTID=9的内容修改为3
update student set studentid=3 where studentid=9

--把XM1和XM2两个字段的值加在一起赋给了studentName,CONCAT可以让字符相加或者是(studentName=XM1||XM2)
update student set studentName=concat('XM1','XM2') where studentID=1 

--序列ID自动增长,序列的种子数为1,增长为1
create sequence studentIDSep;     

--创建一个初始值为1000,增量为1的序列
create sequence studentIDSep increment by 1 start with 1000;      

--删除序列
drop sequence studentIDSep;     

--给STUDENTNAME创建唯一索引(关键字UNIQUE,NAME不能相同)
create unique index NameIndex on student(studentName)     

--查询索引
select NameIndex from student    

--删除索引
drop index NameIndex     

--给表STUDENT插入一行内容
insert into student values(studentIDSep.Nextval,'杨斌','男',21)     

--外键连接(FID_FK外键约束)
constraint fid_fk foreign key(studentid) references student(studentid)     

--创建一个名为MY_STUDENT的新表,该表复制了STUDENT表的数据和结构(列,数据类型)
create table my_student as select * from student  

--满足的条件
WHERE studentid=1    

--对结果分组
GROUP BY student     

--对结果排序
ORDER BY student     

标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commenttime}{commentauthor}

{CommentUrl}
{commentcontent}