Mysql-basic syntax


--Connect to the database-- mysql -u root -p 
--Password: linyifan 

-Create a data table 
-auto_increment increment 
-PRIMARY KEY 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) 
) 
-increase table is_married field 
alter table employee add is_married TINYINT (1)-increase 

table multiple fields 
alter table employee add addr varchar (20), add city varchar (20) 

-delete the city field 
alter table employee drop city 

-modify the age field type to smallint, not null: not empty, default: default 18, and move to the back of the id field 
alter table employee modify age smallint not null default 18 after id

-Modify the 
table age field to ages 
alter table employee change age ages int (2) 
-Modify the table name employee to newemployee 
rename table employee to newemployee 

-Delete the table employee 
drop table employee 

-Add the id as the primary key 
alter table employee add primary key id 

-delete table primary key 
alter table employee drop primary key 

-query 
SELECT * FROM employee;-insert 

a single data key-value pair assignment 
INSERT INTO employee SET name = 'fat' '-insert 

a single data 
INSERT INTO employee ( name, gender, age, department, salary) VALUE (1,1,1,1,1); 

-Insert multiple data 
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 data
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-find the average JS 
SELECT name, AVG (JS) from employee GROUP BY name-find the average JS 
SELECT name, MAX (JS) from employee GROUP BY name-find the highest JS score 
SELECT name, MIN (JS) from employee GROUP BY name-find the lowest JS score 

-main table 
CREATE TABLE Techer ( 
  id TINYINT PRIMARY KEY auto_increment, 
  name VARCHAR (25), 
  age TINYINT, 
) ENGINE = INNODB; --Create a foreign key to the subtable 

Note: The foreign key and the predominant data type must be consistent 
-foreign key constraint pairs The meaning of the table: can not delete the data associated with the foreign key of the child table and the main table 
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)-create a sub-table foreign key 
) ENGINE = INNODB; -no foreign key 

is added when the table is created, the following statement can be executed to add a foreign key named abc 
ALTER TABLE Student ADD CONSTRAINT abc FOREIGN KEY (charger_id) REFERENCES Techer (id)-delete 

foreign key 
ALTER TABLE Student DROP FOREIGN KEY abc 

-cascade delete and SET NULL 
--ON DELETE CASCADE: When deleting the data of the main table, The associated data from the table will also be deleted together with 
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 
--Create subtable foreign key 
) ENGINE = INNODB; 
 
-cascade delete and 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 --create a foreign key to the child table 
) ENGINE = INNODB ; 
  -Multi 


-table query -join query: inner join innerjoin, outer join left join right join full join full join 
  -subquery 
-inner join query 
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 

-outer left join: Techer as the main table, it can be displayed if it can be matched, and it can not be displayed if it can be matched. NULL 
SELECT * FROM Techer T LEFT JOIN Student S ON T.id = S.charger_id 

-external right connection: with Student as the main table, it can be displayed if it matches, and NULL if it cannot be matched 
SELECT * FROM Techer T RIGHT JOIN Student S ON T.id = S.charger_id 

-external full connection: UNION will remove duplicate data 
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 

-compound query 
SELECT * FROM employee, department WHERE employee = Department dep_id the ORDER BY .dep_id the AND employee.age ASC> 25; 

- sub-query 
the SELECT * the FROM the Employee the WHERE dep_id the iN (the SELECT dep_id the FROM Department) 

- with sub-keyword query exists 
- within the query does not return to the anti-record inquiry Instead, it returns a True or False, the outer statement is queried when True is returned, and the outer statement is not queried when False is returned 
SELECT * FROM employee WHERE EXISTS (SELECT dept_name FROM department WHERE dept_id = 203)

- the index of  
- to create an index with the index maintenance time and consume a lot of disk space, but will improve the speed of query

the CREATE TABLE Test ( 
  ID a PRIMARY KEY AUTO_INCREMENT the INT, 
  name VARCHAR (25), 
  Resume the TEXT, 
  saary the DELETE the INT 1000 - Default 1000 
); 
- name field added to the table unique index 
ALTER TABLE test MODIFY name VARCHAR (20 ) UNIQUE 

-create a common index named (index_name) 
CREATE TABLE emp ( 
  id INT PRIMARY KEY auto_increment, 
  name VARCHAR (25), 
  INDEX index_name (name) 
); 

-create a multi-column index named (index_name_resume) 
CREATE TABLE emp ( 
  id INT PRIMARY KEY auto_increment, 
  name VARCHAR (25), 
  resume VARCHAR (20) 
  INDEX index_name_resume (name, resume) 
); --Add 

index (on an existing table)
CREATE UNIQUE INDEX index_name ON emp (id) 
 
procedure 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 

function call autoinsert ()-stored 

procedure end 

CREATE TABLE TEST (id int, name varchar (20)) 


-open things 
Transaction Start 
- submit things 
the commit 
- rollback 
rOLLBACK 

--savepoint: roll back to a specified location

  

Guess you like

Origin www.cnblogs.com/Essaycode/p/12672756.html