插入数据 选择列插入数据
INSERT INTO student(id,sname,gender) VALUES(3,"小强",0);
反应
(1 row(s) affected)
Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000
插入数据 全列插入
INSERT INTO student VALUES(6,"吕小强",1)
添加数据 添加多个数据
INSERT INTO student VALUES(7,"吕小强",1),(9,"刘小备",0),(12,"aaa",1);
小结 增
非全列的添加数据
insert into 表名(列1,列2,列n) values(值1,值2,值n);
全列添加数据
insert into 表名 values(值1,值2,值n);
添加多个数据
insert into 表名 values(值1,值2,值n),(值1,值2,值n),.....;
删除指定的数据
DELETE FROM student WHERE id=9;
删除所有数据
一行一行的删除数据
delete from 表名
一次性删掉整个表 然后重建一个同样的表
truncate table 表名
修改数据
UPDATE student SET sname = "大装" WHERE id = 3;
格式
update 表名 set 列1=值1,列n=值n where 条件
演练 学生表与老师表
CREATE DATABASE hpSchool CHARACTER SET utf8;
USE hpSchool;
CREATE TABLE teacher(
id INT PRIMARY KEY AUTO_INCREMENT,
tname VARCHAR(5) NOT NULL,
skill VARCHAR(20),
joinDate DATETIME
);
DESC teacher;
INSERT INTO teacher VALUES(NULL,"卡卡西","雷切","2020-1-2");
SELECT * FROM teacher;
SHOW TABLES;
CREATE TABLE student(
id INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR(5) NOT NULL,
place VARCHAR(10) NOT NULL,
gender INT DEFAULT 1,
score INT,
birthday DATETIME,
registTime TIMESTAMP,
tid INT,
CONSTRAINT sb1234 FOREIGN KEY (tid) REFERENCES teacher(id)
);
DESC student;
INSERT INTO student(sname,place,tid) VALUES("小强","武汉",1);
SELECT * FROM student;
完整查询
基本查询
SELECT * FROM teacher;
查询并显示指定的列
SELECT id,tname,skill FROM teacher;
去重查询
SELECT DISTINCT place FROM student;
列与列的运算
查询 列与列的运算 ifnull
SELECT id,IFNULL(score,60),IFNULL(score,60)+id AS total FROM student;
起别名
|