MYSQL Basics

http://www.cnblogs.com/wupeiqi/articles/5713315.html

http://www.cnblogs.com/wupeiqi/articles/5713323.html

http://www.cnblogs.com/wupeiqi/articles/5729934.html

http://www.cnblogs.com/wupeiqi/articles/5716963.html

1. Database management system DBMS
software

bin
config
db (save data)
- admin
- 123asdfasd.txt {username, password, type...}
- 123asdfasdb.txt{username, password, type...}
- course
- school
src

Write a program:
a. The database is local
1, find the directory
2, add data
b. The database is remote
1, the socket is connected to the remote machine
2, the socket sends {command} # add|asdfasdfadf
What does it do? ? ? ?
a.
A, program
program, socket client
B, data
socket server
C, make a set of rules
add|Adefa Satan launch point send
delete|asdfasdf
....
D, socket client and server user authentication, authorization , Restriction

Someone made a set of software:

socket client
B, data
socket server
C, make a set of rules
add|Adefa Satan launch point send
delete|asdfasdf
....
D, socket client and server user authentication , authorization, restrictions

-- SqlServer (charged), Oracle, sqlite, access...,,,, MySQL

MySQL, SqlServer (charged), Oracle, sqlite, access..
server and client
mysql:

add|asdfasd
SqlServer:
a|asdfasdf

Second, download and install

1. Download
2. Unzip to any directory 3. Run mysqld
on the server side

4. Client connection

Save trouble:
Make the mysql server into a Windows service
net start mysql
net stop mysql
===, open the service management, directly...

3. MySQL database

1. Concept

database, folder
database table, file
data line, one line of data in file

2.
Start server:
net start mysql
or
C:\mysql-5.7.16-winx64\mysql-5.7.16-winx64\mysql

mysql. server start

/etc/init.d/mysql start


Client connection:
C:\mysql-5.7.16-winx64\mysql-5.7.16-winx64\mysql -u root -p


Initial test:

show databases; # View the current MySQL database There are those data, the root directory has those folders

create database database name; # Create a folder

use database name; # Use the selected database, enter the directory

show tables; # Check the tables in the current database,

create table table name (nid int,name varchar(20), pwd varchar(64)); # Create database table

select * from table name; # View all data in the

table insert into table name(nid,name,pwd) values(1,'alex' ,'123'); # Insert data

-- select * from table name;

3. User authorization
##Enable all permissions for the liyinchun user. ===========IMPORTANT, REMOTE CONNECTION ====================================

































a. Free your hands, when repeating the operation of the file, directly send the command to the mysql server, and automatically operate
b. Database, table, row
c. Account opening and authorization
password, must use quotation marks ''
Others, it is recommended to use

d. Client connection ( Client provided by MySQL)
1.
mysql -u root -h 192.168.1.1 -p
# Enter the password

2.

Initial test:

show databases; # Check the current Mysql data, the root directory has those folders

create database database name; # Create a folder

use database name; # Use the selected database, enter the directory

show tables; # Check the tables in the current database,

create table table name(nid int,name varchar(20), pwd varchar(64)); # Create a database table

select * from table name; # View all data in the

table insert into table name(nid,name,pwd) values(1,'alex','123'); # Insert data

-- select * from table name ;

3. ;;;;;;;;;;;


4. SQL statement

database level
SHOW DATABASES;

CREATE DATABASE database name;
CREATE DATABASE database name DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

USE database name;

drop database database name;

table-level table
show tables;
desc tb1; view the specific properties of the

table*** create table tb1(nid int, name varchar(10));
# Transaction, atomic operation, rollback

a. Default value
b. Can it be empty
c. Auto-increment column (a table can only have one, number, must be index-primary key)
d. Primary key index:

a table can only have one There is a primary key, which cannot be repeated and cannot be null. - In general, the primary key
1 , 2, 3, 4, 5, and 6 are set for auto-increment
columns. Unique index:
can be null, and a table can have multiple unique columns
1 , 2, 3, 4, 5, 6, null

-- Constraint
-- index, speed up lookup

create table xxx(
nid.... primary key,
....
)

create table student(
name varchar(10) not null,
num int not null,
age int,
gender int,
primary key (name,num)
)
Constraints:
name num age
a 88 9
a 99 9
a 88 0

# Primary key:
cannot be null,
cannot be repeated,
a table has only one primary key (multiple columns can form a primary key)
# General Usage:
nid int auto_increment primary key,

