mysql學習(linux系統)

一、安裝

[root@xue ~]# yum -y install mariadb-server mariadb

二、啟動

[root@xue ~]# systemctl start mariadb
[root@xue ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.

三、設置初始密碼

[root@xue ~]# mysql_secure_installation
[root@xue ~]# ps -ef | grep mysqld
mysql    31338     1  0 03:04 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql    31495 31338  0 03:04 ?        00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root     31597 28727  0 03:06 pts/0    00:00:00 grep --color=auto mysqld
[root@xue ~]# mysqladmin -uroot -p create RUNOOB
Enter password: 
[root@xue ~]# mysqladmin -uroot -p drop RUNOOB
Enter password: 
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.

Do you really want to drop the 'RUNOOB' database [y/N] Y
Database "RUNOOB" dropped

四、進入數據庫

[root@xue ~]# mysql -uroot -p

五、創建數據庫

MariaDB [(none)]> create database pap;
Query OK, 1 row affected (0.00 sec)

六、創建數據表

MariaDB [pap]> create table class (stu int,name varchar(20),agr int,area varchar(20));
Query OK, 0 rows affected (0.20 sec)

MariaDB [pap]> create table score (stu int,name varchar(20),ke varchar(10),fen int);
Query OK, 0 rows affected (0.10 sec)

MariaDB [php]> create table msg(id int,title varchar(60),name varchar(10),content varchar(1000))engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.10 sec)

七、插入數據

MariaDB [pap]> insert into msg(id,title,name,content) values (1,'初來乍到','張三','分解到家');
Query OK, 1 row affected (0.09 sec)

MariaDB [pap]> insert into msg(id,title,name,content) values (2,'又來了','李四','見覅off');
Query OK, 1 row affected (0.03 sec)

MariaDB [pap]> insert into msg
    -> values
    -> (3,'馮家灣','幾覅哦','幾覅歐文'),
    -> (4,'范圍房','房價跟','飛機我啊'),
    -> (5,'房價而','佛教房','飛機瓦爾');
Query OK, 3 rows affected (0.13 sec)
Records: 3  Duplicates: 0  Warnings: 0

八、修改數據

