3.mysql的中文问题,database级操作,表级操作,数据CRUD,分组操作,时间和日期,字符串相关函数,表的约束
1 連接MYSQL服務器:mysql–uroot –p123456
查看中文問題
show variables like 'character%';
2 修改mysql的字符集,退出mysql提示符界面:
mysql -uroot -p--default_character_set=gbk;
3? 數據庫的操作:創建,查看,修改,刪除
???????? *創建:
?????????????????? 創建一個名稱為mydb1的數據庫。
??????????????????????????? createdatabase mydb1;
?????????????????? 創建一個使用utf-8字符集的mydb2數據庫。
???????? ?????????????????? create database mydb2character set utf8;
?????????????????? 創建一個使用utf-8字符集,并帶校對規則的mydb3數據庫。
??????????????????????????? createdatabase mydb3 character set utf8 collate utf8_general_ci;
???????? *查看:
?????????????????? 顯示所有數據庫
??????????????????????????? showdatabases;
?????????????????? 顯示創建數據庫的語句信息
??????????????????????????? showcreate database mydb2;
???????? *修改:
?????????????????? 修改mydb1的字符集為gbk(不能修改數據庫名)
??????????????????????????? alterdatabase mydb1 character set utf8;?????
???????? *刪除:
?????????????????? 刪除數據庫mydb2
??????????????????????????? dropdatabase mydb1;
?
4 表的操作:創建,查看,修改,刪除
??? usemydb2;
???????? *創建:
?????????????????? 根據實體類Person創建表person
??????????????????????????? Person{
???????????????????????????????????? intid;
???????????????????????????????????? Stringname;
??????????????????????????? }
??????????????????????????? createtable person(
???????????????????????????????????? idint,
???????????????????????????????????? namevarchar(20)
??????????????????????????? );
?????????????????? mysql中的數據類型:
??????????????????????????? bit1位 但可以指定位數,如:bit<3>
??????????????????????????? int2字節 可以指定最大位數,如:int<4> 最大為4位的整數
??????????????????????????? float2個字節 可以指定最大的位數和最大的小數位數,如:float<5,2> 最大為一個5位的數,小數位最多2位
??????????????????????????? double 4個字節 可以指定最大的位數和最大的小數位數,如:float<6,4> 最大為一個6位的數,小數位最多4位
??????????????????????????? char 必須指定字符數,如char(5) 為不可變字符 即使存儲的內容為'ab',也是用5個字符的空間存儲這個數據
??????????????????????????? varchar 必須指定字符數,如varchar(5) 為可變字符 如果存儲的內容為'ab',占用2個字符的空間;如果為'abc',則占用3個字符的空間
??????????????????????????? text:大文本(大字符串)
??????????????????????????? blob:二進制大數據 如圖片,音頻文件,視頻文件
??????????????????????????? date:日期 如:'1921-01-02'
??????????????????????????? datetime:日期時間 如:'1921-01-02 12:23:43'
??????????????????????????? timeStamp:時間戳,自動賦值為當前日期時間
???????????????????????????
???????? ???????? 創建一個員工表
??????????????????????????? createtable employee(id int,name varchar(20),sex bit,birthday date,salarydouble,entry_date date,resume text);
???????? *查看:
?????????????????? 查看所有的表:
??????????????????????????? showtables;
?????????????????? 查看指定表的創建語句
??????????????????????????? showcreate table employee;
??????????????????????????? mysql表 名稱區分大小寫
?????????????????? 顯示指定表的結構:
??????????????????????????? descemployee;
???????? *刪除:
?????????????????? 刪除employee表
?????????????????? droptable employee;
???????? *修改表:
???????create table worker(id int,name varchar(20),sex bit,birthday date,salarydouble,entry_date date,resume text);
?????????????????? 增加一個字段:altertable worker add column height double;
?????????????????? 修改一個字段:altertable worker modify column height float;
?????????????????? 刪除一個字段:altertable worker drop column height;
?????????????????? 更改表名:renametable employee to worker;
?????????????????? 修改表的字符集:altertable worker character set gbk;
????????
5 表數據的CRUD
???????? *C(create增加數據) Insert語句
?????????????????? 新建Employee表并表中添加一些記錄
??????????????????????????? createtable employee(
???????????????????????????????????? idint,
???????????????????????????????????? namevarchar(20),
???????????????????????????????????? sexbit,
???????????????????????????????????? birthdaydate,
???????????????????????????????????? salarydouble,
???????????????????????????????????? entry_datedate,
???????????????????????????????????? resumetext
??????????????????????????? );
???????????????????????????
??????????????????????????? insertinto employee(id,name,sex,birthday,salary,entry_date,resume) values(1,'張三',1,'1983-09-21',15000,'2012-06-24','一個大牛');
??????????????????????????? insertinto employee(id,name,sex,birthday,salary,entry_date,resume) values(2,'李四',1,'1984-09-21',10000,'2012-07-24','一個中牛');
??????????????????????????? insertinto employee(id,name,sex,birthday,salary,entry_date,resume) values(3,'王五',0,'1985-09-21',7000,'2012-08-24','一個小牛');
??????????????????????????? deletefrom employee where id=1
?
??????????????????????????? createtable employee(?? id int,namevarchar(20),sex bit,birthday date,salary double,entry_date date,resume text);
????????
???????? *U(update更新數據) Update語句
?????????????????? 將所有員工薪水都增加500元。
??????????????????????????? updateemployee set salary=salary+500;
?????????????????? 將王五的員工薪水修改為10000元,resume改為也是一個中牛
??????????????????????????? updateemployee set salary=10000,resume='也是一個中牛' where name='王五';
???????? *D(drop刪除數據) Delete語句
?????????????????? 刪除表中姓名為王五的記錄。
??????????????????????????? deletefrom employee where name='王五';
?????????????????? 刪除表中所有記錄。
??????????????????????????? deletefrom employee; --可以有條件,但刪除所有記錄差了一點
?????????????????? 使用truncate刪除表中記錄。
??????????????????????????? truncateemployee;--無條件效率高
????????
???????? 6? *R(Retrieve查找數據) Select語句?
?????????????????? 準備環境:
??????????????????????????? createtable student(
??????????????????????????? idint,
??????????????????????????? namevarchar(20),
??????????????????????????? chineseint,
??????????????????????????? englishint,
??????????????????????????? mathint
??????????????????????????? );
?
??????????????????????????? insertinto student(id,name,chinese,english,math) values(1,'何東',80,85,90);
??????????????????????????? insertinto student(id,name,chinese,english,math) values(2,'權箏',90,95,95);
??????????????????????????? insertinto student(id,name,chinese,english,math) values(3,'何南',80,96,96);
??????????????????????????? insertinto student(id,name,chinese,english,math) values(4,'葉坦',81,97,85);
??????????????????????????? insertinto student(id,name,chinese,english,math) values(5,'何西',85,84,90);
??????????????????????????? insertinto student(id,name,chinese,english,math) values(6,'丁香',92,85,87);
??????????????????????????? insertinto student(id,name,chinese,english,math) values(7,'何北',75,81,80);
??????????????????????????? insertinto student(id,name,chinese,english,math) values(8,'唐嬌',77,80,79);
??????????????????????????? insertinto student(id,name,chinese,english,math) values(9,'任知了',95,85,85);
??????????????????????????? insertinto student(id,name,chinese,english,math) values(10,'王越',94,85,84);
?
?????????????????? 查詢表中所有學生的信息。
??????????????????????????? select* from student;
?????????????????? 查詢表中所有學生的姓名和對應的英語成績。
??????????????????????????? selectname,english from student;
?????????????????? 過濾表中重復數據。
??????????????????????????? selectenglish from student;
??????????????????????????? selectDISTINCT english from student;
??????????????????????????? selectDISTINCT english,name from student;
?
??????????????????????????? selectenglish+chinese+math from student;
??????????????????????????? selectenglish+chinese+math as 總分 from student;
???????? ?????????????????? selectname,english+chinese+math as 總分 from student;
?
?????????????????? 在所有學生英語分數上加10分特長分。
??????????????????????????? selectname,english+10 from student;
?????????????????? 統計每個學生的總分。
??????????????????????????? selectenglish+chinese+math from student;
?????????????????? 使用別名表示學生分數
??????????????????????????? selectname,english+chinese+math as 總分 from student;
??????????????????????????? selectname,english+chinese+math 總分 from student;
?
?????????????????? 查詢姓名為何東的學生成績
??????????????????????????? select* from student where name='何東';
?????????????????? 查詢英語成績大于90分的同學
??????????????????????????? select* from student where english>90;
?????????????????? 查詢總分大于250分的所有同學
??????????????????????????? select* from student where english+chinese+math>250;
?
?????????????????? 查詢英語分數在 85-95之間的同學。
??????????????????????????? select* from student where english>=85 and english<=95;
??????????????????????????? select* from student where english between 85 and 95;
?????????????????? 查詢數學分數為84,90,91的同學。
??????????????????????????? select* from student where math=84 or math=90 or math=91;
??????????????????????????? select* from student where math in(84,90,91);
???????? ???????? 查詢所有姓何的學生成績。
??????????????????????????? select* from student where name like '何%';
?????????????????? 查詢數學分>85,語文分>90的同學。
??????????????????????????? select* from student where math>85 and chinese>90;
?
?????????????????? 對數學成績排序后輸出。
??????????????????????????? select* from student order by math;
?????????????????? 對總分排序后輸出,然后再按從高到低的順序輸出
??????????????????????????? select* from student order by math+chinese+english desc;
?????????????????? 對姓何的學生成績排序輸出
??????????????????????????? select* from student where name like '何%' order by math+chinese+english desc;
??????????????????????????? selectname, math+chinese+english from student where name like '何%' order bymath+chinese+english desc;
?
?????????????????? 統計一個班級共有多少學生?
??????????????????????????? selectcount(*) from student;
?????????????????? 統計數學成績大于90的學生有多少個?
??????????????????????????? selectcount(*) from student where math>90;
?????????????????? 統計總分大于250的人數有多少?
??????????????????????????? selectcount(*) from student where math+chinese+english>250;
?
?????????????????? 統計一個班級數學總成績?
??????????????????????????? selectsum(math) from student;
?????????????????? 統計一個班級語文、英語、數學各科的總成績
??????????????????????????? selectsum(math), sum(chinese), sum(english) from student;
?????????????????? 統計一個班級語文、英語、數學的成績總和
??????????????????????????? selectsum(math+chinese+english)from student;
??????????????????????????? selectsum(math)+sum(chinese)+sum(english) from student;
?
?????????????????? 求一個班級數學平均分?
??????????????????????????? selectavg(math) from student;
?????????????????? 求一個班級總分平均分
??????????????????????????? selectavg(math+chinese+english)from student;
??????????????????????????? selectavg(math)+avg(chinese)+avg(english) from student;
?
?????????????????? 求班級最高分和最低分
??????????????????????????? selectmax(math+chinese+english),min(math+chinese+english) from student;
????????
7 綜合性練習:為學生表,增加一個班級列,然后訓練分組查詢
?????????????????? 查出各個班的總分,最高分
??????????????????????????? 準備環境
???????????????????????????????????? 給表添加一個字段:altertable student add column class_id int;
???????????????????????????????????? 更新表:
?????????????????????????????????????????????? updatestudent set class_id=1 where id<=5;
?????????????????????????????????????????????? updatestudent set class_id=2 where id>5;
??????????????????????????? selectsum(math+chinese+english),max(math+chinese+english) from student group byclass_id;
?
?????????????????? 查詢出班級總分大于1300分的班級ID
??????????????????????????? selectclass_id from student group by class_id havingsum(math+chinese+english)>1300;
??????????????????????????? selectclass_id from student where sum(math+chinese+english)>1300 group by class_id;
?????????????????? note:where和group區別: 在wehre子句中不能使用分組函數
??????????????????
??????????????????
?
8? 時間和日期
?????????????????? mysql>select year (now()), month(now()), day(now()) , date(now());
?????????????????? +--------------+--------------+------------+-------------+
?????????????????? |year (now()) | month(now()) | day(now()) | date(now()) |
?????????????????? +--------------+--------------+------------+-------------+
?????????????????? |?? ??????2014 |??????????? 9 |????????? 7 | 2014-09-07? |
?????????????????? +--------------+--------------+------------+-------------+
????????
?????????????????? selectdate_add(now(), INTERVAL 2 year) from dual;//增加兩年
?????????????????? selectcharset('name')? employee;
?????????????????? selectdate_add(now(), INTERVAL -1 day) 昨天, now() 今天, date_add(now(), INTERVAL +1 day) 明天;
?
9 字符串相關函數
???????? selectconcat( charset('name'), 'aaaa') 自定義 from dual;
?
?
10 表的約束
???????? *定義主鍵約束 primarykey:不允許為空,不允許重復
???????? *定義主鍵自動增長 auto_increment
???????? *定義唯一約束 unique
???????? *定義非空約束 notnull
???????? *定義外鍵約束 constraintordersid_FK foreign key(ordersid) references orders(id)
???????? *刪除主鍵:altertable tablename drop primary key ;
?
???????? createtable myclass
???????? (
?????????????????? idINT(11) primary key auto_increment,
?????????????????? namevarchar(20) unique
???????? );
????????
???????? createtable student(
?????????????????? idINT(11) primary key auto_increment,
?????????????????? namevarchar(20) unique,
?????????????????? passwdvarchar(15) not null,
?????????????????? classidINT(11),? #注意這個地方不要少逗號
?????????????????? constraintstu_classid_FK? foreign key(classid)references myclass(id)
???????? );
????????
?
總結
以上是生活随笔為你收集整理的3.mysql的中文问题,database级操作,表级操作,数据CRUD,分组操作,时间和日期,字符串相关函数,表的约束的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2.使用windows下的客户端连接虚拟
- 下一篇: 5.中文问题(自身,操作系统级别,应用软