SQL语句基础

1、创建数据库

  • create database database_name;

2、删除数据库

  • drop database database_name;

3、创建表

  • A:使用旧表创建新表
  • create table new_table_name like old_table_name;
  • B:使用旧表中查询出来的数据创建表
  • create table new_table_name as select col1,col2…coln from old_table_name only;

4、删除新表

  • drop table table_name;

5、增加一列

  • alter table table_name add column col_name type;

6、添加主键

  • alter table table_name add primary key(col_name);

7、编辑索引

  • A:创建索引:
  • create index index_name on table_name(col_name_01,col_name_02);
  • B:删除索引:
  • drop index index_name on table_name;

(备注:索引不可更改,要想更改只能删除后重新添加)

8、几个基本的SQL语句

  • 选择:select col_name from table where 条件;
  •   插入:insert into table(col_name,col_name01,col_name02…)values(value01,value02,value03…);
  •   删除:delete from table where 条件;
  •   更新:update table set col_name=value_new where 条件;
  • 查找:select * from table_name where col_name like ‘%value%’;
  • 排序:select * from table_name order by col_name;
  • 总数:select  count as totalcount from table_name;
  • 求和:select sum(col_name) as sumvalue from table_name;
  • 平均:select avg(col_name) as avgvalue from table_name;
  • 最大、最小:max(col_name)   min(col_name) ;

9、使用外连接

  • A:left join 左连接:结果集包括连接表的匹配行,还包括左连接表的所有行
    select a.a,a.b,a.c,b.c,b.d,b.f from a left out join b on a.a=b.c
  • B:right join 右连接:结果集包括连接表的匹配行,还包括有连接表的所有行
  • C:\full/cross join 全连接:结果集包括连接表的匹配行,还包括两个连接表的所有记录

10、数据库分组

group by

11、修改数据库名称

sp_renamedb ‘old_db_name’ ‘new_db_name’;

posted @ 2016-03-03 13:50  栗子玄机  阅读(556)  评论(0编辑  收藏  举报