MySQL對錶的操做

對錶的總體操做

建立表spa

use mydb1;utf-8

create table employee(id int, name varchar(40),sex  varchar(2),birthday date,entry_date date,job varchar(40),salary decimal(8,2),resume text);ci

 

查看錶string

show tables;table

show create table employee;cli

desc employee;亂碼

 

修改表date

alter table employee add image blob;select

alter table employee modify sex varchar(4);方法

alter table employee drop sex;

alter table employee character  utf8;

alter table employee change column name xingming varchar(40);

rename table employee to emp;

 

刪除表

Drop table emp;

 

對錶的細操做

插入數據:

Insert into employee

(id,name,sex,birthday,entry_date,job,salary,resume) values

(1, 'john', 'a', '1990-10-01', '2014-10-1', 'ceo',10000, 'hello');

insert into employee values(1, 'john', 'a','1999-09-09','1999-09-09','bbb',90,'aaaaa');

        

插入遇到的問題

         ERROR 1300 (HY000): Invalid utf8 character string: '\xA1\xAFaaa\xA1\xAF'

解決辦法:引號問題’ ’這種是GB2312 , '  '這種是utf-8

ERROR 1366 (HY000): Incorrect string value: '\xC4\xD0' for column 'sex' at row 1

解決辦法:show variables like 'chara%';

                    set character_set_client=gb2312;

                                                        該處查看會顯示亂碼

解決方法:set character_set_results=gb2312;

 

修改表數據:

update employee set salary=10;

update employee set salary=10000 where name='john';

update employee set salary=salary+1000 where name='aaa';

 

刪除表的記錄

delete from employee where sex='null';

delete from employee;  //刪除表的內容,但不刪除定義,不釋放空間

truncate table employee;//刪除內容、釋放空間,但不刪除定義

Drop table emp;       //刪除表的內容、定義和釋放空間

查詢表數據

select * from student;

 

條件查詢

select * from student where name='王五';

select name,english from student;

select distinct english from student;

 

select name,(chinese+english+math) from student;

select name,(chinese+english+math)+10 from student;

 

select name as 姓名,(chinese+english+math)+10 as 總分 from student;  //別名

select name 姓名,(chinese+english+math)+10  總分 from student;

 

select * from student where english>'90';

select name from student where (chinese+english+math)>200;

select name from student where (chinese+english+math)> '200'  //這裏用單引號也是能夠的

 

select name from student where english>80 and english<90;

select name from student where English between 80 and 90;  //這句跟上面那句是同樣的

 

select * from student where math in(89,90,91);

 

模糊查詢

select * from student where name like '_'; 2個字符)

select * from student where name like '%';  (2個字符以上)

 

排順查詢

select name,math from student order by math;

select name 姓名,(chinese+english+math) 總分 from student order by (chinese+english+math) desc;

select name 姓名,(chinese+english+math) 總分 from student order by 總分 desc;

select * from student where name like '%' order by (chinese+english+math) desc;

 

統計

select count(name) from student;

select count(*) from student;

select count(*) from student where (chinese+english+math)>250;

 

求和

select sum(chinese),sum(english),sum(math) from student;

select sum(chinese+english+math) from student;

 

求平均

select avg(chinese) from student;

select avg(chinese+math+english) from student;

相關文章
相關標籤/搜索