日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

mysql sql语句编码_SQL语句实用例子 MySQL编码设置

發(fā)布時(shí)間:2023/11/27 生活经验 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql sql语句编码_SQL语句实用例子 MySQL编码设置 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

SQL語言包含4個(gè)部分:

★ 數(shù)據(jù)定義語言(DDL),例如:CREATE、DROP、ALTER等語句。 ★ 數(shù)據(jù)操作語言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(刪除)語句。 ★ 數(shù)據(jù)查詢語言(DQL),例如:SELECT語句。 ★ 數(shù)據(jù)控制語言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等語句。

關(guān)于數(shù)據(jù)庫編碼的設(shè)置:

在MySQL設(shè)置向?qū)е锌梢栽O(shè)置編碼格式,如果設(shè)置成了UTF-8在命令行操作時(shí)需要注意一下幾點(diǎn): 建表前設(shè)置編碼:

set names utf8;

查詢建表時(shí)用的字符集:

show create table tablename;

為了能在Windows命令行執(zhí)行insert,update,則執(zhí)行命令:

set character_set_client=gbk;

為了能在Windows命令下能正確查看中文,則執(zhí)行命令:

set character_set_results=gbk;

創(chuàng)建數(shù)據(jù)庫:

create database StudentManage;

使用數(shù)據(jù)庫:

use StudentManage;

創(chuàng)建數(shù)據(jù)表:

//學(xué)生表(學(xué)號(hào),學(xué)生姓名)

create table Student(

Sno char(9) primary key,

Sname varchar(20) unique

);

//課程表(課程代號(hào),課程名稱,先修課程代號(hào))

create table Course(

Cno char(4) primary key,

Cname char(40),

Cpno char(4),

foreign key (Cpno) references Course(Cno)

);

//選課表(學(xué)生學(xué)號(hào),課程代號(hào))

create table SC(

Sno char(9),

Cno char(4),

Primary key(Sno,Cno),

foreign key(Sno) references Student(Sno),

foreign key(Cno) references Course(Cno)

);

mysql> desc Student;

+-------+-------------+------+-----+---------+-------+

| Field | Type ? ? ? ?| Null | Key | Default | Extra |

+-------+-------------+------+-----+---------+-------+

| Sno ? | char(9) ? ? | NO ? | PRI | NULL ? ?| ? ? ? |

| Sname | varchar(20) | YES ?| UNI | NULL ? ?| ? ? ? |

+-------+-------------+------+-----+---------+-------+

2 rows in set (0.01 sec)

mysql> desc Course;

+-------+----------+------+-----+---------+-------+

| Field | Type ? ? | Null | Key | Default | Extra |

+-------+----------+------+-----+---------+-------+

| Cno ? | char(4) ?| NO ? | PRI | NULL ? ?| ? ? ? |

| Cname | char(40) | YES ?| ? ? | NULL ? ?| ? ? ? |

| Cpno ?| char(4) ?| YES ?| MUL | NULL ? ?| ? ? ? |

+-------+----------+------+-----+---------+-------+

3 rows in set (0.05 sec)

mysql> desc SC;

+-------+---------+------+-----+---------+-------+

| Field | Type ? ?| Null | Key | Default | Extra |

+-------+---------+------+-----+---------+-------+

| Sno ? | char(9) | NO ? | PRI | NULL ? ?| ? ? ? |

| Cno ? | char(4) | NO ? | PRI | NULL ? ?| ? ? ? |

+-------+---------+------+-----+---------+-------+

2 rows in set (0.01 sec)

修改表結(jié)構(gòu):

添加一個(gè)表字段:

alter table student add column sex char(2);

刪除一個(gè)表字段:

alter table student drop column sex restrict;

修改字段類型:

alter table Student modify Sname varchar(20);

alter table student change Sname Sname varchar(20);

查看表的create語句:

show create table Student;

結(jié)果如下:

| Student | CREATE TABLE `student` (

`Sno` char(9) NOT NULL,

`Sname` varchar(20) default NULL,

PRIMARY KEY ?(`Sno`),

UNIQUE KEY `Sname` (`Sname`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 |

其中的UNIQUE KEY `Sname` (`Sname`)就是約束索引了。 刪除字段的約束索引:

alter table Student drop index Sname;

向表中插入數(shù)據(jù):

插入全部字段:

insert into Student values ('080010011','arthinking');

插入部分字段:

insert into Course (Cno, Cname) values('0001','Java');

修改字段值:

update Student set Sno='080010013' where Sno='123456789';

刪除全部數(shù)據(jù):

delete from Student;

刪除指定數(shù)據(jù):

delete from Student where(Sno=’ 080010013’) or Sname is null;

查詢:

查詢所有列:

select * from student;

查詢指定列:

select Sname from Student;

取消重復(fù)行:

select distinct Cname from Course;

模糊查詢:

select * from student where Sname like '_a%';

排序:

select * from Student order by Sno asc;

select * from Student order by Sno desc;

group by和having子句:

select avg(Cno),Cname from Course group by Cname having avg(Cno) >2;

Course表包含數(shù)據(jù)如下:

+------+-------+------+

| Cno ?| Cname | Cpno |

+------+-------+------+

| 0001 | Java ?| NULL |

| 0002 | C++ ? | NULL |

| 0003 | C++ ? | NULL |

+------+-------+------+

3 rows in set (0.00 sec)

查詢結(jié)果如下:

+----------+-------+

| avg(Cno) | Cname |

+----------+-------+

| ? ? ?2.5 | C++ ? |

+----------+-------+

1 row in set (0.06 sec)

多表查詢:

連接查詢: 查詢有選課的學(xué)生:

select s.Sname from Student s,SC sc where s.Sno = sc.Sno;

多行子查詢: 查詢?cè)谶x課表中有選課的學(xué)生的全部信息:

select * from Student s where s.Sno in (select sc.Sno from SC sc);

在from自句中使用子查詢創(chuàng)建臨時(shí)表: 查找除了0001號(hào)課程外已有人選修了的課程的名稱:

mysql> select distinct c.Cname from Course c,(select Cno,Sno from SC where Cno not in('0001') ) temp where c.Cno=temp.Cno;

分頁查詢:

MySQL:select * from Student limit 0,2;

總結(jié)

以上是生活随笔為你收集整理的mysql sql语句编码_SQL语句实用例子 MySQL编码设置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。