数据库(二)

摘要:
--将数据库的名称更改为xueshengsp_Renamedstudent,xuesheng Add column:Altetable table name Add column column column-type--修改表,添加新列。注意,当它与内置词冲突时,列名由[]altertablexinxiadd[int]varcharaltertablexinXiaddnianningint删除列:altertable table name dropcolumn Column name--修改表以删除列altertable xinxidropcolumn[int]修改列类型:altertable tablename altercolumn name new type插入添加(添加)应用程序:表结构保持不变,只有一行可以添加,或者一个值不能自增长时可以赋值,列名用逗号分隔。默认情况下,值的顺序是表的顺序。如果输入的值不完整,可以在前面添加要输入的列名,并且顺序与值一一对应。

修改表格  

如果SQL server 2008中无法修改表结构,提示错误为:不允许保存修改,……

解决方案:工具→选项→左侧的Designers→表设计器和数据库设计器

去掉“阻止保存要求重新创建表的更改”前面的钩,重新启动系统。

--修改数据库的名字将student的名字修改成xuesheng
sp_renamedb student,xuesheng

增加列:  

Alter table 表名 add 列名 列类型

--修改表,新加入列,注意与内置单词冲突的时候,列名加[]括起来
alter table xinxi add [int] varchar(10)
alter table xinxi add nianling int

删除列: 

alter table 表名 drop column 列名

--修改表删除一列
alter table xinxi drop column [int]

修改列的类型: 

alter table 表名 alter column 列名 新类型

Insert 增(添加) 

应用:表结构不变,只能增加一行或某个值在不是自增长的情况下才能赋值,列名用逗号隔开,值的次序默认为表的次序,如果输入的值不全,可在前面加需要输入的列名,顺序一一对应values值。

--插入数据
insert into xinxi values(1,'张三',96)
insert into xinxi values(2,'李四',91)
insert into xinxi values(3,'王五',69)

Delete 删除 

Delete from 表名       删除表内容(表结构还在)这种删除方式会写日志,所以自增长的序列号会往下延续,不断增加不会从头开始

Truncate table 表名          此删除将表清空,速度快,不写日志,故再输入从头开始

Delete from 表名 where 列名 关系表达式 值            多条件可以加and或or

列名between值1 and 值2 等同于列名<=值2 and 列名>=值1

列名 in(值1,值2,值3,...) 筛选出值为值1或值2或值3...的选项

表中选中某一数据值 按 ctrl+0 此值变为null

Update 改、更新 

Update 表名 set 列名=值,列名=值,…… where 列名 关系表达式 值

update xinxi set fenshu=100 where code=6

Retrieve 检索、查询 

select *from 表

select 列名,列名,…… from 表

select *from 表 where 列名 关系运算符 值 and 列名 关系运算符 值

select *from 表 where 列名 between 1 and 100  (范围查询)

select *from 列名 where 列名 in(3,4,5)

select distinct 列名 from 表    (列去重)

select *from 列名 where name like %5%       %任意多个任意字符;_一个任意字符

---查询语句,条件查询
select *from xinxi
select fenshu,name from xinxi
select fenshu,name from xinxi where name='李四'

select *from xinxi where fenshu between 80 and 100--范围
update xinxi set nianling = 26 where fenshu between 80 and 100
select distinct name from xinxi--针对一列去重显示

update xinxi set name='李四' where code = 9
select *from xinxi where name='李四' and nianling =26
select *from xinxi where name='李四' or nianling =26
select *from xinxi where name in ('李四','赵六')
select *from xinxi where name not in ('李四','赵六')
--模糊查询名字里面带四的,通配符%表示任意很多字符
select *from xinxi where name like '%四%'
--下划线表示任意一个字符
select *From xinxi where name like '李_'
--下划线加中括号,等同于in的功能,任意一组满足就查询出来
select *from xinxi where name like '_[李四,赵六,田七]'

筛选 

Select *from 表名 where 列名 关系表达式 值

去重 

Select distinct 列名 from 表名        去除这一列的重复值

模糊查询 

Select *from 表名 where 列名 like '王%'

通配符:%:任意多个字符;_:一个任意字符;[4,5,6]:中括号代表选里面的值其一

排序 

Select *from 表名 order by 列名 asc (升序) 或 desc (降序)

--按年龄排序,asc升序,desc降序,默认不写是升序
select *from xinxi order by nianling asc
select *from xinxi order by nianling desc
--按降序排列分数后,查前三名
select top 3 *from xinxi order by fenshu desc
--按条件查询后排序,查名字叫李四的人谁的分数最高
select top 1 *from xinxi where name='李四' order by fenshu desc

免责声明:文章转载自《数据库(二)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇W: GPG error: http://ppa.launchpad.net trusty InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 8CF63AD3F06FC659试验go rod 及更改chrome浏览器缓存位置下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

数据库之索引与慢查询优化

一、介绍 1.什么是索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句的优化显然是重中之重。说起加速查询,就不得不提到索引了。 2.为什么要有索引呢? 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构。...

使用select实现非阻塞socket | dbafree首页

使用select实现非阻塞socket | dbafree首页 在linux,perl,python上都存在select系统调用。下面这两个python程序,可以用来学习和调试select操作。 参考至:http://blog.chinaunix.net/space.php?uid=199788&do=blog&id=99434 1.服务...

利用ADO操作外部数据——Excel之VBA(15)

引例: 在VBA里录制导入现有外部数据的宏查看宏代码  1 Sub 宏1() 2 ' 3 ' 宏1 宏 4 ' 5 6 ' 7 Application.CutCopyMode = False 8 With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(...

SQL数据库面试题

Database工程师面试 问题描述:为管理岗位业务培训信息,建立3个表:S (S#,SN,SD,SA) S#,SN,SD,SA 分别代表学号、学员姓名、所属单位、学员年龄C (C#,CN ) C#,CN 分别代表课程编号、课程名称SC ( S#,C#,G ) S#,C#,G 分别代表学号、所选修的课程编号、学习成绩要求实现如下5个处理:1. 使用标准SQ...

HIVE优化学习笔记

概述 之前写过关于hive的已经有两篇随笔了,但是作者依然还是一枚小白,现在把那些杂七杂八的总结一下,供以后查阅和总结。今天的文章介绍一下hive的优化。hive是好多公司都在使用的东西,也有好多大公司进行定制化二次优化,比如鹅厂的Thive等。所以学习hive至关重要,本文只针对大众版免费开源的hive。官网地址:http://hive.apache.o...

Mysql 查询优化,索引原理与慢查询优化。

Mysql 查询优化 索引原理与慢查询优化 一 介绍 为什么要有索引? 一般的应用程序,读写比例在10:左右,而且插入操作和一般的更新操作很少出现性能问题, 在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询, 我们必定要去优化它们,那我们怎么去优化呢?那就要提到索引了。 什么是索引? 索引在MySQL中也叫做“...