1. 程式人生 > >MySQL筆記:數據插入

MySQL筆記:數據插入

opened tab play RR 分享 eat name med creat

一、插入記錄

技術分享圖片
CREATE DATABASE MYDB;
USE MYDB;

CREATE TABLE IDENTITY (
  NAME  CHAR(20) PRIMARY KEY,
  BIRTH DATETIME NOT NULL,
  HOME  CHAR(20)
);

CREATE TABLE STUDENT (
  NUMBER INT PRIMARY KEY AUTO_INCREMENT,
  NAME   CHAR(20) NOT NULL,
  AGE    TINYINT  NOT NULL,
  HOME   CHAR(30)
);

INSERT INTO STUDENT VALUES
(30, Curry, 10, Golden State); #不指定字段名 INSERT INTO STUDENT (NAME, AGE, HOME) VALUES (Durant, 11, Golden State); #指定字段名 INSERT INTO STUDENT (NAME, AGE, HOME) VALUES (Curry, 30, Golden State), (Durant, 29, Golden State), (James, 23, Cleveland), (Irving, 11, Boston); #插入多條記錄
INSERT INTO STUDENT (NAME, AGE, HOME) SELECT NAME, TIMEDIFF(NOW(), BIRTH), HOME FROM IDENTITY; #插入查詢結果 SELECT * FROM STUDENT; DROP DATABASE MYDB;
View Code

二、更新記錄

技術分享圖片
INSERT INTO STUDENT VALUES (35, Durant, 0, 29, Oklahoma);
UPDATE STUDENT
SET HOME = Golden State
WHERE NUMBER
= 35;
View Code


三、刪除記錄

技術分享圖片
DELETE FROM STUDENT
WHERE HOME = Golden State; #清除指定條件的數據
DELETE FROM STUDENT; #清空所有數據
View Code

MySQL筆記:數據插入