当前位置:文档之家› sql完整数据库操作、存储过程、登录判断,增删改查(精)

sql完整数据库操作、存储过程、登录判断,增删改查(精)

sql完整数据库操作、存储过程、登录判断,增删改查(精)
sql完整数据库操作、存储过程、登录判断,增删改查(精)

create database AA go use AA go create table Student ( sid int primary key, sname nvarchar(20, sex nvarchar(20, birthday datetime, class nvarchar(10, pwd nvarchar(10 go create table Course ( cid int identity(1,1 primary key, cname nvarchar(20 go create table Score ( sid int, cid int, score int, primary key(sid,cid go insert into Course values('C#' insert into Course values('English' insert into Student values(1001,'张三','男', '1990-1-

12','一班','123' insert into Student values(1002,'李四','女', '1990-4-20','一班','456' insert into Student values(1003,'王五','男', '1991-10-11','二班','789' insert into Student

values(1004,'赵六','男', '1992-8-5','二班','101' insert into Student values(1005,'天齐','男', '1992-5-5','三班','120' insert into Score values(1001,1,65 insert into Score

values(1001,2,60 insert into Score values(1002,1,50 insert into Score values(1002,2,40 insert into Score values(1003,1,75 insert into Score values(1003,2,60 insert into Score values(1004,1,72 insert into Score values(1004,2,45 select * from student select sname from student select sid,sname from student select * from student order by sid desc --desc 降序 asc 升序 select * from student order by sex,class asc select * from student order by birthday desc select * from student where sid=1001 select * from student where sex='男' and birthday>'1991-1-1' select * from student where birthday between '1990-1-1' and

'1991-1-1' select * from student where sname like '%五%' --%%是通配符 select distinct(class from student --去除重复项 select count(sid from student select count(sid from student where sid=1001 and pwd='123' select count(sid,class from student group by class select count(sid,sex from student group by sex select count(sid,sex,class from student group by sex,class select count(sid,class from student where sex='男' group by class select sum(score from score select avg(score from score where sid=1001 select max(score,cid from score group by cid select avg(score,cid from score group by cid having avg(score>60 select avg(score,sid,cid from score group by sid,cid having

avg(score<60 select avg(score,sid from score group by sid having avg(score>59 select * from score where score = (select max(score from score select * from student where birthday = (select min(birthday from student select * from student select * from course select * from score select count(sid as Y_N from student where sid=1001 and pwd='123'

--select avg(score as avg from score where sid=1001 --select count(sid,class from student group by class --select count(sid,sex from student group by sex --select

count(sid,class,sex from student group by class,sex --select count(sid,class from student where sex='男' group by class --内连接 select stude

nt.sid,student.sname,student.class,score.score from student inner join score on student.sid = score.sid --三表链接 select

student.sid,student.sname,https://www.doczj.com/doc/4b1052417.html,ame,score.score from score inner join student on student.sid=score.sid inner join course on score.cid=course.cid where student.sname = '张三' --用右外表查询(以右表为基准) select

student.sid,student.sname,student.class,score.score from student right outer join score on student.sid=score.sid select student.sid,student.sname,student.class,score.score from student left outer join score on student.sid=score.sid where score.score is null --交叉链接(笛卡尔集合(少用 select student.sid,student.sname,score.score from student cross join score --嵌套查询 --查询最高分的学生 select sid,sname from student where sid in (select sid from score where score = (select max(score from score --总分最高的学生学

号 select sid from( select top 1 sum(score as s,sid from score group by sid order by s desc a --统计每门课程最高分的学生的学号 select score.sid,score.cid,score.score from score, (select max(score as m,cid from score group by cid b where score.cid=b.cid and

score.score= b.m --查班级平均分 select avg(c.score,c.class from (select a.class,b.score from student a,score b where a.sid=b.sid c group by class --个人平均成绩 select

avg(c.score,c.sid from (select a.sid,b.score from student a,score b where a.sid=b.sid c group by sid --单科最高分的同学学号和姓名和班级 select

distinct(d.sid,d.sname,d.class from ( select c.sid,c.sname,c.class from student c,( select sid,score from score,( select max(score as s,cid from score group by cid a where a.s = score.score b where c.sid=b.sid d declare @i int --int i =0; declare @s nvarchar(10 --string s; set @i = 1; --i = 10; --set @s = 'AAAAAAA' --s = 'AAAAAAA' while @i<10 --while 循环 begin print @i set @i = @i + 1 --i++ end select sid,score,case when cid=1 then 'C#' when cid=2 then 'English' end from score select sid,cid,score, case when

score>59 then '及格' else '不及格' end from score --print @i --本地测试用print --print @s -- --if @i=0 --if条件语句 --begin --print 'BBBBBBBBBBBBBBBBBBBBBBBB' --end --else --begin --print 'SSSSSSSSSSSSS' --end --使用存储过程判断用户登录信息alter proc sp_login @sid int, --输入参数 @pwd nvarchar(20, --输入参数 @s

nvarchar(20 output --输出参数 as declare @i int set @i = (select count(1 from student where sid=@sid and pwd=@pwd --return @i if @i = 1 set @s = '合法用户' else set @s = '非法用户' declare @s nvarchar(20 exec sp_login 1001,'123',@s output print @s if(select min(score from score where sid=1001>90 print '学生1001成绩全部优秀' else if(select min(score from score where sid = 1001>

59 print '学生1001成绩全部及格' else print '学生1001有成绩不及格' --为表创建具有不同字段名的视图 create view v_stu(sid,sname,sex as select sid,sname,sex from student --查看视图数据 select * from v_stu --使用存储过程实现从表及联删除 alter proc sp_delete @sid int as delete from score where sid = @sid delete from student where sid = @sid exec sp_delete 1001 --使用存储过程添加学生信息 -------------------添加---------------------------------------开始------------------------------------------------------------------------- alter proc sp_add @sid int, @sname nvarchar(20, @sex nvarchar(10, @birthday datetime, @class nvarchar(10, @pwd nvarchar(10, @i int, @r nvarchar(10 output --一个过程可以有多个输出参数但只有一个返回值输出参数是任何类型 as if @i = 0 begin if not exists (select sid from student where sid=@sid begin insert into student

values(@sid,@sname,@sex,@birthday,@class,@pwd set @r= '添加成功' end else set

@r= '重复添加' end else begin update student set sname =

@sname,sex=@sex,birthday=@birthday,class=@class,pwd=@pwd where sid=@sid if @@rowcount >0 set @r = '修改成功' else set @r = '修改无效' end declare @r int exec @r = sp_add 1011,'李四','男' ,'1990-1-12','一班','123' print @r ------------------------------------查找所有学生分数--------------------一个------------------------------------------------------------------- alter proc sp_selectAllStudentScore as select

student.sid,student.sname,https://www.doczj.com/doc/4b1052417.html,ame,score.score from score right join student on student.sid=score.sid left join course on score.cid=course.cid ----------------------------------

-------------- exec sp_selectAllStudentScore select * from student delete from student where sid= 0 -----------------------------------条件查询学生分数----------------------一个----------------------------------------------------------- create proc sp_selectstudentscore @sid int, @sname nvarchar(10 as if @sid>0 begin select

student.sid,student.sname,https://www.doczj.com/doc/4b1052417.html,ame,score.score from score inner join student on student.sid=score.sid inner join course on score.cid=course.cid where student.sid=@sid end else begin select student.sid,student.sname,https://www.doczj.com/doc/4b1052417.html,ame,score.score from score inner join student on student.sid=score.sid inner join course on score.cid=course.cid where student.sname like '%'+@sname+'%' end --------------------------------------------- exec sp_selectstudentscore 1001,'' exec sp_selectstudentscore '','三' --------------------------查找与删除学生----------------------------------一个----------------------------------------------------------- alter proc sp_operstudent @sid int, @i int as if @i = 0 select * from student where sid = @sid else delete from student where sid = @sid ----------------------登录---------------------------

----------一个------------------------------------------------------------------- alter proc

sp_login @sid int, @pwd nvarchar(20 as declare @i int set @i = (select count(1 from student where sid = @sid and pwd = @pwd return @i --------------------------查看学生分数信息---------------------------------------------------------------------------------------------------- alter proc sp_viewstudent @sid int, @sum int output, @avg int output, @pid int output as declare cur cursor for select avg(score p, sum(score s,sid from score group by sid order by s desc open cur declare @p int, @s int, @sid2 int, @i int set @i = 1 fetch from cur into @p, @s,@sid2 while @@fetch_status = 0 begin if @sid2 = @sid begin set @pid = @i set @sum = @s set @avg = @p end set @i = @i + 1 fetch from cur into @p,

@s,@sid2 end close cur deallocate cur select cname,score from score a,course b where a.cid = b.cid and a.sid = @sid declare @avg int,@pid int,@sum int exec sp_viewstudent 1003 ,@pid output,@sum output,@avg output print @sum print @avg print @pid -------------------------------------------------------------结束------------------------------------------------------ --使用存储过程添加新课程信息,并输入新课程的ID alter proc sp_addCourse

@cname nvarchar(10 as insert into course values(@cname return @@identity declare @i int exec @i = sp_addCourse 'PHP' print @i select * from course --实现数据表分页查询--页码 = 总行数 / 每页行数 page --每页行数 size --总行数 --paixu linshibiao zhuanyonghanshu select top 5 * from (select top (9-(2-1*5 * from employees order by employeeid desc a order by a.employeeid alter proc sp_page @page int, --页码 @table varchar(10, --表名称 @orderby varchar(10, --排序字段名 @size int as declare @sql varchar(500 --放sql语句字符串 set @sql = 'declare @count int ' set @sql = @sql +' set @count =(select count(1 from ' + @table + '' set @sql = @sql + ' select top ' + str(@size + ' * from (select top (@count - (' + convert(varchar(10,@page + '-1*' +str(@size+' * from ' + @table + ' order by ' + @orderby + ' desc a ' + 'order by a.'+ @orderby exec (@sql print @sql exec sp_page 1,'employees','employeeid',3 --函数 declare @str varchar(10 set @str = '1234' declare @i int set @i = 1234 --print len(@str print

len(ltrim(str(@i print substring(@str,1,2 print floor(rand(*1000 -- print getdate(--Datetime.Now print dateadd(day,2,getdate( print dateadd(day,2,'2011-10-20'--往后添加时间 print datediff(day,'2011-10-25','2012-12-10'--时间差 TimeSpan.days print datepart(year,'2011-10-25'--获取时间格式中的一部分(即年,月或日) print datepart(month,'2011-10-25' print datepart(day,'2011-10-25' print

convert(nvarchar(10,datepart(year,'2011-10-25'

+ '/' + convert(nvarchar(10,datepart(month,'2011-10-25' + '/' +

convert(nvarchar(10,datepart(day,'2011-10-25' --按出生年份统计学生人数 --sid counts years select count(sid,datepart(year,birthday from student group by datepart(year,birthday select * from @table declare @table nvarchar(10 set @table = 'student' exec('select * from ' + @table create proc sp_AA @table varchar(10 as exec ('select * from ' + @table exec sp_AA 'student' --编写通用版的分页存储过程 --自定义函数 create function

fun(@i int returns nvarchar(10 as begin return convert(nvarchar(10,@i end print

dbo.fun(10--dbo 当前数据库里有效 --定义函数输入姓名后返回学号 create function fun2(@sname nvarchar(10 returns int as begin declare @i int set @i = (select sid from student where sname=@sname return @i end print dbo.fun2('张三' --查询学号,课程

号,分数同时在分数栏将<60的成绩直接输出为不及格 --学号课程分数 --1001 1 95 --1002 1 不及格 alter function fun3(@score int returns nvarchar(10 as begin declare @r nvarchar(10 if @score<60 set @r = '不及格' else set @r =

convert(nvarchar(10,@score return @r end select sid,cid,dbo.fun3(score from score --在同一列,输出 Employees 表中的lastname 和 firstname 字段 create function

fun4(@lastname varchar(10,@firstname varchar(10 returns varchar(20 as begin return

@lastname +'-'+@firstname end select employeeid,dbo.fun4(lastname,firstname,title from employees --计算 order details 表每条订单的总价 select * from [order details] create function fun5(@unitprice money,@quantity smallint,@discount real returns smallint as begin return @unitprice * @quantity * (1 - @discount end select

*,dbo.fun5(unitprice,quantity,discount from [order details] --游标不占用物理内存,全部是临时文件 declare cur1 cursor for select sid,sname,class from student open cur1 declare @sid int, @sname nvarchar(10,@class nvarchar(10,@i int set @i = 1 fetch from cur1 into @sid,@sname,@class --fetch 取游标所在的行的值 while @@fetch_status = 0 begin print convert(nvarchar(10,@i+'. '+

convert(nvarchar(10,@sid+','+@sname+','+@class set @i =@i + 1 fetch from cur1 into @sid,@sname,@class end close cur1 deallocate cur1 --给所有分数<60的人加送10分declare cur2 cursor for select * from score open cur2 declare @sid int,@cid int,@score int fetch from cur2 into @sid,@cid,@score while @@fetch_status = 0 --0语句成功 -1

语句失败或行不在结果集中 -2提取的行不存在 begin if @score < 60 update score set score = @score + 10 where sid = @sid and cid = @cid fetch from cur2 into

@sid,@cid,@score end close cur2 deallocate cur2 --清空资源 --找出重名的人,并使用合适的方式返回

结果数据。 select * from student alter proc sp_Find as create table #table1 ( sid int, sname nvarchar(10 declare cur3 cursor for select sid,sname from student open cur3 declare @sid int,@sname nvarchar(10,@count int fetch from cur3 into @sid,@sname while @@fetch_status = 0 begin set @count = (select count(sid from student where sname=@sname if @count > 1 insert into #table1 values(@sid,@sname --print

convert(nvarchar(10,@sid+' '+@sname fetch from cur3 into @sid,@sname end close

cur3 deallocate cur3 select * from #table1 exec sp_Find --触发器 --级联删除学生信息alter trigger t_delete on student for delete as begin declare @sid int set @sid = (select sid from deleted --print @sid delete from score where sid = @sid end delete from student where sid = 1004 select * from student select * from score --添加学生信息,同时设置每人的考试成绩默认为0 alter trigger t_insert on student for insert as begin declare @sid int set @sid = (select sid from inserted declare cur cursor for select cid from course open cur declare @cid int fetch from cur into @cid while @@fetch_status = 0 begin insert into score values(@sid,@cid,0 fetch from cur into @cid end close cur deallocate cur end insert into Student values(1030,'十几','男', '1992-5-5','三班','120' select * from student select * from score --修改学生学号信息 101001 111001 alter trigger t_update on student for update as begin declare @sid1 int,@sid2 int set @sid1 = (select sid from deleted set @sid2 = (select sid from inserted update score set sid = @sid2 where

sid=@sid1 end update student set sid = 101001 where sid = 1001 select * from course begin tran begin try delete from student where sid = 1002 end try begin catch rollback --回滚(撤销) end catch select * from student --排序 declare @temp table(pid int identity(1,1, s int,sid int insert into @temp select sum(score s,sid from score group by sid order by s desc select pid from @temp where sid =1001

数据库增删改查基本语句

数据库增删改查基本语句 adoquery1.Fielddefs[1].Name; 字段名 dbgrid1.columns[0].width:=10; dbgrid的字段宽度 adoquery1.Fields[i].DataType=ftString 字段类型 update jb_spzl set kp_item_name=upper(kp_item_name) 修改数据库表中某一列为大写select * from master.dbo.sysobjects ,jm_https://www.doczj.com/doc/4b1052417.html,ers 多库查询 adotable1.sort:='字段名称ASC' adotable排序 SQL常用语句一览 sp_password null,'新密码','sa' 修改数据库密码 (1)数据记录筛选: sql="select * from 数据表where 字段名=字段值orderby 字段名[desc] " sql="select * from 数据表where 字段名like '%字段值%' orderby 字段名[desc]" sql="select top10 * from 数据表where 字段名orderby 字段名[desc]" sql="select * from 数据表where 字段名in('值1','值2','值3')" sql="select * from 数据表where 字段名between 值1 and 值2" (2)更新数据记录: sql="update 数据表set 字段名=字段值where 条件表达式" sql="update 数据表set 字段1=值1,字段2=值2……字段n=值n where 条件表达式" (3)删除数据记录: sql="delete from 数据表where 条件表达式" sql="delete from 数据表"(将数据表所有记录删除) (4)添加数据记录: sql="insert into 数据表(字段1,字段2,字段3…) values(值1,值2,值3…)" sql="insert into 目标数据表select * from 源数据表"(把源数据表的记录添加到目标数据表)

SQL语句增删改查

一、删:有2中方法 1.使用delete删除数据某些数据 语法:delete from <表名> [where <删除条件>] 例:delete from a where name='开心朋朋'(删除表a中列值为开心朋朋的行) 注意:删除整行不是删除单个字段,所以在delete后面不能出现字段名 2.使用truncate table 删除整个表的数据 语法:truncate table <表名> 例:truncate table tongxunlu 注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表 二、改 使用update更新修改数据 语法:update <表名> set <列名=更新值> [where <更新条件>] 例:update tongxunlu set 年龄=18 where 姓名='蓝色小名' 注意:set后面可以紧随多个数据列的更新值;where子句是可选的,用来限制条件,如果不选则整个表的所有行都被更新 四、查 1.普通查询 语法:select <列名> from <表名> [where <查询条件表达试>] [order by <排序的列名>[asc或desc]] 1).查询所有数据行和列 例:select * from a 说明:查询a表中所有行和列 2).查询部分行列--条件查询 例:select i,j,k from a where f=5 说明:查询表a中f=5的所有行,并显示i,j,k3列 3).在查询中使用AS更改列名 例:select name as 姓名from a whrer xingbie='男' 说明:查询a表中性别为男的所有行,显示name列,并将name列改名为(姓名)显示 4).查询空行 例:select name from a where email is null 说明:查询表a中email为空的所有行,并显示name列;SQL语句中用is null或者is not null来判断是否为空行 5).在查询中使用常量 例:select name '唐山' as 地址 from a 说明:查询表a,显示name列,并添加地址列,其列值都为'唐山' 6).查询返回限制行数(关键字:top percent) 例1:select top 6 name from a 说明:查询表a,显示列name的前6行,top为关键字 例2:select top 60 percent name from a 说明:查询表a,显示列name的60%,percent为关键字 7).查询排序(关键字:order by , asc , desc)

SQL数据库中的增删改查总结1

一、增:有2种方法 1.使用insert插入单行数据: 语法:insert [into]<表名> [列名] values <列值> 例:insert into Strdents (姓名,性别,出生日期) values ('邢金聪','男','1990/6/15') 注意:如果省略表名,将依次插入所有列 2.使用insert,select语句将现有表中的数据添加到已有的新表中 语法:insert into <已有的新表> <列名> select <原表列名> from <原表名> 例:insert into addressList ('姓名','地址','电子邮件')selectname,address,email from Strdents 注意:查询得到的数据个数、顺序、数据类型等,必须与插入的项保持一致 二、删:有2中方法 1.使用delete删除数据某些数据 语法:delete from <表名> [where <删除条件>] 例:delete from awhere name='邢金聪'(删除表a中列值为邢金聪的行) 注意:删除整行不是删除单个字段,所以在delete后面不能出现字段名 2.使用truncate table 删除整个表的数据

语法:truncate table<表名> 例:truncate table addressList 注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能 用于有外建约束引用的表 三、改 使用update更新修改数据 语法:update <表名> set <列名=更新值> [where <更新条件>] 例:truncate table addressList 注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能 用于有外建约束引用的表 四、查 1.普通查询 语法:select <列名> from <表名> [where <查询条件表达试>] [order by<排序的列 名>[asc或desc]] 1).查询所有数据行和列 例:select * from a 说明:查询a表中所有行和 2).查询部分行列--条件查询 例:select i,j,kfrom a where f=5 说明:查询表a中f=5的所有行,并显示i,j,k3列

JAVA数据库基本操作增删改查(精)

JAVA 数据库基本操作, 增删改查 package mypack; JAVA 数据库基本操作, 增删改查 import java.sql.Connection; import java.sql.ResultSet; import java.util.ArrayList; public class DbOper {//查询多行记录public ArrayList select({Connection conn =null; ResultSet rs =null; try {import java.sql.PreparedStatement; import java.sql.SQLException; PreparedStatement pstmt =null; ArrayList al =new ArrayList(; conn =DbConn.getConn(;pstmt =conn.prepareStatement(“select *from titles ”; rs =pstmt.executeQuery(;while (rs.next({Titles t =new Titles(;t.setTitleid(rs.getString(1;t.setTitle(rs.getString(2;al.add(t;}}catch (SQLExceptione { e.printStackTrace(;}finally {try {//TODO 自动生成catch 块if (rs!=null rs.close(;if (pstmt!=nullpstmt.close(;if (conn!=nullconn.close(;}catch (SQLExceptione { e.printStackTrace(;}}//TODO 自动生成catch 块 return al; }//查询单个对象public Titles selectOne(Stringtitleid{Connection conn =null; ResultSet rs =null; try {PreparedStatement pstmt =null; Titles t =new Titles(;

mysql增删改查基本语句

mysql 增、删、改、查基本语句 数据库的链接和选择及编码 $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error()); $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error()); mysql_query("set names 'utf8'"); 添加数据 $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error()); $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error()); mysql_query("set names 'utf8'",$sel); $add="insert into title(title,content,username,time) values('$title','$content','$username',$time)"; $query=mysql_query($add); if($query){ echo "add sucess"; echo ""; } else echo "add false"; 删除数据 $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error()); $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error()); mysql_query("set names 'utf8'"); $id=$_GET['id']; $delete="delete from title where id='$id'"; $query=mysql_query($delete); if($query){ echo "delete sucess!"; echo ""; } else echo "delete false"; 改数据 $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error()); $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error()); mysql_query("set names 'utf8'",$sel);

数据库增删改查

02.连接命令:mysql -h[主机地址] -u[用户名] -p[用户密码] 03.创建数据库:create database [库名] 04.显示所有数据库: show databases; 05.打开数据库:use [库名] 06.当前选择的库状态:SELECT DATABASE(); 07.创建数据表:CREATE TABLE [表名]([字段名] [字段类型]([字段要求]) [字段参数], ......); 08.显示数据表字段:describe 表名; 09.当前库数据表结构:show tables; 10.更改表格 11. ALTER TABLE [表名] ADD COLUMN [字段名] DATATYPE 12. 说明:增加一个栏位(没有删除某个栏位的语法。 13. ALTER TABLE [表名] ADD PRIMARY KEY ([字段名]) 14. 说明:更改表得的定义把某个栏位设为主键。 15. ALTER TABLE [表名] DROP PRIMARY KEY ([字段名]) 16. 说明:把主键的定义删除。 17.显示当前表字段:show columns from tablename; 18.删库:drop database [库名]; 19.删表:drop table [表名]; 20.数据操作 21.添加:INSERT INTO [表名] VALUES('','',......顺序排列的数据); 22.查询: SELECT * FROM [表名] WHERE ([条件]); 23.建立索引:CREATE INDEX [索引文件名] ON [表名] ([字段名]); 24.删除:DELETE FROM [表名] WHERE ([条件]); 25.修改:UPDATE [表名] SET [修改内容如name = 'Mary'] WHERE [条件]; 26. 27.导入外部数据文本: 28.1.执行外部的sql脚本 29.当前数据库上执行:mysql < input.sql 30.指定数据库上执行:mysql [表名] < input.sql 31.2.数据传入命令load data local infile "[文件名]" into table [表名]; 32.备份数据库:(dos下) 33.mysqldump --opt school>school.bbb 34. 35. 36. 37.提示:常用MySQL命令以";"结束,有少量特殊命令不能加";"结束,如备份数据库 38.一. 增删改查操作 39. 40.============================================================================ ===== 41.1. 增: 42.insert into 表名values(0,'测试'); 43.注:如上语句,表结构中有自动增长的列,也必须为其指定一个值,通常为0 44.insert into 表名(id,name) values(0,'尹当')--同上

SQL常用增删改查语句

SQLSQL常用增删改查语句 作者:hiker 一.Insert 插入语句 1.Insert into 表名(列名)values (对应列名值) //插入一行. 2.Insert into 新表名(列名) Select (列名) 旧表名 3.Select 旧表名.字段… Into 新表名from 旧表名 4.Select identity ( 数据类型,标识种子,标识增长量) as 列名 Into新表名 From 旧表名 5.Insert 表名(列名) Select (对应列名值) union Select (对应列名值) union Select (对应列名值) 二.Update 更新语句 1.Update 表名set 列名=’更新值’ where 更新条件 三.delete 删除语句 1.delete from 表名where 删除条件 2.truncate table 表名//删除表中所有行 四.select 基本查询语句 1.select 列名from 表名where 查询条件 order by 排序的列名asc或desc升/降 2.select 列名as 别名from 表名where 查询条件 3.select 列名from 表名where 列名is null //查询空值 4.select 列名, ‘常量值’ as 别名from 表名//查询时定义输出一列常量值 5.select top 5 列名from 表名//查询前5行 6.select top 5 percent 列名from 表名//查询前百分之5的数据行 五.select 函数查询语句 1.select LEN(Class_Name)from Class //查询class_Name字符串长度 2.select upper(Class_Name)from Class //查询class_Name并转换为大写 3.ltrim和rtrim //清除字符串左右空格 4.select REPLACE(card_No,'0','9')from CardRecord//修改列中字符串中的字符 列名字符串中0修改为9 5.select STUFF(Card_No,2,3,'8888')from CardRecord 列名字符串中第2个开始删除3个字符,再从第二个开始插入8888字符串 6.select GETDATE()//显示系统日期

MySql常用SQL增删改查精

SQL语句的添加、删除、修改 添加、删除、修改使用db.Execute(Sql命令执行操作 (一Select 语句 A.普通查询 1. 查询全部的行和列 select * from users 2. 查询部分的行并用as命名列(as可用空格代替) select UserName as用户名,NickName 密码,sex from users where sex = 0 select UserName,NickName,sex from users where sex <> 0 3. 查询空行 i. select UserName from users where sex is nu II; ii. select UserName from users where sex =""; 4. 使用常量列 select UserName as用户名,"中北大学"as "学校"from users; 5. 限制固定行数 select UserId ,UserName a用户名from users limit 2,4; 6. 升序排列降序排列按照多列排序 i. select * from users where Height >178 order by age asc; ii. select * from users where Height >178 order by age desc; iii. select UserName as用户名,Height 身高,Age as 年龄from users where Age>20 order

by Height asc, Age desc;

sql完整数据库操作、存储过程、登录判断,增删改查(精)

create database AA go use AA go create table Student ( sid int primary key, sname nvarchar(20, sex nvarchar(20, birthday datetime, class nvarchar(10, pwd nvarchar(10 go create table Course ( cid int identity(1,1 primary key, cname nvarchar(20 go create table Score ( sid int, cid int, score int, primary key(sid,cid go insert into Course values('C#' insert into Course values('English' insert into Student values(1001,'张三','男', '1990-1- 12','一班','123' insert into Student values(1002,'李四','女', '1990-4-20','一班','456' insert into Student values(1003,'王五','男', '1991-10-11','二班','789' insert into Student values(1004,'赵六','男', '1992-8-5','二班','101' insert into Student values(1005,'天齐','男', '1992-5-5','三班','120' insert into Score values(1001,1,65 insert into Score values(1001,2,60 insert into Score values(1002,1,50 insert into Score values(1002,2,40 insert into Score values(1003,1,75 insert into Score values(1003,2,60 insert into Score values(1004,1,72 insert into Score values(1004,2,45 select * from student select sname from student select sid,sname from student select * from student order by sid desc --desc 降序 asc 升序 select * from student order by sex,class asc select * from student order by birthday desc select * from student where sid=1001 select * from student where sex='男' and birthday>'1991-1-1' select * from student where birthday between '1990-1-1' and '1991-1-1' select * from student where sname like '%五%' --%%是通配符 select distinct(class from student --去除重复项 select count(sid from student select count(sid from student where sid=1001 and pwd='123' select count(sid,class from student group by class select count(sid,sex from student group by sex select count(sid,sex,class from student group by sex,class select count(sid,class from student where sex='男' group by class select sum(score from score select avg(score from score where sid=1001 select max(score,cid from score group by cid select avg(score,cid from score group by cid having avg(score>60 select avg(score,sid,cid from score group by sid,cid having avg(score<60 select avg(score,sid from score group by sid having avg(score>59 select * from score where score = (select max(score from score select * from student where birthday = (select min(birthday from student select * from student select * from course select * from score select count(sid as Y_N from student where sid=1001 and pwd='123'

SQL常用的增删改查语句、视屏笔记

SQL:结构化查询语言,是国际化标准组织采纳的标准数据库语言 作用:数据库管理员可以用利用次语言操作数据库系统,即:SQL是一种能够被数据库系统读懂的操作语言。 T—SQL是微软公司针对SQL Server这种关系数据库所定义的一种SQL语言,用户可以完成Server数据库中的所有操作。 SQL的分类: (1):数据定义语言,DDL 用来创建数据库和数据库中的各种对象 数据库中的对象包括:表、视图、索引、存储过程、触发器 DDL常用的命令包括:create、alter、drop (2):数据操纵语言,DML 可以完成对数据库中数据的查询、更新、删除、插入的功能 DML常用的命令包括:select、update、delete、insert (3)数据控制语言,DCL DCL常用的命令包括:grant、revoke、deny (4)数据类型 1、数字数据类型 a、整型数据类型:bigint、int、smallint、tinyint b、浮点数据类型:float、real c、固定精度数据类型:decimal、numeric d、货币数据类型:money、smallmoney 将数据格式转换成字符串方法:STR(ID;---ID为数据类型,STR(ID为字符串 2、字符数据类型:varchar、char、text、nchar、nvarchar、ntext 区别char和varchar数据类型: char数据类型是存放固定长度的字符 Varchar数据类型是存放动态长度的字符

char(14,实际放入到数据库的字符长度为14个字符,如果实际存储的长度小于定义的 长度,那么数据库会在次字符串后面加上3个空格 1、insert 语句: insert语句是属于SQL语言中的DML 作用:为表插入数据 你向一个表中插入一条新记录,但有一个字段没有提供数据。在这种情况下,有下面 的四种可能: 1、如果该字段有一个缺省值(默认值),该值会被使用。 2、如果该字段可以接受空值,而且没有缺省值,则会被插入空值。 3、如果该字段不能接受空值,而且没有缺省值,就会出现错误。你会收到错误信 息: The column in table mytable may not be null. 4、如果该字段是一个标识字段,那么它会自动产生一个新值。当你向一个有标识字 段的表中插入新记录时,只要忽略该字段,标识字段会给自己赋一个新值。 使用INSERT语句可向文本型字段中插入数据。但是,如果你需要输入很长的字符 串,你应该使用WRITETEXT语句。 语法:insert into 表名(列名1,列名2,….. values(值1,值2,…. 注意:(列名1,列名2,…..可以不写,这样的话,插入的values(值1,值2,….中的值必须包含表中的所有列,不然会报错。 把INSERT 语句与SELECT 语句结合起来,可以添加多个记录。像这样: 代码:INSERT mytable (first_column,second_column SELECT another_first,another_second FROM anothertable WHERE another_first=’Copy Me!’ 这个语句从anothertable拷贝记录到mytable.只有表anothertable中字段another_first的值为’Copy Me!’的记录才被拷贝。 2、update 语句它也是属于SQL语句中的DML, 可以修改表的数据,使用WHERE子 句来选择更新特定的记录。 需要更新很长的字符串,应使用UPDATETEXT语句。如果你不提供WHERE子句,表中 的所有记录都将被更新。

数据库语句增删改查

mysql语句用法,添加、修改、删除字段一,连接MySQL 二,MySQL管理与授权 三,数据库简单操作 四, 数据库备份 五,后记 一,连接MySQL 格式:mysql -h 远程主机地址 -u 用户名 -p 回车 输入密码进入: mysql -u root -p 回车 Enter password: ,输入密码就可以进入 mysql> 进入了 退出命令:>exit 或者ctrl+D 二,MySQL管理与授权 1.修改密码:

格式:mysqladmin -u 用户名 -p 旧密码 password 新密码 2.增加新用户: >grant create,select,update....(授予相关的操作权限) ->on 数据库.* -> to 用户名@登录主机 identified by '密码' 操作实例: 给root用户添加密码: # mysqladmin -u root password 52netseek 因为开始root没有密码,所以-p旧密码一项可以省略. 登陆测试: # mysql -u root -p 回车 输入密码,成功登陆. 将原有的mysql管理登陆密码52netseek改为52china. # mysqladmin -u root -p 52netseek password '52china' 创建数据库添加用户并授予相应的权限: mysql> create database phpbb; Query OK, 1 row affected (0.02 sec)

mysql> use phpbb; Database changed mysql> grant create,select,update,insert,delete,alter -> on phpbb.* -> to phpbbroot@localhost identified by '52netseek'; Query OK, 0 rows affected (0.00 sec) 授予所有的权限: >grant all privileges >on bbs.* >to bbsroot@localhost identified by '52netseek' 回收权限: revoke create,select,update,insert,delete,alter on phpbb.* from phpbbroot@localhost identified by '52netseek'; 完全将phpbbroot这个用户删除: >use mysql >delete from user where user='phpbbroot' and host='localhost'; >flush privileges; 刷新数据库 三,数据库简单操作

Mysql数据库·增删改查

Mysql Oracle(甲骨文) 大型数据库 MySql 中小型数据库 DB2 SqlServer ..... Mysql的发展: 瑞典的Mysql AB公司 2008年Sun公司(JAVA) 2009年Oracle 收购sun公司 IBM 69亿美元sun Eclipse(日蚀) Oracle 74亿美元sun Mysql的简单使用: 1.登陆mysql数据库 win+r --->cmd mysql -uroot -p1234 修改密码:mysql> set password for rootlocalhost = password('1234'); 此处可能存在异常情况原因:a、未配置环境变量b、Mysql服务未开启(net start mysql) 2.对库的操作 a.查看所有的库 show databases; 系统自带库: information_schema mysql test b.创建库 create database day01;(不指定编码,跟随数据库系统编码) create database db1 default character set gbk;(指定编码) 查看创建库的语句: show create database 库名. 修改库的编码: alter database day01 default character set utf8; c.删除库 drop database 库名.

drop database day01; 注意: 系统自带的三个库不能删除. d.使用库 use db1; 3.对表的操作 表:二维关系表有行有列的关系表. 记录:表中的一行数据. 字段:表中的一列. 常用的字段类型: 字符串类型: varchar(长度) 、char 数值类型: int(整数) float double(小数) 日期类型: date a.创建表 员工表:员工号性别年龄职位薪水入职日期 create table emp( empno varchar(4), name varchar(30), sex varchar(5), age int(3), job varchar(30), salary int(5), hiredate date ); b.查看所有的表 show tables; c.查看建表语句 show create table 表名. d.查看表结构 desc 表名. e.往表中插入数据 e1.给表中所有的字段插入数据 insert into emp (empno,name,sex,age,job,salary,hiredate) values ('1001','zhangsan','m',22,'developer',10000,'2015-12-21'); 简写形式: insert into emp values ('1002','lisi','m',23,'test',8000,'2015-10-10'); e2.给表中部分字段插入数据 insert into emp(empno,name,sex,age) values ('1003','cuihua','w',18);

sql与mysql可视化增删改查

MYSQL与SQL可视化增,删,改 1.建立表格: 属性: public JTable table; public DefaultTableModel dftm; public String[] columnNames= new String[] { "序号", "用户名", "密码"}; // 定义数组表示表格标题 dftm = new DefaultTableModel(null, columnNames); /*for (int i = 0; i < 10; i++) { Object[] content = { 1, 2, 3 }; dftm.addRow(content);// 增加行 }*/ initTable(dftm); JTable table = new JTable(dftm);// 根据TableModel显示内容 JScrollPane src = new JScrollPane(table); //给表格增加滚动条 src.setBounds(50, 150, 500, 200); //设置滚动条位置与大小 contentPane.add(src); 2.“修改用户”按钮的代码: JButton rewamp = new JButton("\u4FEE\u6539\u7528\u6237"); rewamp.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int id=Integer.parseInt(txtId.getText()); String name=txtName.getText();//取得用户名文本框的值 String pwd=txtPwd.getText();//取得用户密码文本框的值 String sqlUpdate="UPDATE userlist set name='" + name+"', password='"+pwd+"' where Id="+id; DBComm dbco=new DBComm(); dbco.openConnection(); try { dbco.executeUpdate(sqlUpdate);

SQL单词及数据库增删改查

数据库(Database, DB) 数据库管理系统(Database Management System, DBMS) 数据库管理员(Database Administrator, DBA) 数据库系统(Database System, DBS) SQL:结构化查询语言(structured Query Language)DB: 数据库(database) DBA:数据库管理员(database administrator)DBMS:数据库管理系统(database management system) Toa d:癞蛤蟆,一款开发数据库的可视化软件 Roo t: 根 Re la tion shi p:关系 U ser:用户 Ta ble:表 P ro ce dure:存储过程 Fun c tion:函数 Tri gger:触发器 Vie w:视图 In de x:索引 C rea te:创建 Dro p:删除 Ta ble:表 En gine:引擎 Co lu mn:列 P ri ma ry:主要的 Primary key:主键 Na me:列名 Au to:自动的 In cre men t:可增长的 Au to_In cre men t: 自动增长 Da ta Ty pe:数据类型 Not null:非空 Co mmen t:注释 Cha ra c ter se t:字符集 Co lle c tion:采集(对比方法) Fo reig n:外部的

Fo reig n key:外键 SQL 数据化查询语言 DML数据操作语言(Data Manipulation Language)DCL数据控制语言(Data Control Language)DQL: 数据查询语言(Data Query Language)DDL: 数据定义语言(Data Definition Language) in ser t:插入(增) de le te:删除(删) up da te:修改(改) se le c t:查询(查) in to:到哪里。。。 fro m:从哪里来。。。 va lue:值 u nio n:拼接 wh ere:在。。。。。 se t:设置 trun ca te:截断 im por t:导入 ex por t:导出 Or der:顺序 Or der by:排序 Re p la ce:替换 Now:现在 Cei ling:天花板 F loo r:地板 Si gn:符号 Con ver t:转换 Di s tin c t:独特的(去重复的) Li mi t:限制 li ke:像。。。一样 be t wee n 。。。and:在。。。。和。。。。之间 su m:和 avg:平均值

C_连接sql数据库执行简单的增删改查操作

以users表为例,有三个字段,自增长的编号id,int类型;名称name,nvarchar 类型,密码pwd,nvarchar类型 首先在vs2005中引入using System.Data.SqlClient;命名空间 ///

/// 增加 /// /// 姓名 /// 密码 /// public int Insert(string name,string pwd) { SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=. conn.Open(); string sql = "insert into users(name,pwd) values(@name,@pwd)"; SqlCommand cmd = new SqlCommand(sql,conn); SqlParameter parn = new SqlParameter("@name",name); cmd.Parameters.Add(parn); SqlParameter parp = new SqlParameter("@pwd", pwd); cmd.Parameters.Add(parn); int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功 conn.Close(); cmd.Dispose(); return result; } /// /// 删除 /// /// 姓名 /// 密码 /// public int Update(int id) { SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True");//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.

相关主题
文本预览
相关文档 最新文档