sql数据库的基本操作
生活随笔
收集整理的這篇文章主要介紹了
sql数据库的基本操作
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
命令行
1、顯示當(dāng)前數(shù)據(jù)庫服務(wù)器中的數(shù)據(jù)庫列表:mysql> SHOW DATABASES;
2、建立數(shù)據(jù)庫:mysql> CREATE DATABASE 庫名;
3、建立數(shù)據(jù)表:mysql> USE 庫名;mysql> CREATE TABLE 表名 (字段名 VARCHAR(20), 字段名 CHAR(1));
4、刪除數(shù)據(jù)庫:mysql> DROP DATABASE 庫名;
5、刪除數(shù)據(jù)表:mysql> DROP TABLE 表名;
6、將表中記錄清空:mysql> DELETE FROM 表名;
7、往表中插入記錄:mysql> INSERT INTO 表名 VALUES ("hyq","M");
8、更新表中數(shù)據(jù):mysql-> UPDATE 表名 SET 字段名1='a',字段名2='b' WHERE 字段名3='c';
9、用文本方式將數(shù)據(jù)裝入數(shù)據(jù)表中:mysql> load data local infile "d:/mysql.txt" into table 表名;
10、導(dǎo)入.sql文件命令:mysql> USE 數(shù)據(jù)庫名;mysql> source d:/mysql.sql;
11、命令行修改root密碼:mysql> update mysql.user set password=password('新密碼') where user='root';mysql> flush privileges;
12.修改密碼的三種方法:mysql>update user set password=password('123456') where user='joy_pen';mysql>flush privileges;mysql>set password for 'joy_oen'=password('123456');mysql>grant usage on *.* to 'joy_pen' identified by '123456';
1、創(chuàng)建數(shù)據(jù)庫
命令:create database <數(shù)據(jù)庫名> 例如:建立一個(gè)名為xhkdb的數(shù)據(jù)庫mysql> create database xhkdb;
2、顯示所有的數(shù)據(jù)庫
命令:show databases (注意:最后有個(gè)s)mysql> show databases;
3、刪除數(shù)據(jù)庫
命令:drop database <數(shù)據(jù)庫名> 例如:刪除名為 xhkdb的數(shù)據(jù)庫mysql> drop database xhkdb;
4、連接數(shù)據(jù)庫
命令: use <數(shù)據(jù)庫名> 例如:如果xhkdb數(shù)據(jù)庫存在,嘗試存取它:mysql> use xhkdb; 屏幕提示:Database changed
5、當(dāng)前選擇(連接)的數(shù)據(jù)庫mysql> select database();
6、當(dāng)前數(shù)據(jù)庫包含的表信息:mysql> show tables; (注意:最后有個(gè)s)
三、表操作,操作之前應(yīng)連接某個(gè)數(shù)據(jù)庫
1、建表
命令:create table <表名> ( <字段名1> <類型1> [,..<字段名n> <類型n>]);
mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default ''0'',
> degree double(16,2));
2、獲取表結(jié)構(gòu)
命令: desc 表名,或者show columns from 表名
mysql>DESCRIBE MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;
3、刪除表
命令:drop table <表名>
例如:刪除表名為 MyClass 的表 mysql> drop table MyClass;
4、插入數(shù)據(jù)
命令:insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )]
例如,往表 MyClass中插入二條記錄, 這二條記錄表示:編號(hào)為1的名為Tom的成績(jī)?yōu)?6.45, 編號(hào)為2 的名為Joan 的成績(jī)?yōu)?2.99,編號(hào)為3 的名為Wang 的成績(jī)?yōu)?6.5.
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
5、查詢表中的數(shù)據(jù)
1)、查詢所有行
命令: select <字段1,字段2,...> from < 表名 > where < 表達(dá)式 >
例如:查看表 MyClass 中所有數(shù)據(jù) mysql> select * from MyClass;
2)、查詢前幾行數(shù)據(jù)
例如:查看表 MyClass 中前2行數(shù)據(jù)
mysql> select * from MyClass order by id limit 0,2;
6、刪除表中數(shù)據(jù)
命令:delete from 表名 where 表達(dá)式
例如:刪除表 MyClass中編號(hào)為1 的記錄
mysql> delete from MyClass where id=1;
7、修改表中數(shù)據(jù):update 表名 set 字段=新值,… where 條件
mysql> update MyClass set name=''Mary'' where id=1;
8、在表中增加字段:
命令:alter table 表名 add 字段 類型 其他;
例如:在表MyClass中添加了一個(gè)字段passtest,類型為int(4),默認(rèn)值為0
mysql> alter table MyClass add passtest int(4) default ''0''
9、更改表名:
命令:rename table 原表名 to 新表名;
例如:在表MyClass名字更改為YouClass
mysql> rename table MyClass to YouClass;
更新字段內(nèi)容
update 表名 set 字段名 = 新內(nèi)容
update 表名 set 字段名 = replace(字段名,''舊內(nèi)容'',''新內(nèi)容'');
1、顯示當(dāng)前數(shù)據(jù)庫服務(wù)器中的數(shù)據(jù)庫列表:mysql> SHOW DATABASES;
2、建立數(shù)據(jù)庫:mysql> CREATE DATABASE 庫名;
3、建立數(shù)據(jù)表:mysql> USE 庫名;mysql> CREATE TABLE 表名 (字段名 VARCHAR(20), 字段名 CHAR(1));
4、刪除數(shù)據(jù)庫:mysql> DROP DATABASE 庫名;
5、刪除數(shù)據(jù)表:mysql> DROP TABLE 表名;
6、將表中記錄清空:mysql> DELETE FROM 表名;
7、往表中插入記錄:mysql> INSERT INTO 表名 VALUES ("hyq","M");
8、更新表中數(shù)據(jù):mysql-> UPDATE 表名 SET 字段名1='a',字段名2='b' WHERE 字段名3='c';
9、用文本方式將數(shù)據(jù)裝入數(shù)據(jù)表中:mysql> load data local infile "d:/mysql.txt" into table 表名;
10、導(dǎo)入.sql文件命令:mysql> USE 數(shù)據(jù)庫名;mysql> source d:/mysql.sql;
11、命令行修改root密碼:mysql> update mysql.user set password=password('新密碼') where user='root';mysql> flush privileges;
12.修改密碼的三種方法:mysql>update user set password=password('123456') where user='joy_pen';mysql>flush privileges;mysql>set password for 'joy_oen'=password('123456');mysql>grant usage on *.* to 'joy_pen' identified by '123456';
1、創(chuàng)建數(shù)據(jù)庫
命令:create database <數(shù)據(jù)庫名> 例如:建立一個(gè)名為xhkdb的數(shù)據(jù)庫mysql> create database xhkdb;
2、顯示所有的數(shù)據(jù)庫
命令:show databases (注意:最后有個(gè)s)mysql> show databases;
3、刪除數(shù)據(jù)庫
命令:drop database <數(shù)據(jù)庫名> 例如:刪除名為 xhkdb的數(shù)據(jù)庫mysql> drop database xhkdb;
4、連接數(shù)據(jù)庫
命令: use <數(shù)據(jù)庫名> 例如:如果xhkdb數(shù)據(jù)庫存在,嘗試存取它:mysql> use xhkdb; 屏幕提示:Database changed
5、當(dāng)前選擇(連接)的數(shù)據(jù)庫mysql> select database();
6、當(dāng)前數(shù)據(jù)庫包含的表信息:mysql> show tables; (注意:最后有個(gè)s)
三、表操作,操作之前應(yīng)連接某個(gè)數(shù)據(jù)庫
1、建表
命令:create table <表名> ( <字段名1> <類型1> [,..<字段名n> <類型n>]);
mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default ''0'',
> degree double(16,2));
2、獲取表結(jié)構(gòu)
命令: desc 表名,或者show columns from 表名
mysql>DESCRIBE MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;
3、刪除表
命令:drop table <表名>
例如:刪除表名為 MyClass 的表 mysql> drop table MyClass;
4、插入數(shù)據(jù)
命令:insert into <表名> [( <字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )]
例如,往表 MyClass中插入二條記錄, 這二條記錄表示:編號(hào)為1的名為Tom的成績(jī)?yōu)?6.45, 編號(hào)為2 的名為Joan 的成績(jī)?yōu)?2.99,編號(hào)為3 的名為Wang 的成績(jī)?yōu)?6.5.
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
5、查詢表中的數(shù)據(jù)
1)、查詢所有行
命令: select <字段1,字段2,...> from < 表名 > where < 表達(dá)式 >
例如:查看表 MyClass 中所有數(shù)據(jù) mysql> select * from MyClass;
2)、查詢前幾行數(shù)據(jù)
例如:查看表 MyClass 中前2行數(shù)據(jù)
mysql> select * from MyClass order by id limit 0,2;
6、刪除表中數(shù)據(jù)
命令:delete from 表名 where 表達(dá)式
例如:刪除表 MyClass中編號(hào)為1 的記錄
mysql> delete from MyClass where id=1;
7、修改表中數(shù)據(jù):update 表名 set 字段=新值,… where 條件
mysql> update MyClass set name=''Mary'' where id=1;
8、在表中增加字段:
命令:alter table 表名 add 字段 類型 其他;
例如:在表MyClass中添加了一個(gè)字段passtest,類型為int(4),默認(rèn)值為0
mysql> alter table MyClass add passtest int(4) default ''0''
9、更改表名:
命令:rename table 原表名 to 新表名;
例如:在表MyClass名字更改為YouClass
mysql> rename table MyClass to YouClass;
更新字段內(nèi)容
update 表名 set 字段名 = 新內(nèi)容
update 表名 set 字段名 = replace(字段名,''舊內(nèi)容'',''新內(nèi)容'');
轉(zhuǎn)載于:https://www.cnblogs.com/xcnpeng/p/7196781.html
總結(jié)
以上是生活随笔為你收集整理的sql数据库的基本操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Rusty String
- 下一篇: Oracle数据库创建表空间及用户授权