DML 语言
SQL 语言
- DDL语言 数据库定义语言
创 create 改 alter 删 drop - DML语言 数据库操作语言 对表中的数据进行操做
增 insert 删 delete 改 update
insert 插入操作
格式1: insert into 表名(列名1,列名2,…) values(values1,values2,…)
注意:插入值要与列保持一致
格式2:insert into 表名 values(值1,值2,…)
格式3:添加多条数据
Insert into 表名 values(值1,值2,…),(值1,值2,值3…)
CREATE TABLE Teacher(
Tno varchar(7) primary key,
Tname varchar(10) not null,
Tsex char(2) default '男',
Birthday datetime,
Dept varchar(20),
Sid char(18)
);
CREATE TABLE Student(
sno varchar(7) primary key,
sname varchar(10) not null,
ssex char(4) default '男',
Dept varchar(20)
);
INSERT INTO Student(sno,sname) values('123','sa');
desc Student;
INSERT INTO Student values('124','john','女','CS');
INSERT INTO Student values('125','amy','女','CS'),('126','sheldon','男','CS');
创建表格并插入以下数据
CREATE TABLE teacher(
id int,
name varchar(20),
address varchar(20),
salary int,
birthdate date
);
INSERT INTO teacher values(1,'王明','济南',1000,'1989-06-13');
INSERT INTO teacher values(2,'李琦','济南',2000,'1993-06-13');
INSERT INTO teacher values(3,'汪洋','济南',3000,'1989-08-13');
INSERT INTO teacher values(4,'李峰','济南',3500,'1989-09-13');
INSERT INTO teacher values(5,'张曼','',4500,'1989-06-13');
INSERT INTO teacher values(6,'尚温省','null',1000,'1989-09-13');
delete 删除操作(只是删除数据,并不删除表结构)
格式1:delete from 表名;(整个表中数据全部删除)
格式2:delete from 表名 where 条件;
SELECT * from Student;
--删除性别为男的学生
DELETE FROM Student where ssex='男';
and 多个条件同时满足 delete from 表名 where 条件1 and 条件2 and 条件3…
or 多个条件只需满足其中一个 delete from 表名 where 条件1 or 条件2 or 条件3…
DELETE FROM Student; --所有数据都被删除
注意外键约束影响
需要先删除主表记录,从表记录才可以删除
UpDate命令,修改表中数据
格式1:
update 表名 set 列名=值;整列的值都被改为设置的值
UPDATE teacher set id=1;
格式2:
update 表名 set 列名1=值1,列名2= 值2,…
UPDATE teacher set id=2,address='南京';
格式3:(有条件的更新)
update 表名 set 列名=值 where 条件;(多个列修改时还是用逗号隔开)
UPDATE teacher set salary=8700 where name='王明';
--薪水在现有条件加上500
UPDATE teacher set salary=salary + 500;
查询数据 163集
select 列名 from 表名;
注意:先读取from后的语句
CREATE TABLE `course` (
`Cno` VARCHAR(20) NOT NULL,
`Cname` VARCHAR(20) NOT NULL,
`Tno` VARCHAR(20) NOT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `course`(`Cno`,`Cname`,`Tno`) VALUES ('3-105','计算机导论','825'),('3-245','操作系统','804'),('6-166','数字电路','856'),('9-888','高等数学','831');
CREATE TABLE `score` (
`Sno` VARCHAR(20) NOT NULL,
`Cno` VARCHAR(20) NOT NULL,
`Degree` DECIMAL(4,1) DEFAULT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `score`(`Sno`,`Cno`,`Degree`) VALUES ('103','3-245','86.0'),('105','3-245','75.0'),('109','3-245','68.0'),('103','3-105','92.0'),('105','3-105','88.0'),('109','3-105','76.0'),('101','3-105','64.0'),('107','3-105','91.0'),('108','3-105','78.0'),('101','6-166','85.0'),('107','6-166','79.0'),('108','6-166','81.0');
CREATE TABLE `student` (
`Sno` VARCHAR(20) NOT NULL,
`Sname` VARCHAR(20) NOT NULL,
`Ssex` VARCHAR(20) NOT NULL,
`Sbirthday` DATETIME DEFAULT NULL,
`class` VARCHAR(20) NOT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `student`(`Sno`,`Sname`,`Ssex`,`Sbirthday`,`class`) VALUES ('108','丘东','男','1977-09-01 00:00:00','95033'),('105','匡明','男','1975-10-02 00:00:00','95031'),('107','王丽','女','1976-01-23 00:00:00','95033'),('101','李军','男','1976-02-20 00:00:00','95033'),('109','王芳','女','1975-02-10 00:00:00','95031'),('103','陆君','男','1974-06-03 00:00:00','95031');
CREATE TABLE `teacher` (
`Tno` VARCHAR(20) NOT NULL,
`Tname` VARCHAR(20) NOT NULL,
`Tsex` VARCHAR(20) NOT NULL,
`Tbirthday` DATETIME DEFAULT NULL,
`Prof` VARCHAR(20) DEFAULT NULL,
`Depart` VARCHAR(20) NOT NULL
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO `teacher`(`Tno`,`Tname`,`Tsex`,`Tbirthday`,`Prof`,`Depart`) VALUES ('804','李诚','男','1958-12-02 00:00:00','副教授','计算机系'),('856','张旭','男','1969-03-12 00:00:00','讲师','电子工程系'),('825','王萍','女','1972-05-05 00:00:00','助教','计算机系'),('831','刘冰','女','1977-08-14 00:00:00','助教','电子工程系');
Distinct 去重复
格式:select distinct 列名 from 表名;
select * from teacher;
select distinct Tsex from teacher;
别名查询(只是对查询出来的结果标题进行修改)
别名查询
格式:select 列名1 ‘别名1’,列名2 ‘别名2’,… from 表名
select Sno '学号' from student;
将as看作别名的标识符
167集