@mysql basic sql statement
DB DBMS SQL to
connect to the database
mysql-> mysql -h ip -uuser -ppassword
use leaseback
Create table
CREATE TABLE test1 (
id int(11) PRIMARY KEY,
city VARCHAR(20) not null,
count_person int(50)
);
Copy table field structure and index
CREATE TABLE test2 LIKE test1;
Copy table does not copy index can select fields
CREATE table test3 as SELECT id,city FROM test1;
Add column
ALTER TABLE test1 add COLUMN color VARCHAR(20);
Delete primary key
ALTER TABLE test1 DROP PRIMARY KEY;
Add primary key
alter TABLE test1 add PRIMARY KEY(id);
Add data to stored procedure
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();
Stored procedure cycle add
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();
Delete stored procedure method
drop procedure if exists pre;
Add index
ALTER TABLE test1 add UNIQUE INDEX (city);
Show existing indexes
show INDEX from test1;
Delete index
ALTER TABLE test1 drop INDEX city;
Insert a piece of data
INSERT into test1 (id,city,count_person,color) VALUES (1,'成都',10000,'白色');
Query data in the table
SELECT * from test1;
SELECT * from test2;
SELECT * FROM test1 WHERE id<10;
SELECT * from test1 WHERE color LIKE '%白色%';
Sort forward and reverse
SELECT * FROM test1 ORDER BY count_person DESC;
SELECT * FROM test1 ORDER BY count_person ASC;
Built-in function
#总数
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 operator combines the other two result tables
SELECT color FROM test1 UNION ALL SELECT color FROM test2;
SELECT color FROM test1 UNION SELECT color FROM test2;
The EXCEPT operator returns what is in 1 but not in 2 is related to the order
SELECT color FROM test1 EXCEPT SELECT color FROM test2;
The intersect operator returns those records that have the same value in the selected columns of the two tables
SELECT color,city FROM test1 intersect SELECT color,city FROM test2;
Inner connection WHERE or on
SELECT * FROM test1,test2 WHERE test1.color=test2.color;
SELECT * FROM test1 INNER JOIN test2 on test1.color=test2.color;
Outer join the intersection of two tables (left outer join (including the data in the left table), right outer join (including the data in the right table))
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;
Fully connected
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;
change the data
UPDATE test1 set color='pink' WHERE id BETWEEN 1 AND 5
Delete table
DROP TABLE test2;
There are three ways for sql to clear table data:
1. truncate-delete all data, retain the table structure, and cannot be undone to restore the
truncate table table name
2. delete-delete row by row is extremely slow, not suitable for large amounts of data delete
delete from table name
delete from table name where column name="value"
3. drop-delete table, delete data and table structure together, quickly
drop form table name