Database 2

Chapter 1 SQL Statement The
database does not know the JAVA language, but we also need to interact with the database. At this time, we need to use the language SQL statement recognized by the database, which is the code of the database.
Structured Query Language (SQL), referred to as SQL, is a database query and programming language used to access data and query, update and manage relational database systems.
Creating a database, creating a data table, and adding pieces of data information to a data table all require the use of SQL statements.
1.1 SQL Statement
l SQL classification:
n Data Definition Language: DDL (Data Definition Language) for short, used to define database objects: database, table, column, etc. Keywords: create, alter, drop, etc.
Data manipulation language: referred to as DML (Data Manipulation Language), used to update the records of the table in the database. Keywords: insert, delete, update, etc.
Data control language: DCL (Data Control Language) for short, used to define the access authority and security level of the database, and create users.
n Data Query Language: DQL (Data Query Language) for short, used to query the records of the table in the database. Keywords: select, from, where, etc.
1.2 General SQL syntax
l SQL statements can be written in one or more lines, ending with a semicolon
l Spaces and indentation can be used to enhance the readability of statements
l SQL statements in MySQL databases are not case-sensitive , uppercase is recommended, for example: SELECT * FROM user.
l You can also use /**/ to complete comments
l The data types we often use in MySQL are as follows
Database 2
The detailed data types are as follows (detailed reading is not recommended!)
Database 2
Database 2
1.3 Database operation: database
l Create database
format:

  • create database database name;
  • create database database name character set character set;
    for example: #The
    encoding of the data in the database database is the default encoding utf8 specified when installing the database
    CREATE DATABASE day21_1; #Create
    a database and specify the encoding of the data in the database
    CREATE DATABASE day21_2 CHARACTER SET utf8 ;
    Database 2
    l View database
    View all databases in the database MySQL server:
    show databases;
    View the definition information of a database:
    show create database database name;
    for example:
    show create database day21_1;
    l delete database
    drop database database name;
    for example:
    drop database day21_2;
    l Other database operation commands to
    switch databases:
    use database name;
    for example:
    use day21_1;
    l View the database in use:
    select database();
    1.4 Statements related to table structure 1.4.1 Create table
    l Format:
    create table table name (
    fieldname type(length) constraint,
    fieldname type(length) constraint
    );
    For example:
    ###Create a classification table
    CREATE TABLE sort (
    sid INT, #classification ID
    sname VARCHAR(100) #classification name
    );
    1.4.2 Primary key constraints
    The primary key is a field used to identify the current record. It is characterized by being non-null and unique. Under normal circumstances in development, the primary key does not have any meaning, but is only used to identify the current record.
    Format:
    l 1. Create the primary key when creating the table, and add the primary key after the field.
    create table tablename(
    id int primary key,
    .......
    )

