SQL language classification
Data query language DQL, data manipulation language DML, data definition language DDL, Data Control Language DCL.
A, DDL
Creating a Basic Table
create table student ( Sno char(5) not null unique, Sname char(20) unique, Ssex char(1), Sage int, Sdept char(15));
Copy table structure and data
create table new_dwd_inv_return_record as select * from dwd_inv_return_record;
Copy not copy the data table structure
create table new_dwd_inv_return_record as select * from dwd_inv_return_record where 1 =0;
### to modify the basic table
modify table name (a method):
ALTER TABLE user10 RENAME TO user11;
Modify the table name (Act II):
RENAME TABLE user11 TO user10;
Delete table
drop table <表名>
Delete the data table, the retention structure
truncate table ‘表名’
Modify the table name
ALTER TABLE dwd_pro_repay_record RENAME TO d_pro_repay_record;
Query data (fuzzy query)
select * from dwd_inv_contract where borrow_rate like '%' limit 19;
View table structure information
desc student;
Establish and delete indexes
create [unique] [cluster] index < index name> ON <table name> (<column name> [<order] [, <column name> [<sequence]] .....);
NOTE:
UNIQUE: This indicates that each index value of the index corresponding to only record a unique
cluster: index table to be established clustered index, i.e. after a column built according to the index, on the order of data storage hard drive is adjusted by sequentially stored in the column, sequential storage order and indexes are consistent
drop index <index name>;
create unique index studentIndex on student (sno);
drop index studentIndex;
Create a view
create view v_payment_myisam as select * from payment;
Two, DML
Add field
alter table tea add id int(20) not null PRIMARY KEY ;
Delete fields in the table
alter table tea drop column id;
Set the primary key increment
auto_increment
Modify field names:
alter table tab_info rename column createname to thisname;
Modify field properties:
alter table tab_info alter column thisname varchar(200) not null;
Modify the default values
alter table tabinfo add constraint df default('嘿嘿') for thisname;
View table structure
desc 表名
Inserting data into the table
insert into tbl1 values('jingba',2345);
Delete the data table
delete from tbl1 where id=1;
Modify data in the table
update tbl1 set name=""
From the first few began to check back a few
select * from dept order by deptno limit 2,1;
View the current time
select now();
Set time format
select date_format(now() , '%Y-%m-%d %H:%:m:%s');
Three, DCL
Set a new password to the root user
mysql>update user set authentication_string=password("123456") where user="root";
Create a user and grant all privileges
mysql> GRANT ALL PRIVILEGES ON *.* TO jiqing@"%" IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.03 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
View all users (root)
select host,user from mysql.user;
Grant remote access permission
GRANT ALL PRIVILEGES ON *.* TO 'bdr'@'%' IDENTIFIED BY 'D.123%dr' WITH GRANT OPTION;
Four, SQL function
Sum (sum)
select sum(order_price) as sunnum from orders ;
Averaging (avg)
select AVG(order_price) from orders
De-emphasis (distinct)
SELECT DISTINCT (order_price) from orders;
Sort (order by)
SELECT DISTINCT(order_price) from orders ORDER BY order_price DESC;(默认,升序ASC)
Specify the number of queries (limit)
SELECT DISTINCT(order_price) from orders limit 3;
Fuzzy query (like)
SELECT customer from orders where customer LIKE 'li%'
Range queries - hash (in)
SELECT * from orders where order_price in (300,600);
Range queries - range (between)
SELECT * from orders where order_price BETWEEN 300 and 600;