MYSQL學習03--MySQL表操做、錶鏈接

1. MySQL表數據類型

  • 數值類型
  • 日期和時間類型
  • 字符串類型

2. 用SQL語句建立表

  • 基本語法
    CREATE TABLE table_name (column_name column_type);
  • 建立實例
    CREATE TABLE IF NOT EXISTS `courses`( `cid` INT UNSIGNED AUTO_INCREMENT, `student_name` VARCHAR(40) NOT NULL, `course_name` VARCHAR(40) NOT NULL, PRIMARY KEY (`cid`) )ENGINE = INNODB DEFAULT CHARSET= utf8;html

    3. 用SQL語句向表中添加數據

  • 基本語法
INSERT INTO table_name ( field1, field2,...fieldN )  
                       VALUES  
                       ( value1, value2,...valueN );
  • 插入實例
    INSERT INTO salary(s_name,s_sex,salary) VALUES("張三","m",2500);

4. 用SQL語句刪除表

  • 基本語法
    DROP TABLE table_name ;
  • 刪除表的幾種方式的不一樣
  1. DROP
    DROP TABLE table_name ;
  2. TRUNCATE
    truncate table student;
  3. DELETE
    delete from student where T_name = "張三";

5. 用SQL語句修改表

  • 修改列名mysql

  • 修改表中數據sql

  • 刪除行數據庫

  • 刪除列code

  • 新建列htm

  • 新建行blog

6. MySQL別名

  • INNER JOINci

  • LEFT JOIN字符串

  • CROSS JOINget

  • 自鏈接

  • UNION

  • 以上幾種方式的區別和聯繫

    0七、做業

  • 項目3、超過5名學生的課
    建立以下所示的courses 表 ,有: student (學生) 和 class (課程)。
CREATE TABLE IF NOT EXISTS `courses`(  
    `cid` INT UNSIGNED AUTO_INCREMENT,  
    `student_name` VARCHAR(40) NOT NULL,  
    `course_name` VARCHAR(40) NOT NULL,  
    PRIMARY KEY (`cid`)  
)ENGINE = INNODB DEFAULT CHARSET= utf8;

插入數據 :

INSERT INTO courses(student_name,course_name) VALUES("張三","math");
INSERT INTO courses(student_name,course_name) VALUES("李四","English");
INSERT INTO courses(student_name,course_name) VALUES("王五","math");
INSERT INTO courses(student_name,course_name) VALUES("趙六","Biology");
INSERT INTO courses(student_name,course_name) VALUES("五天","math");
INSERT INTO courses(student_name,course_name) VALUES("劉八","math");
INSERT INTO courses(student_name,course_name) VALUES("孫慧","math");
INSERT INTO courses(student_name,course_name) VALUES("王偉","Computer");
INSERT INTO courses(student_name,course_name) VALUES("鄭一","math");
INSERT INTO courses(student_name,course_name) VALUES("陳二","math");
INSERT INTO courses(student_name,course_name) VALUES("張一","Computer");
INSERT INTO courses(student_name,course_name) VALUES("張七","math");
INSERT INTO courses(student_name,course_name) VALUES("周天","English");

編寫一個 SQL 查詢,列出全部超過或等於5名學生的課,同一個學生不被重複計算。

SELECT course_name  
FROM   
    (SELECT DISTINCT student_name,course_name   
        from courses) aaa  
GROUP BY course_name  
HAVING COUNT(course_name)>5;

結果如圖:

  • 項目4、交換工資
    建立一個 salary表,以下所示,有m=男性 和 f=女性的值 。
CREATE TABLE IF NOT EXISTS `salary`(  
    `sid` INT UNSIGNED AUTO_INCREMENT,  
    `s_name` VARCHAR(40) NOT NULL,  
    `s_sex` VARCHAR(2) NOT NULL,  
    `salary` INT NOT NULL,  
    PRIMARY KEY (`sid`)  
)ENGINE = INNODB DEFAULT CHARSET= utf8;

插入數據:

INSERT INTO salary(s_name,s_sex,salary) VALUES("張三","m",2500);  
INSERT INTO salary(s_name,s_sex,salary) VALUES("李四","f",1500);  
INSERT INTO salary(s_name,s_sex,salary) VALUES("王五","m",5500);  
INSERT INTO salary(s_name,s_sex,salary) VALUES("趙六","f",500);  
INSERT INTO salary(s_name,s_sex,salary) VALUES("吳八","m",3500);

交換全部的 f 和 m 值:

UPDATE salary SET s_sex =IF(s_sex ="m","f","m");
  • 項目5、組合兩張表
    在數據庫中建立表1和表2,並各插入三行數據(本身造):
CREATE TABLE IF NOT EXISTS `person`(  
    `person_id` INT UNSIGNED AUTO_INCREMENT,  
    `first_name` VARCHAR(40) NOT NULL,  
    `last_name` VARCHAR(40) NOT NULL,  
    PRIMARY KEY(`person_id`)  
)ENGINE=INNODB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS `address`(  
    `address_id` INT UNSIGNED AUTO_INCREMENT,  
    `person_id` INT UNSIGNED NOT NULL,  
    `city` VARCHAR(40) NOT NULL,  
    `state` VARCHAR(40) NOT NULL,  
    PRIMARY KEY(`address_id`),  
    FOREIGN KEY(`person_id`) REFERENCES person(`person_id`)  
)ENGINE=INNODB DEFAULT CHARSET=utf8;

插入數據:

INSERT INTO person(first_name,last_name) VALUES("jack","chen");  
INSERT INTO person(first_name,last_name) VALUES("tom","wang");  
INSERT INTO person(first_name,last_name) VALUES("cat","zhang");  
INSERT INTO person(first_name,last_name) VALUES("joy","chen");
INSERT INTO address(person_id,city,state) VALUES(1,"hubei","wuhan");  
INSERT INTO address(person_id,city,state) VALUES(2,"hunan","changsha");  
INSERT INTO address(person_id,city,state) VALUES(3,"sanxi","san");  
INSERT INTO address(person_id,city,state) VALUES(4,"beijing","canpin");

編寫一個 SQL 查詢,知足條件:不管 person 是否有地址信息,都須要基於上述兩表提供 person 的如下信息:FirstName, LastName, City, State:

SELECT p.person_id,p.last_name,p.first_name,a.city,a.state   
from person p   
LEFT JOIN address a   
on p.person_id =a.person_id;
  • 項目6、刪除重複的郵箱
    編寫一個 SQL 查詢,來刪除 email 表中全部重複的電子郵箱,重複的郵箱裏只保留 Id 最小 的那個。
    建表省略。
DELETE FROM email where ID NOT IN (  
SELECT minid FROM(  
SELECT MIN(ID) as minid  
FROM email   
GROUP BY email) b  
);

參考:http://www.runoob.com/mysql/mysql-select-query.html

相關文章
相關標籤/搜索