數(shù)據(jù)庫、表操作

1.啟動(dòng)MySQL服務(wù)

啟動(dòng):net start mysql;

關(guān)閉:net stop mysql;

登錄數(shù)據(jù)庫:mysql -uroot -p123

2.查看數(shù)據(jù)庫:show databases;

3.使用test數(shù)據(jù)庫:use test;

4.創(chuàng)建test數(shù)據(jù)庫:create database test;

create database if not exits test;(如果數(shù)據(jù)庫存在也不報(bào)錯(cuò))

5.刪除test數(shù)據(jù)庫:drop database test;

drop database if exits test;(如果數(shù)據(jù)庫不存在也不報(bào)錯(cuò))

6.修改數(shù)據(jù)庫編碼為UTF-8:alter database test character set utf8;(utf-8的-要省略)

7.查看當(dāng)前數(shù)據(jù)庫中的表:show tables;

8.查看test表的創(chuàng)建語句:show create table test;

9.查看test表的表結(jié)構(gòu):desc test;

10.刪除test表:drop table test;

11.創(chuàng)建表user:

? primary key(主鍵):主鍵定義為int,定義為varchar會(huì)報(bào)錯(cuò);

? auto_increment(自增長(zhǎng)):只有主鍵能設(shè)置自增長(zhǎng),其他字段不能;

not null(非空):非空約束能多個(gè)存在

unique(唯一):唯一約束能多個(gè)存在

? create table user(

? ? ?? id ? int(10) ? ?? primary key? auto_increment,

? ? ?? password? varchar(10) ?? not null? unique,

? ? ?? age? int(5)

? ? ?? );

create table user(

? ? ? id? int(10),

? ? ? password? varchar(10),

? ? ? age? int(5),

? ? primary key(id)

? ? ? );

12.test表中添加列id

alter table test add (id int(5));

13.修改test表的id列的類型為char

alter table test modify id char(5);

14.修改test表的id列名為age(可同時(shí)修改數(shù)據(jù)類型)

alter table test change id age int(5);

15.test表刪除name列

alter table test drop name;

16.重命名test表為user

alter table test rename to user;

17.查看user表中數(shù)據(jù)(*表示所有字段)

select * from user;

select id ,name? from user;

18.去除user表中重復(fù)記錄

insert into user values(1,'123');

insert into user values(1,'123');

insert into user values(2,'123');

insert into user values(1,'1234');

select distinct * from user;

select distinct id from user;


select distinct name from user;

19.查看emp表sal和comm之和

因?yàn)閟al和comm兩列的類型都是數(shù)值類型,所以可以做加運(yùn)算。如果sal或comm中有一個(gè)字段不是數(shù)值類型,那么會(huì)出錯(cuò)。

select *,sal+comm from emp;

comm列有很多記錄的值為NULL,因?yàn)槿魏螙|西與NULL相加結(jié)果還是NULL,所以結(jié)算結(jié)果可能會(huì)出現(xiàn)NULL。下面使用了把NULL轉(zhuǎn)換成數(shù)值0的函數(shù)IFNULL:

select *,sal+ifnull(comm,0) from emp;

20.給id列添加sfz 別名(as可省略)

select id as sfz from user;

select id sfz from user;

21.退出數(shù)據(jù)庫

exit

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • ORACLE自學(xué)教程 --create tabletestone ( id number, --序號(hào)usernam...
    落葉寂聊閱讀 1,242評(píng)論 0 0
  • 1.簡(jiǎn)介 數(shù)據(jù)存儲(chǔ)有哪些方式?電子表格,紙質(zhì)文件,數(shù)據(jù)庫。 那么究竟什么是關(guān)系型數(shù)據(jù)庫? 目前對(duì)數(shù)據(jù)庫的分類主要是...
    喬震閱讀 2,023評(píng)論 0 2
  • 一、上堂回顧 1.概念? 數(shù)據(jù)庫管理系統(tǒng),數(shù)據(jù)庫,表? SQL的分類:DDL、DML、DQL、DCL2.數(shù)據(jù)庫的使...
    WenErone閱讀 459評(píng)論 0 0
  • 接下來的一些內(nèi)容,我們需要提前學(xué)一些簡(jiǎn)單的sql語句,方便大家理解接下來的知識(shí)。 DDL—數(shù)據(jù)定義語言(Creat...
    不排版閱讀 475評(píng)論 0 1
  • 世界經(jīng)濟(jì)放緩 各國貨幣超發(fā) 以鄰為壑 嚴(yán)重通貨膨脹開始 以理發(fā)店舉例 年前普通剪發(fā)從十元上漲為十二元 通脹率20%...
    宇文泰閱讀 195評(píng)論 0 0

友情鏈接更多精彩內(nèi)容