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
INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN );
INSERT INTO salary(s_name,s_sex,salary) VALUES("張三","m",2500);
DROP TABLE table_name ;
DROP TABLE table_name ;
truncate table student;
delete from student where T_name = "張三";
修改列名mysql
修改表中數據sql
刪除行數據庫
刪除列code
新建列htm
新建行blog
INNER JOINci
LEFT JOIN字符串
CROSS JOINget
自鏈接
UNION
以上幾種方式的區別和聯繫
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;
結果如圖:
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");
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;
DELETE FROM email where ID NOT IN ( SELECT minid FROM( SELECT MIN(ID) as minid FROM email GROUP BY email) b );