mysql commonly used commands Miscellany

View version

mysqladmin -uRootmaster -pRootmaster@777 version
select version()

  

View Log_bin whether to open

show variables like 'log_bin';

 

Create a table

create table students ( student_id int unsigned, name varchar(39), sex char(1), birth date, primary key (student_id));
create table tb_emp1(id int(11), name varchar(25),deptId int(11), salary float);

create table member(id bigint auto_increment primary key,
name varchar(20),sex tinyint not null default '0'
)engine=myisam default charset=utf8 auto_increment=1;

create table tb_member1(
id bigint primary key auto_increment ,
name varchar(20),
sex tinyint not null default '0'
)ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

 
 

create table tb_member2 like tb_member1;
DROP table IF EXISTS tb_member;
create table tb_member(
id bigint primary key auto_increment ,
name varchar(20),
sex tinyint not null default '0'
)ENGINE=MERGE UNION=(tb_member1,tb_member2) INSERT_METHOD=LAST CHARSET=utf8 AUTO_INCREMENT=1 ;

 


设置主键:
create table tb_emp2 (id int(11) primary key, name varchar(25), depId int(11), salary float);
create table tb_emp3 ( id int(11),name varchar(25),depId int(11), salary float, primary key(id));
create table tb_emp4 ( name varchar(25), deptId int(11), salary FLOAT, primary key(name,deptId));

 

 

Foreign key constraint

Definition table tb_emp5, so he built deptId tb_dept1 as a foreign key to the primary key id,

create table tb_dept1( id int(11) primary key, name varchar(22) not null, location varchar(50) );
create table tb_emp5 ( id int(11) primary key, name varchar(25), deptId int(11), salary float, constraint fk_emp_dept1 foreign key(deptId) references tb_dept1(id));

  

The only constraint line

create table tb_dept2 ( id int(11) primary key, name varchar(22) unique, location varchar(50));
create table tb_dept3 ( id int(11) primary key, name varchar(22), location varchar(50), constraint sth unique(name));

  

The default value constraints

create table tb_emp7 ( id int(11) primary key, name varchar(25) not null, deptId int(11) default 1111, salary float);

  

Attribute value table automatically adds
a table can have a field set

AUTO_INCREMENT, may be any integer type (TINYINT, SMALLIN, INT, BIGINT)
create table tb_emp8( id int(11) primary key auto_increment, name varchar(25) not null, deptId int(11), salary float);
insert into tb_emp8 (name,salary) values('lucy',1000),('lura',1200),('kevin',1500);

  

Modify table data

1. Modify the table name
alter table tb_dept3 rename tb_deptment3;
2. Modify the table field
alter table tb_dept1 modify name varchar(33);
3. Modify the field name
alter table tb_dept1 change location loc varchar(50);
4. Edit field name location, and the data type is changed to varchar (60);
alter table tb_dept1 change loc location varchar(60);

  

Add field

alter table tb_dept1 add managerId int(10);
alter table tb_dept1 add column1 varchar(12) not null;
In the first category the table to add a field
alter table tb_dept1 add column2 int(11) first;
Add a field after a specified column of the table
alter table tb_dept1 add column3 int(11) after name;

  

Delete field

Delete field
alter table tb_dept1 drop column2;
Modify the field position
alter table tb_dept1 modify column1 varchar(12) first;
After the field into the designated column
alter table tb_dept1 modify column1 varchar(12) after location;

  

Delete table

drop table if exists tb_dept2;
alter table tb_emp drop foreign key fk_emp_dept; # if foreign key

  

 

 

 

Insert data

insert into students(student_id,name,sex,birth) value (1,'steven','1','1991-01-01');

insert into member(name,sex) select name,sex from member;
insert into tb_member2(id,name,sex) select id,name,sex from member where id%2=1;

  

First install the root password change

/data/app/mysql-3307/bin/mysqladmin -uroot password '123456'

  

The default view engine

show variables like '%storage_engine%';
show create table tb_dept\G

  

Inquire:

select f_id,f_name from fruits where f_name='apple';
Less than
select f_name,f_price from fruits where f_price < 10;
Bring in
select s_id, f_name,f_price from fruits  where s_id in (101,2) order by f_name;
select s_id, f_name,f_price from fruits  where s_id in (101,102) order by f_name;

between and
select f_name,f_price from fruits where f_price between 2.00 and 10.20;
not between and
select f_name,f_price from fruits where f_price not between 2.00 and 10.20;

  

like query

select * from fruits where f_name like 'b%';
select * from fruits where f_name like 'b%y';

_ Underscore a previous match
select * from fruits where f_name like ' ____y';

  

 

Null query:

select c_id,c_name,c_email from customers where c_email is null;
select * from customers where c_email is not null;

  

and inquiries:

select f_id,f_price,f_name from fruits where s_id='101' and f_price>8;
select f_id, f_price, f_name from fruits where s_id in('101','102') and f_price >= 5 and f_name ='apple';

 

or query:

select s_id,f_name,f_price from fruits where s_id=101 or s_id=102;

  

Query results are not repeated:

select distinct s_id from fruits ;

  

Sort query results:

Sorting Results
select f_name from fruits order by f_name;
Multi-column sorting
select f_name, f_price from fruits order by f_name ,f_price;
Specify the sort direction
select f_name,f_price from fruits order by f_price desc;
First price in descending order by name ascending
select f_price,f_name from fruits order by f_price desc, f_name;

 

lower_case_table_names = ignore the MySQL table names in the Linux environment-sensitive, otherwise use MyCAT time will be prompted to find the table of error 1

  

 

Guess you like

Origin www.cnblogs.com/sunshine-long/p/10968966.html