l 2. Create the primary key when creating the table, and specify the primary key at the end of the table creation
create table tablename(
id int,
......,
primary key(id)
)
l 3. Delete the primary key: alter table table name drop primary key;
alter table sort drop primary key;
l 4. Automatic growth of the primary key: Generally, the primary key is a self-growing field and does not need to be specified.
To implement adding self-growth statement, add auto_increment after the primary key field (only for MySQL)
For example:
###Create a classification table
CREATE TABLE sort (
sid INT PRIMARY KEY auto_increment, #classification ID
sname VARCHAR(100) #classification name
);
Database 2
other constraints: Other constraints, such as foreign keys, unique, non-null, etc., will be introduced in detail in the employment class.
1.4.3 View tables
l View all tables in the database:
format: show tables;
l View table structure:
format: desc table name;
for example: desc sort;
1.4.4 Delete table
l format: drop table table name;
for example: drop table sort;
1.4.5 Modify the table structure format:
l alter table table name add column name type (length) constraint;
role: modify the table to add columns.
For example:
#1, add a new field to the classification table to describe the classification varchar(20)
ALTER TABLE sort ADD sdesc VARCHAR(20) ;
l alter table table name modify column name type (length) constraint;
role: modify the type length and constraints of the table to modify the column.
For example:
#2, modify the classification name field of the classification table, type varchar(50) add constraint not null
ALTER TABLE sort MODIFY sname VARCHAR(50) NOT NULL;
l alter table table name change old column name new column name type (length) constraint;
role: modify the table to modify the column name.
For example:
#3, is the classification name of the classification table Replace the field with snamesname varchar(30)
ALTER TABLE sort CHANGE sname snamename VARCHAR(30);
l alter table table name drop column name;
function: modify the table to delete the column.
For example:
#4, delete the snamename column in the classification table
ALTER TABLE sort DROP snamename;
l rename table table name to new table name;
role: modify the table name
such as:
#5, Rename the classification table sort to category
RENAME TABLE sort TO category;
l alter table table name character set character set;
function: Modify the character set of the table
For example:
#6, Modify the encoding table of the classification table category, modify it to gbk
ALTER TABLE category CHARACTER SET gbk;
1.4.6 Insert table records:
l Syntax:
insert into table (column name 1, column name 2, column name 3..) values ​​(value 1, value 2, value 3..); -- Insert some columns into the
table insert into table values ​​(value 1, value 2, value 3..); -- Insert all columns into the table
l Note:
The inserted data should be the same as the data type of the field and
the size of the data The data positions listed in values ​​that should be
within must correspond to the permutation position of the column being added.
Except for numeric types, values ​​of other field types must be enclosed in quotation marks.
If you want to insert a null value, you can leave the field blank, or insert null.
For the automatic growth of the column in the operation, you can directly insert the null value.
l For example:
INSERT INTO sort(sid,sname) VALUES('s001', 'Apparel');
INSERT INTO sort(sid,sname) VALUES('s002', 'Clothing');
INSERT INTO sort VALUES('s003', 'cosmetics');
INSERT INTO sort VALUES('s004','books');
Database 2
1.4.7 Update table records:
used to modify the data of the specified conditions, and specify the columns for the records that meet the conditions Modify to the specified value
l Syntax:
update table name set field name = value, field name = value;
update table name set field name = value, field name = value where condition;
l Note:
u The type of column name and the modified value must be Consistent.
When u modify the value, it cannot exceed the maximum length.
If the u value is a string or a date, you need to add ''.
For example:
#1, modify the value in the specified sname field to daily necessities
UPDATE sort SET sname='daily necessities';
# 2, Change the sname in the record whose sid is s002 to daily necessities
UPDATE sort SET sname='daily necessities' WHERE sid='s002';
UPDATE sort SET sname='daily necessities' WHERE sid='s003';
1.4.8 Delete records: delete
l syntax:
delete from table name [where condition];
or
truncate table table name;
l interview questions:
delete all records in the table using delete from table name;Or use the truncate table table name;
Delete method: delete deletes one by one without clearing the number of auto_increment records.
truncate directly deletes the table, rebuilds the table, auto_increment will be set to zero, and start over.
For example:
DELETE FROM sort WHERE sname='daily necessities'; #Table
data is cleared
DELETE FROM sort;
1.5 DOS operation data garbled solution
When we operate Chinese on the dos command line, an error will be reported
insert into user(username,password) values('Zhang San ','123');
ERROR 1366 (HY000): Incorrect string value: '\xD5\xC5\xC8\xFD' for column 'username' at row 1
Reason: Because of the problem of mysql client encoding, ours is utf8, And the system's cmd window encoding is the gbk
solution (temporary solution): modify the mysql client encoding.
show variables like 'character%'; View all mysql encodings
Database 2
in the figure. Client-related encoding settings:
client connetion result and client-related
database server system and server-side related
l Modify the client encoding to gbk.
set character_set_results= gbk; / set names gbk;
The above operations are only effective for the current window, and will be invalid if the server is closed. If you want to modify it permanently, use the following methods:
l In the mysql installation directory, there is a my.ini file
default-character-set=gbk client-side encoding setting
character-set-server=utf8 server-side encoding setting
Note: After modifying the configuration file, Restart the service
Chapter 2 SQL Query Statement The
query statement is used most times in development, and the "zhangwu" accounting table is used here.
l Create an account table:
CREATE TABLE zhangwu (
id INT PRIMARY KEY AUTO_INCREMENT, -- account ID
name VARCHAR(200), -- account name
money DOUBLE, -- amount
);
l Insert table records:
INSERT INTO zhangwu(id ,name,money) VALUES (1,'food expenses',247);
INSERT INTO zhangwu(id,name,money) VALUES (2,'salary income',12345);
INSERT INTO zhangwu(id,name,money) VALUES (3,'Clothing expenditure',1000);
INSERT INTO zhangwu(id,name,money) VALUES (4,'Meal expenditure',325);
INSERT INTO zhangwu(id,name,money) VALUES (5,'Stock income ',
INSERT INTO zhangwu(id,name,money) VALUES (6, payout for playing mahjong, 8000);
INSERT INTO zhangwu(id,name,money) VALUES (7,null,5000);
2.1 Query syntax:
l Query the specified field information
select field 1, field 2,...from table name;
for example:
select id, name from zhangwu;
l query all fields in the table
select from table name;
for example:
select
from zhangwu;
note: use " " in the practice and learning process It can be used in the actual development, but it is not recommended to use it. The reason is that the field information to be queried is not clear. If the number of fields is large, the query speed will be very slow.
l distinct is used to remove duplicate records
select distinct field from table name;
for example:
select distinct money from zhangwu;
l alias query, use the as keyword, as can be omitted.
Alias ​​can set aliases for fields and tables in the table. When the query statement is complex, using aliases can greatly simplify the operation.
Table alias format:
select
from table name as alias;
or
select from table name alias;
Column alias format:
select field name as alias from table name;
or
select field name alias from table name;
for example ,
table alias:
select
from zhangwu as zw;
column alias:
select money as m from zhangwu;
or
select money m from zhangwu;
l In the operation of the sql statement, we can directly operate on the column.
For example: display the amount of all accounts + 10000 yuan.
select pname, price + 10000 from product;
2.2 Conditional query
where statement table conditional filtering. It is used to query and modify data if it meets the conditions and does not operate if it does not meet the conditions.
Format: select field from table name where condition;
while conditions are as follows:
Database 2
for example:
query all meal expenses records
SELECT FROM zhangwu WHERE name = 'meal expenses';
query information with an amount greater than 1000
SELECT
FROM zhangwu WHERE money >1000;
query Accounting information
SELECT with an amount between 2000-5000FROM zhangwu WHERE money >=2000 AND money <=5000;
or
SELECT
FROM zhangwu WHERE money BETWEEN 2000 AND 5000;
Query the product information with the amount of 1000 or 5000 or 3500
SELECT FROM zhangwu WHERE money =1000 OR money =5000 OR money = 3500;
or
SELECT
FROM zhangwu WHERE money IN(1000, 5000, 3500);
to query the account information whose account name contains "expenditure".
SELECT FROM zhangwu WHERE name LIKE "%expenditure%";
query the account information without five characters in the account name
SELECT
FROM gjp ledger WHERE ldesc LIKE "____ "; -- five underscores_query
the account name without For null accounting information
SELECT FROM zhangwu WHERE name IS NOT NULL;
SELECT
FROM zhangwu WHERE NOT (name IS NULL);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324970445&siteId=291194637