The basic operation of the database (a)

mysqld install # install sql service

net start mysql # Start Service

stop service net stop mysql #

select user (); # to view the current user

mysql -uroot -p # username password
mysql -uroot -h192.168.12.61 # others mysql connection

set password = password ( '123'); # Set Password (note semicolon, can forget wrap directly added something ';')

\ C # sql statement to abandon the current representation to be executed

create user 'username' @ 'allows network segments' identified by 'password' # Create an account
# Create an account
mysql> create user 'eva'@'192.168.10.%' identified by '123'; # indicates the segment
mysql > create user 'eva'@'192.168.10.5' # indicates a machine may be connected
mysql> create user 'eva' @ '%' # indicates that all machines are connected
mysql> show grants for 'eva'@'192.168.10.5' ; # view a user's permissions
# telnet
MySQL-uroot--p123 -h192.168.10.3
# account authorization to
MySQL> Grant All ON to 'EVA' @ '%';.
MySQL> flush privileges; # refresh the authorization with immediate effect
# create an account and authorize
mysql> grant all on. to ' eva' @ '%' identified by '123'

 

Basic operation:

The basic operation of the database:

create database database name;

show databases; view the database

use the database name to switch to the library

The basic operation of the data table:

create table damo (num int, user_name char (12), password char (32)); Create a table

show tables; see all the tables in the current library

desc table to view the table structure

Basic operation data:

insert into demo values(1,'xuhuo','xuhuo');

select * from table name; see the table of contents of all

Select the specified field Field 1, Field 2 ... from table to view the table

update demo set password = 'xuhuo1' where num = 1; # modified data

delete from demo where num = 1; delete data, where the back with the deleted data

 

type of data

  • Digital Type

    • Integer tinyint (0,255) int (0- more than four billion)

    • All data created by default is signed

    • create table t1(i1 tinyint unsigned,i2 int unsigned) # unsigned 是约束,表示无符号

    • Decimal float double

    • create table t3(f1 float(7,2),f2 double)

  • String

    • Fixed-length: save time wasted space char (255 characters)

    • Longer: space-saving waste of time varchar (65535 Ge)

    • create table t5(c1 char(5),c2 varchar(5))

  • Time Type

    • datetime # year, month, day, hour during 0000-9999 years

    • date # date

    • minutes and seconds when time #

    • In year #

    • year, month, day, hour when the timestamp # 1970- 2038

  • enum and set

    • enum radio

    • create table t6(username char(12),sex char() enum(''))

    • set multiple choice

 

Single-table operation:

insert

# 增 insert
# insert into emp values (3,'alex',84,'female',2.2,'linux,python'),
# (4,'alex',84,'female',2.2,'linux,python');
# insert into emp(id,name) values (5,'wusir'), (6,'wusir');
# insert into emp2 select * from emp;
# insert into emp2(id,name) select id,name from emp;

#删
delete

# Change Update
Update Table 1 set field value = 1, 2 where the value of the field condition = 1;

 

select:

# Prepare to build the table:
the Create the Table the Employee (
the above mentioned id int not null UNIQUE AUTO_INCREMENT,
emp_name VARCHAR (20) not null,
Sex enum ( 'MALE', 'FEMALE') not null default 'MALE', # mostly men
age int (3) not null default unsigned 28,
hire_date DATE not null,
POST VARCHAR (50),
post_comment VARCHAR (100),
salary Double (15,2),
Office int, a division of a house #
depart_id int
);

https://www.cnblogs.com/Eva-J/articles/9688313.html

select * from 表名;

# Specify the columns in the query:
the SELECT name from the table name;
# use four operations in the column:
the SELECT emp_name, salary * 12 from the Employee;
# rename a query result fields:
the SELECT emp_name, salary * 12 AS annul_salary from the Employee;
# deduplication DISTINCT
SELECT DISTINCT POST from Employee;

# Concat () splice
select concat ( 'Name:', emp_name), concat ( ' annual salary:', salary * 12) from employee;

case when语句
SELECT
(
CASE
WHEN emp_name = 'jingliyang' THEN
emp_name
WHEN emp_name = 'alex' THEN
CONCAT(emp_name,'_BIGSB')
ELSE
concat(emp_name, 'SB')
END
) as new_name
FROM
employee;

 

where:

The line filter conditions where clause
comparison operations => <!> = <= = / <>
BETWEEN A and B
SELECT * from Employee where the salary BETWEEN 10000 and 20000;
in
SELECT * from Employee where in the salary (17000,19000);
like Fuzzy queries
_ wildcard matches a character length
select * from employee where emp_name like ' jin___';
the contents of any length% wildcard
select * from employee where the emp_name like 'Jin%';
regexp regular matching
select * from employee where emp_name regexp '^ jin';

 

逻辑运算
# and
select * from employee where age > 18 and salary > 10000;
# or
select * from employee where age > 18 or salary > 10000;
# not
select * from employee where salary not in (10000,17000);

 

About null
employees See job description to null
select * from employee where post is null ;
see the job description of the employee information is not null
select * from employee where post not null is;

 

Five aggregate function
COUNT ()
max ()
min ()
AVG ()

 

group by group aggregation
of all employee name query job name and job included in
select post, emp_name from employee group by post;
query aged 20 years or older in each sector average salary
select post, avg (salary) from employee where age> 20 group by post;

 

Filter having (group by + aggregate functions)
query ten thousand greater than the average salary department:
SELECT Employee from Group by POST POST HAVING AVG (the salary)> 10000;

 

 

order by 排序
升序
select * from employee order by salary;
select * from employee order by salary asc;
降序
select * from employee order by salary desc;
select * from employee order by age,salary desc;
select * from employee order by age desc,salary asc;

 

limit
select * from table order by limit n columns; # taking the first n
select * from table order by column limit m, n; starting from m + 1, taking the n
select * from table order by limit n offset m columns; from m + 1 starts, taking the n

 

# Keyword must be written in the order
select * from Table where conditions having packet filter group by order by sorting limit n;

 

POST the SELECT, GROUP_CONCAT (emp_name) from the Employee Group by POST;
# query all members of a group

 

# Exercise

https://www.cnblogs.com/Eva-J/articles/11074845.html

https://www.cnblogs.com/Eva-J/articles/9772614.html

Guess you like

Origin www.cnblogs.com/xuyuwei/p/11587174.html