MariaDB [pap]> update msg set id=2,content='房間愛我' where name='李四';
Query OK, 1 row affected (0.11 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [pap]> insert into msg values ();
Query OK, 1 row affected, 3 warnings (0.70 sec)

九、刪除某行數據

MariaDB [pap]> delete from msg where id=2;
Query OK, 1 row affected (0.12 sec)

十、查找數據

MariaDB [pap]> select id,title from msg;
+------+--------------+
| id   | title        |
+------+--------------+
|    1 | 初來乍到     |
|    3 | 馮家灣       |
|    4 | 范圍房       |
|    5 | 房價而       |
| NULL |              |
+------+--------------+
5 rows in set (0.00 sec)

MariaDB [pap]> select name,content from msg where id>2;
+-----------+--------------+
| name      | content      |
+-----------+--------------+
| 幾覅哦    | 幾覅歐文     |
| 房價跟    | 飛機我啊     |
| 佛教房    | 飛機瓦爾     |
+-----------+--------------+
3 rows in set (0.00 sec)

十一、刪除表

MariaDB [pap]> drop table class;
Query OK, 0 rows affected (0.11 sec)
MariaDB [pap]> create table class( id int primary key auto_increment,name varchar(10),age tinyint)charset utf8;
Query OK, 0 rows affected (0.14 sec)

MariaDB [pap]> insert into class (name,age) values ('zhangsan',25);
Query OK, 1 row affected (0.03 sec)

MariaDB [pap]> insert into class (name,age) values ('zhangsan',127);
Query OK, 1 row affected (0.10 sec)

MariaDB [pap]> insert into class (name,age) values ('zhangsan',-128);
Query OK, 1 row affected (0.11 sec)

十二、插入行

MariaDB [pap]> alter table class add age2 tinyint unsigned;
Query OK, 3 rows affected (0.12 sec)               
Records: 3  Duplicates: 0  Warnings: 0

MariaDB [pap]> insert into class(name,age,age2) values ('lisi',25,0);
Query OK, 1 row affected (0.04 sec)

MariaDB [pap]> alter table class add age3 tinyint(1);
Query OK, 4 rows affected (0.21 sec)               
Records: 4  Duplicates: 0  Warnings: 0

浮點型

MariaDB [pap]> create table goods(name varchar(10) not null default '',price float(6,2) not null default 0.00) charset=utf8;
Query OK, 0 rows affected (0.14 sec)

MariaDB [pap]> insert into goods(name,price) values ('跑步機',688.896);
Query OK, 1 row affected (0.10 sec)
MariaDB [pap]> alter table goods add bigprice float(9,2) not null default 0.0;
Query OK, 1 row affected (0.14 sec)                
Records: 1  Duplicates: 0  Warnings: 0

MariaDB [pap]> alter table goods add deciprice decimal(9,2) not null default 0.0;
Query OK, 1 row affected (0.07 sec)                
Records: 1  Duplicates: 0  Warnings: 0

MariaDB [pap]> insert into goods(name,bigprice,deciprice) values ('',1234567.23,1234567.23);
Query OK, 1 row affected (0.26 sec)

MariaDB [pap]> select * from goods;
+-----------+--------+------------+------------+
| name      | price  | bigprice   | deciprice  |
+-----------+--------+------------+------------+
| 跑步機    | 688.90 |       0.00 |       0.00 |
|           |   0.00 | 1234567.25 | 1234567.23 |
+-----------+--------+------------+------------+
2 rows in set (0.00 sec)

MariaDB [pap]> create table stu(name char(8) not null default '',waihao varchar(10) not null default '')charset utf8;
Query OK, 0 rows affected (0.11 sec)


year類型

MariaDB [pap]> create table y(ya year(4));
Query OK, 0 rows affected (0.12 sec)

MariaDB [pap]> insert into y values ('1901');
Query OK, 1 row affected (0.33 sec)

MariaDB [pap]> insert into y values ('2200');
Query OK, 1 row affected, 1 warning (0.00 sec)

MariaDB [pap]> insert into y values ('97');
Query OK, 1 row affected (0.09 sec)

MariaDB [pap]> insert into y values ('12');
Query OK, 1 row affected (0.44 sec)

date類型

MariaDB [pap]> create table d(title varchar(30),dt date)charset utf8;
Query OK, 0 rows affected (0.11 sec)

MariaDB [pap]> insert into d values ('開國大典','1994-10-01');
Query OK, 1 row affected (0.07 sec)

MariaDB [pap]> insert into d values ('世界末日','2012-02-30');
Query OK, 1 row affected, 1 warning (0.26 sec)

time類型

MariaDB [pap]> create table t(tm time);
Query OK, 0 rows affected (0.43 sec)

MariaDB [pap]> insert into t values ('13:34:56');
Query OK, 1 row affected (0.36 sec)

MariaDB [pap]> insert into t values ('13:60:00');
Query OK, 1 row affected, 1 warning (0.36 sec)

日期時間類型

MariaDB [pap]> create table user(name varchar(20) not null default '',regtime datetime not null default '1000-01-01 00:00:00')charset utf8;
Query OK, 0 rows affected (0.21 sec)

MariaDB [pap]> insert into user(name) values ('張三');
Query OK, 1 row affected (0.04 sec)

MariaDB [pap]> insert into user values ('李四','2012-02-22 14:28:36');
Query OK, 1 row affected (0.33 sec)

MariaDB [pap]> create table teacher(name varchar(20),gender tinyint)engine=innodb default charset utf8;
Query OK, 0 rows affected (0.36 sec)

MariaDB [pap]> insert into teacher values ('張',1);
Query OK, 1 row affected (0.10 sec)

MariaDB [pap]> create table t2(gender enum('男','女'))charset utf8;
Query OK, 0 rows affected (0.36 sec)

MariaDB [pap]> insert into t2 values ('男');
Query OK, 1 row affected (0.34 sec)

MariaDB [pap]> insert into t2 values ('女');
Query OK, 1 row affected (0.10 sec)

MariaDB [pap]> insert into t2 values ('春哥');
Query OK, 1 row affected, 1 warning (0.16 sec)

綜合練習

創建數據表

MariaDB [pap]> 
MariaDB [pap]> create table wl163(
    -> id int primary key auto_increment,
    -> name char(3) not null default '',
    -> age tinyint unsigned not null default 0,
    -> email varchar(30) not null default '',
    -> tel char(11) not null default '',
    -> salary decimal(7,2) not null default '1900.79',
    -> riqi date not null default '2018-12-15'
    -> )charset=utf8;
Query OK, 0 rows affected (0.13 sec)

插入數據

MariaDB [pap]> insert into wl163
    -> (name,age,email,tel,riqi)
    -> values
    -> ('陳心宇',21,'chenxinyu@yz.com','18912499740','2016-09-10');
Query OK, 1 row affected (0.10 sec)

MariaDB [pap]> insert into wl163
    -> values
    -> (3,'段宗?',21,'duanzonghe@ah.com','18951312252',1234.56,'2016-09-10');
Query OK, 1 row affected (0.10 sec)

MariaDB [pap]> insert into wl163
    -> (name,age,email)
    -> values
    -> ('薛懷',21,'xuehuai@ha.com'),
    -> ('楊其龍',21,'yangqilong@sh.com'),
    -> ('張遷',21,'zhangqian@sy.com'),
    -> ('王欣',21,'wangxin@ah.com');
Query OK, 4 rows affected (0.11 sec)
Records: 4  Duplicates: 0  Warnings: 0

修改數據

MariaDB [pap]> update wl163
    -> set
    -> tel='15161737044',
    -> salary=3999.99
    -> where id=4;
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 數據庫 數據庫介紹 之前通過IO流操作文件保存數據弊端1、效率低2、一般只能保存少量的數據3、只能保存文本數據 什...
    沉浮_0644閱讀 813評論 0 0
  • .數據庫 數據庫的發展: 文件系統(使用磁盤文件來存儲數據)=>第一代數據庫(出現了網狀模型,層次模型的數據庫)=...
    小Q逛逛閱讀 995評論 0 2
  • 第一節 HTML基礎 1、瀏覽器接介紹 瀏覽器是網頁運行的平臺,常見的有IE、火狐、谷歌 2、服務器介紹 (1)網...
    下頁luck閱讀 298評論 0 1
  • 人的一輩子可以說無時無刻不在做選擇題,出門上班,是坐公交呢還是坐地鐵,吃早點是吃包子呢還是吃油條,高考報志愿是報師...
    柒零曉閱讀 490評論 0 6
  • 城市里爆發了一場大游行,這是枕頭屆發起的聲勢浩大的抗議游行,枕頭們集體大罷工了!每家每戶的枕頭都離開各自的...
    Clearness閱讀 383評論 0 0