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'   删除一条数据,已主键查询删除一条,如果以别的数据查询可能删除多条--重名。






原文地址:https://www.cnblogs.com/youshashuosha/p/5130687.html