一、數(shù)據(jù)庫
1.1 數(shù)據(jù)庫 基本知識
DB:
DBMS:
數(shù)據(jù)庫,數(shù)據(jù)表,表的結(jié)構(gòu)。。
DB:是指datebase(數(shù)據(jù)庫)
數(shù)據(jù)庫是存儲數(shù)據(jù)的一個集合,數(shù)據(jù)庫中通常使用數(shù)據(jù)表等組成,而數(shù)據(jù)表是由數(shù)據(jù)的字段和數(shù)據(jù)的值等信息組成。
DBMS:是指datebase mangement systerm(數(shù)據(jù)庫管理系統(tǒng))
它是操作數(shù)據(jù)庫和管理數(shù)據(jù)庫的一個系統(tǒng),比如mysql、sqlserver等都是屬于數(shù)據(jù)庫管理軟件,人們通過這些系統(tǒng)或者工具來管理數(shù)據(jù)庫內(nèi)的數(shù)據(jù)。
DBS:是指datebase systerm (數(shù)據(jù)庫系統(tǒng))
數(shù)據(jù)庫系統(tǒng)又?jǐn)?shù)據(jù)庫和數(shù)據(jù)庫管理軟件等組成,數(shù)據(jù)庫是一個邏輯上的存儲數(shù)據(jù)的概念,而對應(yīng)的是實體是數(shù)據(jù)庫管理軟件存儲存儲在硬盤上的數(shù)據(jù)庫,所以數(shù)據(jù)庫系統(tǒng)包含數(shù)據(jù)庫和數(shù)據(jù)庫管理軟件。
1.2 Mysql的安裝和卸載
1.3 登錄
方式一:DOS窗口:輸入以下命令:
C:\Users\ruby>mysql -u root -p
回車后輸入密碼即可
方式二:通過Mysql的Command Line來登錄:
直接輸入密碼即可
方式三:通過其他的可視化工具軟件:
1.4 創(chuàng)建數(shù)據(jù)庫:
1.創(chuàng)建數(shù)據(jù)庫:
//create database [if not exists]數(shù)據(jù)庫名 [default charset utf8 collate utf8_general_ci];
mysql> create database my1905 character set utf8;
Query OK, 1 row affected (0.00 sec)
2.顯示有哪些數(shù)據(jù)庫:
mysql> show databases;
3.切換到數(shù)據(jù)庫:以后的操作都是針對該數(shù)據(jù)庫的,比如建表。。
mysql> use my1905;
4.查看當(dāng)前數(shù)據(jù)庫有哪些數(shù)據(jù)表:
mysql> show tables;
5.刪除數(shù)據(jù)庫:
mysql> drop database if exists my1905;
1.5 數(shù)據(jù)類型
char(10)–>定長的字符串
? "wangergou "
? "abc "
varchar(10)–>變長
? “wangergou”
? “abc”
1.6 數(shù)據(jù)表的操作
1.創(chuàng)建數(shù)據(jù)庫:
mysql> create database if not exists my1905 default charset utf8 collate utf8_ge
neral_ci;
2.創(chuàng)建數(shù)據(jù)表:
mysql> create table users(
-> id int(4) primary key auto_increment,
-> username varchar(20),
-> pwd varchar(30));
3.查看表結(jié)構(gòu):desc–>describe
mysql> desc users;
4.顯示檢表語句:
mysql> show create table users;
注意點:
1.先創(chuàng)建數(shù)據(jù)庫
mysql:
? database1–>oa
? database2–>bluebird
? 。。。。
2.切換數(shù)據(jù)庫
? use 數(shù)據(jù)庫名
3.創(chuàng)建數(shù)據(jù)表
mysql>create table test1(
? ->id int(4) auto_increment primary key,
? ->…);
5.插入一條數(shù)據(jù):
mysql> insert into users(id,username,pwd) values(1,'admin','123456');
Query OK, 1 row affected (0.02 sec)
6.查詢數(shù)據(jù):
mysql> select * from users;
+----+----------+--------+
| id | username | pwd |
+----+----------+--------+
| 1 | admin | 123456 |
+----+----------+--------+
1 row in set (0.00 sec)
1.7 修改表結(jié)構(gòu)
alter table 表名 xxx。。。
-
添加字段:add
mysql> alter table users add( -> age int(4), -> birthday date);
-
修改已有字段的數(shù)據(jù)類型:modify
mysql> alter table users modify age float(4,1);
注意點:并不能隨意的更改已有列的數(shù)據(jù)類型。尤其是表中已經(jīng)有數(shù)據(jù)了
? A:兼容類型:長度可以從小到大,不能已有的數(shù)據(jù)越界。
? B:不兼容類型:varchar–>int,更改失敗。
3.更改列的名字:change
mysql> alter table users change pwd password varchar(30);
?
-
刪除某列:drop
mysql> alter table users drop birthday;
如果該列存在數(shù)據(jù),那么數(shù)據(jù)也會被刪掉。
5.表重命名:rename to
mysql> alter table users rename to user2;
mysql> rename table user2 to user3;
6.刪除表:drop table
mysql> drop table user3;
1.8 插入數(shù)據(jù)
1.插入數(shù)據(jù):
insert into 表名(列1,列2,列3.。。) values(值1,值2,值3.。。)
全列插入:如果有所有列都要插入數(shù)據(jù),那么可以省略列的名字
缺省插入:如果有某一個或一些字段沒有數(shù)值,那么就要寫清楚列名和值。
同時插入多行:
1.9 修改數(shù)據(jù)
語法結(jié)構(gòu):
update 表名 set 列1=值1,列2=值2...[where 條件];
where后是修改條件:為true,才會修改數(shù)據(jù)。
運算符:
=,數(shù)值相等
!=,<>,數(shù)值不等
between ... and,區(qū)間
>
<
>=
<=
or
and
in(值1,值2,值3.。)
1.修改學(xué)號為1006的同學(xué)姓名為陳聰
mysql> update student set name='陳聰' where no=1006;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from student;
+------+--------+------+------+------------+
| no | name | age | sex | birthday |
+------+--------+------+------+------------+
| 1001 | 王二狗 | 18 | 男 | 2007-10-10 |
| 1002 | rose | 19 | 女 | 2006-09-09 |
| 1003 | jack | 20 | 男 | 2005-08-06 |
| 1004 | 張三 | 18 | 女 | 1990-12-12 |
| 1005 | 李四 | 21 | 男 | 1991-06-08 |
| 1006 | 陳聰 | 22 | 男 | 1992-10-10 |
+------+--------+------+------+------------+
6 rows in set (0.00 sec)
2.年齡小于19歲的同學(xué),性別改為女
mysql> update student set sex='女' where age < 19;
Query OK, 1 row affected (0.01 sec)
Rows matched: 2 Changed: 1 Warnings: 0
mysql> select * from student;
+------+--------+------+------+------------+
| no | name | age | sex | birthday |
+------+--------+------+------+------------+
| 1001 | 王二狗 | 18 | 女 | 2007-10-10 |
| 1002 | rose | 19 | 女 | 2006-09-09 |
| 1003 | jack | 20 | 男 | 2005-08-06 |
| 1004 | 張三 | 18 | 女 | 1990-12-12 |
| 1005 | 李四 | 21 | 男 | 1991-06-08 |
| 1006 | 陳聰 | 22 | 男 | 1992-10-10 |
+------+--------+------+------+------------+
6 rows in set (0.01 sec)
3.年齡大于等于18歲,并且小于等于19歲的同學(xué)姓名改為馬冬梅
mysql> update student set name='馬冬梅' where age >= 18 and age <= 19;
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select *from student;
+------+--------+------+------+------------+
| no | name | age | sex | birthday |
+------+--------+------+------+------------+
| 1001 | 馬冬梅 | 18 | 女 | 2007-10-10 |
| 1002 | 馬冬梅 | 19 | 女 | 2006-09-09 |
| 1003 | jack | 20 | 男 | 2005-08-06 |
| 1004 | 馬冬梅 | 18 | 女 | 1990-12-12 |
| 1005 | 李四 | 21 | 男 | 1991-06-08 |
| 1006 | 陳聰 | 22 | 男 | 1992-10-10 |
+------+--------+------+------+------------+
6 rows in set (0.00 sec)
4.修改年齡19到20歲之間的同學(xué)姓名為馬春梅:
mysql> update student set name='馬春梅' where age between 19 and 20;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from student;
+------+--------+------+------+------------+
| no | name | age | sex | birthday |
+------+--------+------+------+------------+
| 1001 | 馬冬梅 | 18 | 女 | 2007-10-10 |
| 1002 | 馬春梅 | 19 | 女 | 2006-09-09 |
| 1003 | 馬春梅 | 20 | 男 | 2005-08-06 |
| 1004 | 馬冬梅 | 18 | 女 | 1990-12-12 |
| 1005 | 李四 | 21 | 男 | 1991-06-08 |
| 1006 | 陳聰 | 22 | 男 | 1992-10-10 |
+------+--------+------+------+------------+
6 rows in set (0.00 sec)
二、SQL
結(jié)構(gòu)化查詢語言(Structured Query Language)。操作數(shù)據(jù)庫的。
DDL語言:數(shù)據(jù)定義語言(用于定義數(shù)據(jù)的表結(jié)構(gòu))Data Definition Language
? 創(chuàng)建數(shù)據(jù)表:create table 表名
? 修改數(shù)據(jù)表:alter table 表名
? 刪除數(shù)據(jù)表:drop table 表名
DML語言:數(shù)據(jù)操縱語言(用于操作數(shù)據(jù)表中的數(shù)據(jù))DML - Data Mainpulation Language
? 添加數(shù)據(jù):insert
? 修改數(shù)據(jù):update
? 刪除數(shù)據(jù):delete
DQL語言:數(shù)據(jù)查詢語言(專門用于數(shù)據(jù)的查詢)DQL - Data Query Language
? 查詢數(shù)據(jù):select
DCL語言:
三、總結(jié)
數(shù)據(jù)庫:
? 安裝和卸載(看文檔)
? 數(shù)據(jù)庫的登錄:
? 1.dos窗口:mysql命令—>配置環(huán)境變量
? -u 用戶名
? -p 密碼
? 2.mysql的命令行:直接輸入密碼即可
? 3.通過一些可視化工具:比如navicat
1.show databases;
2.create database if not exists my1905 character set utf8;
? default charset utf8 collate utf8_general_ci;
3.use my1905;
4.create table student(id int(4) primary key auto_increment,name varchar(30),sex varchar(2));
5.alter table 表名
? add 列名 數(shù)據(jù)類型
? modify 列名 數(shù)據(jù)類型
? change 原列名 新列名 數(shù)據(jù)類型
? drop 刪除列
6.drop table 表名;
7.insert into 表名(列1,列2,列3.。。) values(值1,值2,值3.。。。)
? 全列插入:
? 同時插入多條:
8.update 表名 set 列1=新值,列2=新值 [where 修改條件];
? where 后 的是表達(dá)式是boolean
? =,!=,<>,>,<,>=,<=,between and, and , or ,not …
? null—> is null ,is not null
9.delete from 表名 where 刪除條件
約束:主鍵,外鍵文章來源:http://www.zghlxwxcb.cn/news/detail-783710.html
查詢:簡單查詢,復(fù)雜,多表文章來源地址http://www.zghlxwxcb.cn/news/detail-783710.html
到了這里,關(guān)于50天精通Golang(第14天)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!