mysql數據庫語法——(六)


1、創建數據庫:

      create database myschool;

2、刪除數據庫:

      drop database myschool;

3、創建表:

      create  table  [if  not  exists]   表名(

             字段1       數據類型    [字段屬性|約束]   [ 索引 ]  [ 注釋],

              ……

      ) [ 表類型 ] [表字符集] [注釋];

      create  table `student`(

              `studentNo`     int(4)      not null      comment '學號'   primary   key,   #非空,主鍵

               `name` char(10),

               ……

     ) comment="學生表"; 

4、添加字段

      alter  table  demo02   add `password`   varchar(32)   not   null;

5、修改字段

      alter  table 表名 change  原字段名  新字段名  數據類型  [ 屬性 ];

      alter table  demo02  change `name` `username` char(10) not null;

6、設置默認值約束

      alter  table  grade  alter  column   gtype   set  default  '一年級';

7、刪除字段

      alter  table  表名  drop  字段名

      alter  table  demo02  drop  `password`;

8、添加主鍵約束

      alter  table  表名  add  constraint  主鍵名  primary  key  表名(主鍵字段);

      alter  table  `grade`  add   constraint  `pk_grade`  primary  key  `grade`(`gradeId`);  

9、添加外鍵約束

      alter  table  表名  add  constraint   外鍵名   foreign   key  (外鍵字段)  references  關聯表名(關聯字段);

      alter  table  `student`  add  constraint  fk_student_grade  foreign  key(`gradeId`)  references  `grade` (`gradeId`);

10、添加檢查約束

        alter  table  test  add constraint  CK_test_tprice  check( tprice > = 100);

11、插入數據

        insert  into  `subject`(`subjectName`,`classHour`,`gradeId`)

        values('logic java',220,1),('HTML',160,1);

12、將查詢結果插入到新表

        create   table  新表(select  字段1,字段2,……FROM  原表);

        create  table `phoneList`(select `studentName,`phone` from `student`);

13、更新數據

        update  表名  set  列名  =  更新值 [ where  更新條件];

        update student  set  sex = '女'   where  id = 1;

14、刪除數據

         delete  [ from ]  表名 [ where <刪除條件>];

         delete  from  student  where   id  =  1;

15、使用select語句進行查詢

        select  <列名|表達式|函數|常量>

        from <表名>

        [where  <查詢條件表達式>]

         [order  by  <排序的列名>    [ASC或DESC] ];

       select  studentNo  AS  學生編號,studentName  AS 學生姓名, address AS  學生地址

       from   student 

       where  id = 1;

       或者:

        select firstName+'.' + lastName AS 姓名  FROM  employee;

16、 查詢空值

        select   studentName   from  student  where  email  is  null ;

17、在查詢中使用常量列

        select  studentName  AS 姓名,address AS  地址, '北京' AS 學校名稱 from  student;

18、 聚合函數 

        AVG(  )

        COUNT(  )

        MAX(  )

        MIN(  )

        SUM(  )

        select  sum(studentResult)  from  result;

        select  AVG(studentResult)  from  result;

19、字符串函數

       CONCAT(str1,str2,……,strn)     select    CONCAT('MY','S','QL');     返回:MYSQL

       INSERT(str,pos,len,newstr)       select    INSERT('這是Oracle數據庫',3,6,'MySQL');     返回:這是MySQL數據庫

       LOWER(str)    select  lower('MYSQL');     返回:mysql

       upper(str)     

       substring(str,num,len)   select  substring('JavaMySQLOracle',5,5);         返回:MySQL

20、時間日期函數

      select  datediff(now(),'2008-8-8');      返回3327,返回日期參數date1和date2之間相隔的天數

      select   adddate(now(),5);

21、數學函數:

       CEIL(x)      返回大於或等於數值x的最小整數   select ceil(2.3)   返回:  3

       FLOOR(x)    返回小於或等於數值x的最大整數     select  floor(2.3)   返回:2

22、order  by  字句

       ASC:升序   DESC:降序

       例:要在學生成績排序的基礎上,再按照課程ID進行排序的語句如下:

               select   studentId   AS  學生編號,studentResult  AS  成績,courseID  AS  課程ID,

                from   result

                whre  studentResult>60

                order  by  studentResult,courseID;

 23、group   by  分組

        select  <列名|表達式|函數|常量>

        from <表名或視圖>

        [where  <查詢條件表達式>]

        [group  by <分組的字段名>]

         [order  by  <排序的列名>    [ASC或DESC] ]

         [LIMIT [位置偏移量,] 行數];

        多列分組查詢

        select  count(*)  AS  人數,grade  AS  年級, sex  AS 性別  from  student

        group  by  grade ,sex

        order by grade;

24、簡單子查詢

        select  `studentNo`,`studentName`,`sex`,`bornDate`,`address`  from  `student`  

        where  `bornDate` >

         (select  `bornDate`  from  `student`  where   `studentName` = '李斯文');

25、IN和NOT IN 子查詢

       查詢Logic  Java課程至少一次考試剛好等於60分的名單:

       select  `studentName`  from  `student`

       where  `studentNo`  IN(

             select  `studentNo`  from  `result`

             where   `subjectNo` =(

                         select    `subjectNo`  from  `subject`

                         where  `subjectName` = `Logic Java`

            ) and  `studentResult` = 60

       );

26、EXISTS子查詢

        EXISTS關鍵字后面的參數是一個任意的子查詢,如果該子查詢有返回行,則EXISTS子查詢的結果為true,此時再執行外層查詢語句。如果子查詢沒有返回行,則EXISTS子查詢的結果為false,此時外層語句不再執行查詢。

        檢查Java課程最近一次考試。如果有成績達到80分以上者,則顯示分數排在前5名學員的學號和分數。

         select  `studentNo`    AS  學號,`studentResult`  成績  from   `result`

          where  exists (

               #查詢Logic Java最后一次考試成績大於80的記錄

               select   *   from  `result`   where  `subjectNo` = (

                      select  `subjectNo`   from   `subject`   where  `subjectName` =  'Logic  Java'

                ) AND    `examDate` = (

                          select  MAX(`examDate`)     FROM  `result`   where  `subjectNo` = (

                              select   `subjectNo`  from  `subject`

                              where  `subjectName` = 'Logic  Java'   

                           )

               ) AND  `studentResult` > 80

         )   AND `subjectNo` =  ( select  `subjectNo`  from  `subject`  where `subjectName` = 'Logic Java')

         ORDER  BY `studentResult`  DESC  LIMIT  5;

27、使用HAVING字句對分組后的數據進行篩選

       select   count(*)   AS  人數, gradeId   AS  年級   from   student

       group   by   gradeId

       having   count(*) > 2;

28、內連接查詢

       select  student.studentName,result.subjectNo,result.studentResult

        from  student,result

        where student.studentNo = result.studentNo;

        或者:

        select  s.studentName,r.subjectNo,r.studentResult

         from  student AS s

          INNER  JOIN result AS r  on(s.student.No = r.studentNo);

29、 左外連接查詢

        select   s.studentName,r.subjectNo,r.studentResult

        from  student  AS  s

        left  outer  join result as r  on s.studentNo = r.studentNo;

 

          


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM