当前位置:网站首页>Introduction to SQL -- Basic additions, deletions, modifications, and exercises

Introduction to SQL -- Basic additions, deletions, modifications, and exercises

2022-07-23 06:06:00 Diesel

Data addition, deletion, modification and query

1. increase :INSERT

INSERT INTO tbl_name [(column [, column ...])] 
VALUES(value [, value ...]);
  • The inserted data must be of the same data type as the field

  • The length of the data must be within the specified range of the column

  • stay values The data position listed in must correspond to the arrangement position of the added column

  • Character and date data should be enclosed in single quotation marks , If the string can be converted to numeric type , You can complete implicit conversion in this field

  • Columns can be inserted with null values NULL, The premise is that this field allows null values

  • You can add multiple pieces of data in one statement :

    VALUES (…), (…), (…), …

  • If you add data to all fields in the table , Field names can be omitted

  • When there is a default value , When the inserted data does not have the value of this field , Then it is added as the default value by default ; Otherwise, an error will be reported : If a field is not specified NOT NULL, This value is not given when adding data , The default is NULL Insert into this field

2. Change :UPDATE

UPDATE tbl_name
	SET	col_name1=expr1 [, col_name2=expr2, ...]
	[WHERE where_definition]
  • If there is no WHERE, Then modify all existing entries ( Use with caution !!!
  • You can modify multiple fields simultaneously in one statement

3. Delete :DELETE

DELETE FROM tbl_name
[WHERE where_definition]
  • If there is no WHERE expression , Delete all data entries . This operation is different from DROP TABLE sentence
  • To delete the value of a column in an entry , You should use UPDATE Empty statement (NULL perhaps ’')

4. check :SELECT

SELECT [DISTINCT] *|{col_name1, col_name2, ...}
FROM tbl_name;
  • DISTINCT Optional , Remove duplicate data when displaying results , Only when all fields of the query result are the same , Will remove duplicate data

  • You can use expressions to operate on the columns of the query

    SELECT * | {col_name1 | expression, col_name1 | expression, ...} FROM tbl_name
    
  • Use AS sentence , Rename the column names of query results

    SELECT col_name AS  Alias  FROM tbl_name
    

stay where Operators commonly used in clauses

Comparison operator

  • > < <= >= = <> != Greater than less than
  • BETWEEN … AND … The value in a certain interval : Closed interval
  • IN(set) In collection ( list ) In the value of the
  • [NOT] LIKE ‘pattern’ Fuzzy query :‘ king %’ String starting with Wang
  • IS NULL Is it empty

Logical operators

  • and And
  • or or
  • not Not

order by Clause sort query results

SELECT * | {col_name1 | expression, col_name1 | expression, ...} 
	FROM tbl_name
	order by column asc|desc [, column asc|desc, ...]
  • column Specify sorted columns

  • asc Ascending ( Default ),desc Descending

  • order by Clause should be in SELECT End of statement

  • Can pass , Realize multi-level sorting

practice

curd on table

CREATE TABLE `goods` (
	id INT,
	goods_name VARCHAR(10),
	price DOUBLE);

insert

INSERT INTO `goods` (id, goods_name, price)
	VALUES(10, 'HUAWEI', 2000);

INSERT INTO `goods` (id, goods_name, price)
	VALUES(11, 'Apple', 1999);

INSERT INTO `goods` (id, goods_name, price)
	VALUES(12, 'VIVO', 2199), (13, 'XIAOMI', 1999);

INSERT INTO `goods`
	VALUES(14, 'OPPO', 2499);
	
INSERT INTO `goods` (id, goods_name, price)
	VALUES('8848', '8848', 23999);

INSERT INTO `goods` (id, goods_name, price)
	VALUES(11, 'Samsung', NULL);

SELECT * FROM `goods`

update

UPDATE emp SET salary = 5000

UPDATE emp
	SET salary = 6000
	WHERE user_name = 'Danfer'

INSERT INTO emp
	VALUES(2, 'Haochen', '2021-06-01', '2021-06-01 15:25:59', 'sleeping for fun', 7000, ' What a his mother , Sleep can make money ', 'h:\\haha.jpg')

UPDATE emp
	SET salary = salary + 1000, job = 'sleeping and eating'
	WHERE user_name = 'Danfer'

SELECT * FROM emp

delete

DELETE FROM emp
	WHERE user_name = 'Danfer'
	
DELETE FROM emp

SELECT * FROM emp

select

CREATE TABLE score(
	id INT,
	`name` VARCHAR(20),
	chinese INT,
	math INT,
	english INT);

INSERT INTO score
	VALUES (1, ' king 123', 102, 135, 143),
				(2, ' In the east 123', 116, 132, 142),
				(3, ' Liu 123 hin ', 108, 135, 140),
				(4, ' beam 123', 106, 125, 138),
				(5, ' Zhou 123', 121, 113, 143),
				(6, ' Liu 456', 128, 138, 137);

SELECT * FROM score;

SELECT `name`, english FROM score;

SELECT DISTINCT english FROM score;

SELECT `name`, (chinese + math + english) FROM score;

SELECT `name`, (chinese + math + english) AS total FROM score;

SELECT * FROM score WHERE `name`=' king 123'

SELECT * FROM score WHERE chinese>=105

SELECT * FROM score WHERE (chinese+math+english)>=369

SELECT * FROM score WHERE chinese>110 AND math>120

SELECT * FROM score WHERE `name` LIKE ' Liu %'

SELECT * FROM score WHERE `name` LIKE '% hin ' order by math

SELECT `name`, chinese, math, english, (chinese+math+english) as total FROM score order by total desc
原网站

版权声明
本文为[Diesel]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207221757224640.html