zoukankan      html  css  js  c++  java
  • 数据库命令-实战

    1、创建数据库

      create database 库名   

    #显示数据库

    show databases;

    2、删除数据库

      drop database 库名

    3、切换数据库

      use 库名

    4、创建表格

      create table 表名

    5、删除表

      drop table 表名

    6、增

    1)单条

       insert into 表名 values(值1,值2

    2)批量

       insert into 表名 select 1,值2 union select 1,值2

    3)提取数据到新表

       select 字段1,字段2 into 新表 from 旧表 where 条件

    4)提取数据到已存在的表

       insert into 目的表 select 字段1,字段2 from 源表 where 条件

    7、删

    1)删除表

      drop table

    2)删除表里边的数据

      delete from

    3)删除表里的部分数据

      delete from where 条件

    8、改

      update 表名 set 字段=新值 where 条件

    9、查

    1)查询所有

      select * from 表名

    2)查询部分字段

      select 字段1,字段2 from

    3)以条件查询

      select 字段1,字段2 from where 条件

    4)表中前3条数据

      top 3

    5)排序

      order by 字段 (asc/desc


    SQL Server-2008  查询所有字段

    alter table stud add tel int  (添加列

     

    alter table stud drop column tel  (删除stud表里的column

     

    alter table stud alter column tel varchar(20)   (修改列的类型)

    注:只能修改列的类型,不能修改其他项

    select * from stud    -----(查询stud表里的所有数据)

     

    select sname,age from stud   ----(查询stup表中的姓名和年龄)

     

    select * from stud where sname like '%'  ----(查询stud表里姓名一栏里所有姓张的(名字))

     

    select sname,sex,age from stud where sname like '%'   ----(查询stud表里面所有姓张的名字,性别和年龄)

     select * from stud where sname='张三'  ----(查询stud表里面姓名是“张三”的所有信息)

     

    select * from stud where age>25   -----(查询stud表里年龄大于25岁(人)的所有信息)

     

    select * from stud where age>25 and age<30  ------(查询stud表里面年龄大于25和小于30岁(人)的所有信息 )

    select * from stud where age between 25 and 30  ----(查询stud表里面年龄在2530岁之间人的所有信息)(between:之间))

    select * from stud where age>25 and sex=''   ----(查询stud表里面年龄大于25岁和性别是“男”的所有信息)

    select * from stud where address='北京' or address='上海'    ----(查询stud表里面住址在北京或是在上海的(所有人的)信息)

     

    select * from stud where email like '%@163.com'    ----查询stud表里面邮箱地址尾号是@163.com所有信息)

    select * from stud where marry is null(查询stud表里面结婚一项为空的所有信息*marry结婚

     

    select * from stud where marry is not null     ----(查询stud表里面结婚一项非空的所有信息)

     

    select * from stud where email like '[x-z]%'     ----(查询stud表里面邮箱地址首字母xz之间的所有信息)

    select sname '姓名',sex 性别,age as 年龄 from stud --(把stud表里面查询时表头显示的姓名,性别,年龄 翻译成中文显示出来)。

    select top 3 * from stud     ----(查询stud表里面前3行所有信息)(top:顶部)

     

    select top 3 sname from stud     ----(查询stud表里面前三行的名字)

    select * from stud order by age asc  from stud  ---(在stud表里面按照年龄字段给所有信息做升序排序)

    select top 3 * from stud order by age asc    ----(在stud表里面给年龄字段的前3条信息做升序排序)

    select top 5 * from score order by sc desc     ----(在score表里面给成绩的前5条信息作降序排序)

    select * from score  ----(查询score表里的所有信息)

     

    select sname,age into girl from stud      ----(提取stud表里面的姓名,年龄,未婚的

    where marry=0 and sex=''             和性别为“女”的放到新表girl里)

               

    select * from girl  ----(查看girl表里的所有数据)

     

    select * from stud  ----(查看stud表里的所有数据)

     

    insert into girl

    select sname,age from stud where sid=3  ----(提取stud里面 学号的第三行里的姓名和年龄放到新表gril里)

    update girl set sname='a4' where age=23  ----改表(把girl表里面的年龄是23的那个姓名改成叫“a4”)

    update stud set marry=0 where marry is null  ----(把stud表里面结婚为空的改成未婚)

     

    select * from score   ----(查询score表里的所有数据)

     

    update score set sc=90 where sid=1 and cid=1  ----(把score表里面学号的第一行和编号的第一行改成90

    select * from girl  ----(查看girl表里的所有数据)

     

    delete from girl where age<25  ----(删除girl表里面年龄小于25的(年龄)数据)

     

    select * from stud  ----(删除stud表里面的所有数据)

     

    select COUNT(*) from stud  ----(统计stud表里里面的所有数据)

     

    select COUNT(*) from stud where sex=''  ----(统计stud表里面性别是“女”的数据)

     

    select COUNT(*) from stud where address is not null  ----(统计stud表里面地址非空的数据)

     

    select COUNT(address) from stud  ----(统计stud表里面的地址)

     

    select COUNT(address) from stud where sex=''  ----(统计stud表里面女性的地址)

     

    select SUM(age) from stud  ----(求stud表里面的年龄之和)

     

    select * from score  ----(查看score表里的所有数据)

     

    select AVG(sc) from score  ----(求score表里面成绩的平均数)

     

    select MAX(sc) from score  ----(求score表里面成绩的最大值)

     

    select MIN(sc) from score  ----(求score表里面成绩的最小值)

     

    select sid,avg(sc) from score group by sid  ----(求score表里面sid组里的平均分)

     

    select cid,sum(sc) from score group by cid  ----(求score表里面cid组里分数的和)

     

    select sid,avg(sc) from score group by sid   ----(求score表里面sid组里分数大于70

    having avg(sc)>70                         的平均分)(√)

     

    (前面有分组字段,后面有聚合函数,就只能用having代替where

     

    select sid,AVG(sc) from score where sc>70 group by sid  ----(求score表里面sid组里分数大于70分的平均分)

    select distinct sid from score where sc>80  ----(去掉score表里面成绩大于80分的重复项) (distinct:去重)

    select getdate()  ----(查询日期)getdate:取得日期/时间信息

     

    select DATEDIFF(yy,'1999-1-1','2009-1-1')    ----(取年,计算求差值) ( 方式一)

     

    select DATEDIFF(mm,'1999-1-1','2000-1-1')  ----(取月,计算求差值)  

     

    select DATEDIFF(dd,'1999-1-1','1999-2-1')   ----(取日,计算求差值)

     

    select DATEPART(yy,getdate())  ----(取年,取出日期)              (方式二)

     

    select DATEPART(mm,getdate())  ----(取月,取出日期)

     

    select DATEPART(dd,getdate())  ----(取日,取出日期)

     

    select YEAR('1999-9-10')  ----(具体日期)                         (方式三)

     

    select MONTH(getdate())  ----(具体日期,月)

     

    select DAY(GETDATE())  ----(具体日期,日)

     

    select * from stud where DATEPART(mm,birth)>6  ----(查询大于6月份的所有数据)

     

    select 1+2  ----(用select打印1+2)  两种方法

    print 1+2  ----(用print打印1+2

     

    select 'qwe'+'asd'  ----(用select打印字符串+字符串)varchar20

     

    select 'the number is ' + convert(varchar(10),5) 

    (打印varchar20)类型的字符串加int类型的数字,需要先将两者数据类型转换成

    统一数据类型,才能打印出结果)

     

    select 'today is ' + convert(varchar(30),GETDATE(),110)  ----(只有convert数据类型转换日期类型的时候,后面才能支持多一个格式,除了日期类型后面不能有格式)

     

    select  LEN('abc')  ----(打印字符串个数) LEN:求字符长度

     

    select LEN('abc')  ----(打印汉字和字符串个数)注:一个字符和一个数字或一个汉子在这里都只占一个字节

     

    Select  RIGHT('我是中国人',3)  ----(右取3个,结果是“中国人”)RIGHT:右取

     

    Select  LEFT('我是中国人',3)  ----(左取3个,结果是“我是中”) LEFT:左取

     

     

    update stud set sname='张六六' where sid=4  ----(把stud表里学号的第4行姓名改成“张六六”)

     

    update stud set sname=''+RIGHT(sname,LEN(sname)-1)

    where sname like '%'

    (把stup表里面姓张的名字连名带姓的取出来并用LEN求其长度,再以(RIGHT)右取的方式减去姓氏只把名字取出来,之后把姓氏用(update)更改成姓“钱”,最后姓名(sname)就叫钱六六了)

     

    update stud set sname=''+RIGHT(sname,LEN(sname)-1)  ----()

    (更   改) (姓名=“钱”)(右取)(姓名,求长度,(姓名)减“姓”)

    where sname like '%'  ----()

    (条件)(姓名)(张……)

     

     

     

  • 相关阅读:
    animation循环滚动
    <canvas>简单学习
    月末总结
    回顾-总结(2)
    初识正则
    学习中小项目遇到事
    在炎热的夏天学习以提高效率
    前端存储之cookie、localStorage
    总结(1)
    jQuery
  • 原文地址:https://www.cnblogs.com/lslymc/p/14263886.html
Copyright © 2011-2022 走看看