mysql基础sql语句使用示例

@mysql基础sql语句
DB DBMS SQL
连接数据库

mysql-> mysql -h ip -uuser -ppassword
 use leaseback

创建表

CREATE TABLE test1 (
	id int(11) PRIMARY KEY,
	city VARCHAR(20) not null,
	count_person int(50)
);

复制表 字段结构和索引

CREATE TABLE test2 LIKE test1;

复制表 不会复制索引 可以选择字段

CREATE table test3 as SELECT id,city FROM test1;

增加列

ALTER TABLE test1 add COLUMN color VARCHAR(20);

删除主键

ALTER TABLE test1 DROP PRIMARY KEY;

添加主键

alter TABLE test1 add PRIMARY KEY(id);

存储过程添加数据

DROP PROCEDURE IF EXISTS proc_initData;
DELIMITER $
CREATE PROCEDURE proc_initData()
BEGIN
    DECLARE i INT DEFAULT 1;
    WHILE i<=3 DO
        
insert into t_user ( 
    userId,
    userName,
    password,
    phone)
 values (i,'ww','123456','135786');
    SET i = i+1;
    END WHILE;
END $

CALL proc_initData();

存储过程循环添加

delimiter $$     
create procedure pre()		
begin
declare i int;	
set i=1;
while i<21 do		
INSERT INTO test.test2(id,city,count_person,color) VALUES (i, CONCAT('西昌',i),CONCAT(220,i), 
  'blue');
set i=i+1;		
end while;
end 
$$
call pre();

删除存储过程方法

drop procedure if exists pre;	

添加索引

ALTER TABLE test1 add UNIQUE INDEX (city);

显示已有索引

show INDEX from test1;

删除索引

ALTER TABLE test1 drop INDEX city;

插入一条数据

INSERT into test1 (id,city,count_person,color) VALUES (1,'成都',10000,'白色');

查询表里数据

SELECT * from test1;
SELECT * from test2;
SELECT * FROM test1 WHERE id<10;
SELECT * from test1 WHERE color LIKE '%白色%';

排序 正序 倒序

SELECT * FROM test1 ORDER BY count_person DESC;
SELECT * FROM test1 ORDER BY count_person ASC;

内置函数

#总数
SELECT count(city) FROM test1;
#求和
SELECT SUM(count_person) as 'Total' from test1;
#求平均数
SELECT AVG(count_person) as avgperson from test1;
#求最大和最小
SELECT MAX(count_person) as maxperson FROM test1;
SELECT MIN(count_person) as minperson FROM test1;

UNION 运算符 组合其他两个结果表

SELECT color FROM test1 UNION ALL SELECT color FROM test2;
SELECT color FROM test1 UNION SELECT color FROM test2;

EXCEPT运算符 返回1中有而2中没有的 和顺序有关

SELECT color FROM test1 EXCEPT SELECT color FROM test2;

intersect运算符 返回在两个表的选定列中具有相同值的那些记录

SELECT color,city FROM test1 intersect SELECT color,city FROM test2;

内连接 WHERE或on

SELECT * FROM test1,test2 WHERE test1.color=test2.color;
SELECT * FROM test1 INNER JOIN test2 on test1.color=test2.color;

外连接 两个表的交集(左外连接(包括左边表的数据),右外连接(包括右边表的数据))

SELECT * FROM test1 LEFT JOIN test2 ON test1.color=test2.color;
SELECT * FROM test1 LEFT JOIN test2 ON test1.color=test2.color and test1.count_person>2000;
SELECT * FROM test1 RIGHT JOIN test2 on test1.color=test2.color and test2.count_person>20000;
SELECT * FROM test1 RIGHT JOIN test2 on test1.color=test2.color WHERE test2.count_person>20000;

全连接

SELECT * FROM test1 FULL JOIN test2;

分组 Group by

SELECT color FROM test1 Group by color;
SELECT color,count(id) AS sums FROM test1 GROUP BY color;
SELECT test1.color,count(test1.id) as sums FROM test1 LEFT JOIN test2 on test1.color=test2.color GROUP BY test1.color;

修改数据

UPDATE test1 set color='pink' WHERE id BETWEEN 1 AND 5

删除表

DROP TABLE test2;

sql清空表数据的三种方式:
1、truncate–删除所有数据,保留表结构,不能撤销还原
truncate table 表名
2、delete–是逐行删除速度极慢,不适合大量数据删除
delete from 表名
delete from 表名 where 列名="value "
3、drop–删除表,数据和表结构一起删除,快速
drop form 表名

猜你喜欢

转载自blog.csdn.net/Krystal_RonghuiLi/article/details/107998871