--连接数据库 --mysql -u root -p --密码:linyifan -- 创建数据表 -- auto_increment 自增 -- PRIMARY KEY 主键 CREATE TABLE employee( id TINYINT PRIMARY KEY auto_increment, name VARCHAR(25), gender boolean, age TINYINT, department VARCHAR(20), salary DOUBLE(7,2) ) -- 增加表is_married字段 alter table employee add is_married TINYINT(1) -- 增加表多个字段 alter table employee add addr varchar(20),add city varchar(20) -- 删除city字段 alter table employee drop city -- 修改age字段类型为smallint,not null:非空,default:默认18,且移动到id字段的后面 alter table employee modify age smallint not null default 18 after id -- 修改表age字段为ages alter table employee change age ages int(2) -- 修改表名employee改为newemployee rename table employee to newemployee -- 删除表employee drop table employee -- 添加id为主键 alter table employee add primary key id -- 删除表主键 alter table employee drop primary key -- 查询 SELECT * FROM employee; -- 插入单条数据 键值对赋值 INSERT INTO employee SET name = '胖胖' -- 插入单条数据 INSERT INTO employee(name,gender,age,department,salary) VALUE(1,1,1,1,1); -- 插入多条数据 INSERT INTO employee(name,gender,age,department,salary) VALUE (1,1,1,1,1),(2,2,2,2,2),(3,3,3,3,3); -- 删除数据 DELETE FROM employee WHERE id = 10 OR id = 4 --更改数据 UPDATE employee SET name = '胖胖',department = '18' WHERE id = 13; UPDATE employee SET name = '胖胖',department = '18' WHERE id = 13; UPDATE employee SET name = '阿胖',age = age + 1,department = '我的一辈子' WHERE id = 13; --清空表 TRUNCATE TABLE employee --查 --distinct:去重name SELECT distinct name from employee SELECT * from employee name like 'n%' SELECT * from employee WHERE JS BETWEEN 80 AND 100 SELECT * from employee WHERE JS IN(80,89,100) SELECT * from employee WHERE JS IN(80,89,100) ORDER BY id DESC SELECT * from employee WHERE JS IN(80,89,100) ORDER BY id DESC SELECT name from employee GROUP BY name SELECT name,SUM(JS) from employee GROUP BY name SELECT name,SUM(Diango) from employee GROUP BY name HAVING SUM(Django) > 150 SELECT name,SUM(JS)/COUNT(name) from employee GROUP BY name -- 求JS平均值 SELECT name,AVG(JS) from employee GROUP BY name -- 求JS平均值 SELECT name,MAX(JS) from employee GROUP BY name -- 求JS最高分 SELECT name,MIN(JS) from employee GROUP BY name -- 求JS最低分 --主表 CREATE TABLE Techer( id TINYINT PRIMARY KEY auto_increment, name VARCHAR(25), age TINYINT, )ENGINE = INNODB; --创建子表外键 注:外键和主见的数据类型要保持一致 --外键约束对子表的含义:不能删除子表和主表存在外键关联的数据 CREATE TABLE Student( id TINYINT PRIMARY KEY auto_increment, name VARCHAR(25), gender boolean, age TINYINT, charger_id TINTINT, FROM KEY (charger_id) REFERENCES Techer(id) --创建子表外键 )ENGINE = INNODB; --在创建表的时候没有加外键可执行下面语句添加一个外键名为abc的外键 ALTER TABLE Student ADD CONSTRAINT abc FOREIGN KEY (charger_id) REFERENCES Techer(id) --删除外键 ALTER TABLE Student DROP FOREIGN KEY abc --级联删除与SET NULL --ON DELETE CASCADE:当删除主表的数据时,从表的所关联的数据也会一起删掉 CREATE TABLE Student( id TINYINT PRIMARY KEY auto_increment, name VARCHAR(25), gender boolean, age TINYINT, charger_id TINTINT, FROM KEY (charger_id) REFERENCES Techer(id) ON DELETE CASCADE --创建子表外键 )ENGINE = INNODB; --级联删除与SET NULL --SET NULL CREATE TABLE Student( id TINYINT PRIMARY KEY auto_increment, name VARCHAR(25), gender boolean, age TINYINT, charger_id TINTINT, FROM KEY (charger_id) REFERENCES Techer(id) ON DELETE SET NULL --创建子表外键 )ENGINE = INNODB; --多表查询 --连接查询:内连接 innerjoin,外连接 left join right join 全连接 full join --子查询 --内连接查询 SELECT * FROM Techer T,Student S WHERE T.id = S.charger_id SELECT * FROM Techer T INNER JOIN Student S ON T.id = S.charger_id --外左连接:以Techer为主表,能匹配上得就显示,不能匹配上得显示NULL SELECT * FROM Techer T LEFT JOIN Student S ON T.id = S.charger_id --外右连接:以Student为主表,能匹配上得就显示,不能匹配上得显示NULL SELECT * FROM Techer T RIGHT JOIN Student S ON T.id = S.charger_id --外全连接:UNION会去掉重复得数据 SELECT * FROM Techer T LEFT JOIN Student S ON T.id = S.charger_id UNION SELECT * FROM Techer T RIGHT JOIN Student S ON T.id = S.charger_id SELECT * FROM Techer T LEFT JOIN Student S ON T.id = S.charger_id UNION ALL SELECT * FROM Techer T RIGHT JOIN Student S ON T.id = S.charger_id --复合查询 SELECT * FROM employee,department WHERE employee.dep_id = department dep_id ORDER BY ASC AND employee.age > 25; --子查询 SELECT * FROM employee WHERE dep_id IN(SELECT dep_id FROM department) --带exists关键字的子查询 --内查询不反回查询记录而是返回一个True or False,返回True时外层语句进行查询,返回False时外层语句不进行查询 SELECT * FROM employee WHERE EXISTS(SELECT dept_name FROM department WHERE dept_id = 203 ) --索引 --创建索引与维护索引的消耗很多的时间和磁盘空间,但会提高查询的速度 CREATE TABLE test( id INT PRIMARY KEY auto_increment, name VARCHAR(25), resume TEXT, saary INT DELETE 1000 --默认值1000 ); --给表name字段添加唯一索引 ALTER TABLE test MODIFY name VARCHAR(20) UNIQUE --创建普通索引名为(index_name) CREATE TABLE emp( id INT PRIMARY KEY auto_increment, name VARCHAR(25), INDEX index_name(name) ); --创建多列索引名为(index_name_resume) CREATE TABLE emp( id INT PRIMARY KEY auto_increment, name VARCHAR(25), resume VARCHAR (20) INDEX index_name_resume(name,resume) ); --添加索引(已存在的表上) CREATE UNIQUE INDEX index_name ON emp(id) --存储过程start CREATE TA Ind(id int ,name varchar(20)); delimiter $$ CREATE procedure autoinsert() BEGIN declare i int default 1; while(1<500000)do insert into Ind values(i,'lin') set i = i + 1 end while; END $$ delimiter; --调用函数 call autoinsert() --存储过程end CREATE TABLE TEST(id int,name varchar (20)) --开启事物 start transaction --提交事物 commit --回滚 rollback --savepoint:指定回滚到某一位置