zoukankan      html  css  js  c++  java
  • SQL关键词及语句

    T-SQL语句:

    创建表:create table name( code varchar(50),)

    主键:primary key

    非空:not null

    自增长:auto_increment

    外键:references

    删除表: drop table family

    创建数据库:create database mydb

    删除数据库:drop database mydb

    CRUD操作:

        C: create
        增加数据:
        insert into nation values('n001','汉族')--普通添加

        insert into nation values('','','')  -- 自增长列也要添加

        insert into nation (code) values ('n002')--往表中添加特定列的数据
        
        R: read
        
        查询数据:

        1、查询所有数据   select * from info

        查特定列  select code,name from info

        2、根据条件差

        select * from info where code='p001'  一个条件查询

        select * from info where code='p001' and nation ='n001'  多条件 并关系  查询

        select * from info where code='p001' or nation = 'n001'  多条件  或关系 


        select * from car where price>=50 and price <= 60    范围查询

        select * from car where price between 50 and 60

        3、模糊查询

        select * from car where name like '%型'    已型为结束的数据 ------》  %通配符代表任意多个字符

        select * from car where name like'%奥迪%'

        select * from car where name like'_马%'

        4、排序

        select * from car order by price asc    按照价格升序排列(默认)asc 可以省去不写

        select * from car order by price desc   按照价格降序排列

        select * from car order by price desc ,oil desc   按照两列进行排序  前面的为主要

        5、统计函数(聚合函数)

        select count(code) from car   查询表中有多少条数据

        select max(price) from car    取价格中最大值

        select min(price) from car    取价格最小值

        select sum(price) from car     取价格的总和

        select avg(price) from car    取价格的平均值

        6、分组查询

        select * from car group by brand

        select brand from car group by brand        

        select count(brand) from car group by brand     获取分组每组的数量

        select brand from car group by brand having count(*)>2   分组后筛选数量大于2的

        7、分页查询

        select * from car limit m,n    跳过m条数据去n条数据
        
        8、去重查询

        select distinct  brand from car 











        U:update
        修改数据:
        update nation set name = '回族'  ---全部修改

        update nation set name = ‘汉族’ where code = 'n002' --修改某一条数据

        update nation set code = 'n003' where name = '汉族'
        D:delete

        删除数据:

        delete from nation--删除整个表中的数据

        delete from nation where code = 'n001'   删除一条数据,已主键查询删除一条,如果以别的数据查询可能删除多条--重名。

  • 相关阅读:
    康复计划
    Leetcode 08.02 迷路的机器人 缓存加回溯
    Leetcode 38 外观数列
    Leetcode 801 使序列递增的最小交换次数
    Leetcode 1143 最长公共子序列
    Leetcode 11 盛水最多的容器 贪心算法
    Leetcode 1186 删除一次得到子数组最大和
    Leetcode 300 最长上升子序列
    Leetcode95 不同的二叉搜索树II 精致的分治
    Leetcode 1367 二叉树中的列表 DFS
  • 原文地址:https://www.cnblogs.com/fangchongyan/p/5132267.html
Copyright © 2011-2022 走看看