SQL

一、数据库定义语言DDL

      1、创建表

           create table  tableName  (     // tableName可以自己定义作为表名

                      sid   char(10),

                      age  smallint,

                     primary key(sid) );

     

          create table  tableName(

                      sid   char(10) primary key,

                      age  smallint  );


      2、删除表

            drop  table  tableName;


      3、修改表   Alter

           I、增加属性

              alter  table tableName

              add  sex  char(1);

           II、修改属性          

             alter  table tableName

              alter  age  int;

           III、删除属性          

             alter  table tableName

             drop age;


二、数据库操纵语言DML

        I、添加数据

         insert  into  tableName (name,age)       //如果下面的数据与表头的属性一致,则可以省略上面的属性名

         values('zhangsan',19);

       II、删除数据

          delete  from  tableName;    //删除表的全部数据

          delete  from  tableName     //删除表中名字为zhangsan的数据

          where  name='zhangsan';

       III、查询

       1、select语句

              select  name

              from   tableName;

        

        2、where 语句  

              select  name

              from   tableName

              where sid='2014010113';

             where语句中可以使用逻辑连接词and, or, not。逻辑连接词的运算对象可以包含<, >, <=, >=, =, <>的表达式

   

         3、自然连接

              select  *                                  //*为输出所以属性的值

              from   tableName natural join tableName1;

               //对以上的改进,避免相同的属性名进行自然连接 

              select  *                                  //*为输出所以属性的值

              from   tableName  join tableName1 using(sid);

         

          4、笛卡尔积

              select  *

              from   tableName , tableName1;

           

         5、as    可以给属性或者表进行更名,Oracle数据库不需要写出as

              select a.name   as  b                 

              from   tableName as a ;

        

           6、distinct   all            

              select distinct name                 //去除重复的数据

              from   tableName ;

              

             select all name                       //保留重复的数据

              from   tableName ;



原文地址:https://www.cnblogs.com/maokun/p/6736459.html