当前位置:网站首页>MySQL basic addition, deletion, modification and query exercise

MySQL basic addition, deletion, modification and query exercise

2022-06-09 09:14:00 Maximize+

practice 1

# Add, delete, change and check exercises 
# Create database dbtest11
CREATE DATABASE IF NOT EXISTS dbtest11 CHARACTER SET 'utf8'

# The following script creates a table my_employees
USE dbtest11;
CREATE TABLE my_employees(
		id INT(10),
		first_name VARCHAR(10),
		last_name VARCHAR(10),
		userid VARCHAR(10),
		salary DOUBLE(10,2)
);
CREATE TABLE users(
		id INT,
		userid VARCHAR(10),
		department_id INT
);
SHOW TABLES;

# Display table my_employess Structure 
DESC my_employees;
DESC users;

# towards my_employees insert data 
INSERT INTO  my_employees
VALUES(1,'patel','Ralph','Rpatel',895);
SELECT*FROM my_employees;

INSERT INTO  my_employees
VALUES
(2,'Dancs','Betty','Bdancs',860),
(3,'Biri','Ben','Bbiri',1100),
(4,'Newman','Chad','Cnewman',750),
(5,'Ropeburn','Audrey','Aropebur',1550);
SELECT*FROM my_employees;


# towards users Insert data into the table 
INSERT INTO users VALUES
(1,'Rpatel',10),
(2,'Bdancs',10),
(3,'Bbiri',20),
(4,'Cnewman',30),
(5,'Aropebur',40);

SELECT*FROM users;

# take 3 Of employee No last_name It is amended as follows "drelxer"
UPDATE my_employees
SET last_name = 'drelxer'
WHERE id = 3;

# Reduce all wages to less than 900 The salary of the employee is modified to 1000
UPDATE my_employees
SET salary = 1000
WHERE salary <900;

# take userid by Bbiri Of users Table and my_employees All records in the table are deleted 
# The way 1
DELETE FROM my_employees
WHERE userid = 'Bbiri';
DELETE FROM users
WHERE userid = 'Bbiri'; 
# The way 2
DELETE m,u
FROM my_employees m 
JOIN users u
ON m.userid = u.userid
WHERE m.userid = 'Bbiri';
SELECT*FROM my_employees;
SELECT*FROM users;

# Delete my_employees、users Table all data 
DELETE FROM my_employees;
DELETE FROM users;
# Check the correction results 
SELECT*FROM my_employees;
SELECT*FROM users;

# Empty my_employees
TRUNCATE TABLE my_employees;
# Data cannot be rolled back at this time 

practice 2

# Use the existing dbtest11 database 
USE dbtest11;

# Create a table pet
CREATE TABLE pet(
	NAME VARCHAR(20),
	OWNER VARCHAR(20),
	species VARCHAR(20),
	sex CHAR(1),
	birth YEAR,
	death YEAR
);

DESC pet;

# Add records 
INSERT INTO pet
VALUES
('Fluffy','harold','Cat','f','2003','2010'),
('Claws','gwen','Cat','m','2004',NULL),
('Buffy',NULL,'Dog','f','2009',NULL),
('Fang','beeny','Dog','m','2000',NULL),
('bowser','diane','Dog','m','2003','2009'),
('Chirpy',NULL,'Bird','f','2008',NULL);

SELECT*FROM pet;

# Add fields : The master's birthday owner_birth DATE type 
ALTER TABLE pet
ADD owner_birth DATE;

# Name as Claws The owner of the cat is changed to Kevin
UPDATE pet
SET OWNER = 'Kevin'
WHERE NAME = 'Claws'; AND species = 'Cat'
SELECT*FROM pet;
# Change the owner of the undead dog to duck
UPDATE pet
SET OWNER = 'duck'
WHERE death IS NULL AND species = 'Dog';
SELECT*FROM pet;

# Search for pet names without owners 
SELECT NAME
FROM pet
WHERE OWNER IS NULL;
# Query dead Cat The name of , master , And the time of death 
SELECT NAME,OWNER,death
FROM pet
WHERE death IS NOT NULL AND species = 'Cat';

# Delete the dead dog 
DELETE FROM pet
WHERE death IS NOT NULL
AND species = 'Dog';
SELECT*FROM pet;
# Check all pet information 
SELECT*FROM pet;

practice 3

# Use existing dbtest11
USE dbtest11;

# establish employee, And add a record 
CREATE TABLE employee(
	id INT,
	NAME VARCHAR(15),
	sex CHAR(1),
	tel VARCHAR(25),
	addr VARCHAR(35),
	salary DOUBLE(10,2)
);

INSERT INTO employee(id,NAME,sex,tel,addr,salary)VALUES
(10001,' Zhang Yiyi ',' male ','13456789000',' Qingdao, Shandong ',1001.58),
(10002,' Liu Xiaohong ',' Woman ','13454319000',' Baoding, Hebei ',1201.21),
(10003,' Li Si ',' male ','0751-1234567',' Guangdong foshan ',1004.11),
(10004,' Liu Xiaoqiang ',' male ','0755-5555555',' Guangdong shenzhen ',1501.23),
(10005,' Wang Yan ',' male ','020-1232133',' Guangdong guangzhou ',1405.16);

SELECT * FROM employee;

# Find the salary in 1200~1300 Employee information between 
SELECT*FROM employee
WHERE salary BETWEEN 1200 AND 1300;
# Find out the last name " Liu " Employee's job number , full name , Home address 
SELECT id,NAME,addr
FROM employee
WHERE NAME LIKE ' Liu %';

# Change Li Si's home address to Shaoguan, Guangdong 

UPDATE employee
SET addr = ' Shaoguan, Guangdong '
WHERE NAME = ' Li Si ';

# Find the name with “ Small ” The employees' 
SELECT *
FROM employee
WHERE NAME LIKE '% Small %';

About addition, deletion, modification and query

Yes, add, delete, modify, and check JDBC Very common operations , Cooperation after proficiency SELECT Query can flexibly manipulate and modify data at will , Is a very important content .
The following content is Data type refinement 、 constraint 、 View 、 stored procedure
Then there JDBC Technology .

原网站

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