# Please create the table locally:
CREATE DATABASE database name DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
use database name;

create table tb5(
nid int not null auto_increment primary key,
name varchar(16),
age int default 19
)engine=innodb default charset=utf8;

e. foreign key foreign key, one-to-many Two

tables create constraints

-- constraints-
- foreign key, many



f data types: numeric, string, and time.
value:
binary:
'bit binary
integer:
tinyint
smallint the
int
BIGINT
- not the same range of

decimal:
decimal - accurate

- 985,412,154,521,321.857541245421245845
FLOAT - 98541215458798465465749465465465421321.857541245420000000
- 9854121540000000000000000000000000000000000000000000000
DOUBLE -- 985412154458798465465749465465465400000000000000000000000

String:
# Fixed length
char
create table tb13(n char(7),b int, c int)
insert ssss
# Fast search speed, waste of space


# Variable length
varchar
create table tb13(n varchar(7),b int, c int)
insert ssss
insert sssss
insert sssss5
# Search speed is slow, save space
text
mediumtext
longtext
binary data:
TinyBlob, Blob, MediumBlob, LongBlob
# Upload file
# Blob, forced binary mode
# varchar(65), "D:\av. avi" to save the uploaded file on the hard disk, D:\av.avi ;

time:
DATE
YYYY-MM-DD (1000-01-01/9999-12-31)
# 1996-12-12 18:45

TIME
HH: MM:SS('-838:59:59'/'838:59:59')

YEAR
YYYY(1901/2155)

DATETIME

YYYY-MM-DD HH:MM:SS(1000-01-01 00:00:00/9999-12-31 23:59:59 Y)

TIMESTAMP

YYYYMMDD HHMMSS (1970-01-01 00:00:00/sometime in 2037)

enum
single selection

set
multiple selection

create table tb13(num decimal(6,2)) #8888.18

drop table tb1; # Direct table delete

delete from tb1 ; # Clear the table content
truncate table tb1; # Clear the table content, fast, auto increment back to the far point

select * from tbl;

data row level *****

select * from tbl;



# Increase
insert into tb1(name,age ) values('alex', 18);
insert into tb1(name,age) values('alex', 18),('eric', 19),('eric', 19);
create

# delete
delete from table
delete from table where id=1 and name='alex'

# Change
update table set name = 'alex' where id>1


# Check
select * from table
select * from table where id > 1
select nid,name,gender as gg from table where id > 1

# other

join table operations: ★★★★★★★
# join table
select * form a,b where ax = bo
mysql> select * from love,live where love .nid = live.nid;

# join,
a. left join
select * from a LEFT JOIN b ON ax = bo

b. inner join , will never appear Null
select * from a inner JOIN b ON ax = bo #combination



combination
, Automatically handle overlapping
select nickname
from A
union
select name
from B

combination, do not handle overlapping
select nickname
from A
union all
select name
from B


#Sort
select * from table order by column asc - Arrange from small to large according to "column"
select * from table order by column desc - according to "column" in descending order
select * from table order by column 1 desc, column 2 asc - according to "column 1" in descending order, if the same, then according to column 2 in ascending order To the big sort   



#Group
by
mysql> SELECT item FROM live GROUP BY item; #Aggregate
function
mysql> select nid, max(item), min(item), sum(item), count(item) from live group by item;
Maximum, minimum and number of times

mysql> select max(nid), item from live group by item having max(nid) > 3;
# You cannot use where when deleting aggregation conditions, you need to use having!

#limit
select * from table limit 5; - first 5 rows
select * from table limit 4,5; - print 5 rows from row 4
select * from table limit 5 offset 4 - print 5 rows from row 4 (and above method The same, but this is used more)

===========Today's content organization =================
1. Database concept, many
2. Database, table , line
3, start the server, start the client to connect to the server
- user management
- permission management
4, SQL statement
database operation *
table operation **
- can be empty
- default value
- primary key
- foreign key
- auto increment
- data type (char, varchar)
data row ******:
add, delete, modify, check
Others:
limit offset
left join
order by
group by ,having max,min,sum,count
# Condition, fuzzy,.....



 

 

 

 



create table fuwuzhongxing(nid int not null auto_increment primary key,
name varchar(10),age int, class int default 4)engine=innodb default charset=utf8;

 

Guess you like

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