mysql复习-数据操作

  • 数据库复习(粗略整理)

对数据的操作

  一、添加

    方法1:insert into 表名(表列名1,表列名2,表列名3,...) values(表列名1的值,表列名2的值,表列名3的值,......)一一表名次序和值次序相对应

      insert into student(student_id,student_name,student_age) values(1,'张三',15)

    方法2:insert into 表名 values(表列名1的值,表列名2的值,表列名3的值,......)一一表名次序与数据库表名次序对应,并且全部列名都需要有对应得值

  •    insert into student values(2,'李四',16,null,null)

    二、查询

    方法1:select * from (表名) where (条件语句)一一查询全部数据 用*代替,并根据需要对查询条件使用使用以及添加查询条件

      select * from student

    方法2:select (表列名1,表列名2,表列名3,...) from (表名) where (条件语句) 可根据列名查询或者*查询,并根据需要对查询条件使用使用以及添加查询条件

      select student_id,student_age,student_name from student where id=0

  三、修改

    update 表名set 列名1='hn',列名2='其他' where 条件一一列名之间逗号分隔,条件根据需要决定是否使用以及添加

      update student set student_address='hn',student_subject='其他' where student_id=1

  四、删除

      delete from 表名 where 条件一一条件可根据需要决定是否需要和增加

      delete from student where student_id=2

对表的操作

  一、创建表

    CREATE TABLE 表名称 ( 列名称1 数据类型, 列名称2 数据类型, 列名称3 数据类型, .... )

      create table Persons(Id_p int,LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) )

    SQL 约束

      NOT NULL一一 不允许为空

        create table Persons(Id_p int not null,LastName varchar(255) not null, FirstName varchar(255), Address varchar(255), City varchar(255) )

      UNIQUE一一和 PRIMARY KEY 约束均为列或列集合提供了唯一性的保证。PRIMARY KEY 拥有自动定义的 UNIQUE 约束。每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。

        create table Persons(Id_p int not null,LastName varchar(255) not null, FirstName varchar(255), Address varchar(255), City varchar(255) , UNIQUE (Id_P))

      PRIMARY KEY一一约束唯一标识数据库表中的每条记录。主键必须包含唯一的值。主键列不能包含 NULL 值。每个表都应该有一个主键,并且每个表只能有一个主键。

        create table Persons(Id_p int not null,LastName varchar(255) not null, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (Id_P))

      FOREIGN KEY一一一个表中的 FOREIGN KEY 指向另一个表中的 PRIMARY KEY。

        create table Persons(Id_p int not null,LastName varchar(255) not null, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (Id_P),FOREIGN KEY (Id_P) REFERENCES Persons(Id_P))

      CHECK一一约束用于限制列中的值的范围。

        create table Persons(Id_p int not null,LastName varchar(255) not null, FirstName varchar(255), Address varchar(255), City varchar(255), CHECK (Id_P>0))

      DEFAULT一一约束用于向列中插入默认值。

        create table Persons(Id_p int not null,LastName varchar(255) not null, FirstName varchar(255), Address varchar(255), City varchar(255) DEFAULT 'Sandnes')

  二、ALTER TABLE:ALTER TABLE 语句用于在已有的表中添加、修改或删除列。

    在表中添加列一一ALTER TABLE 表名 ADD 添加行字段名 添加字段数据类型

      alter table books add otherTitle varchar(50)

    删除表中的列一一ALTER TABLE 表名 DROP COLUMN 行名

      alter table books drop column otherTitle

    改变表中列的数据类型一一ALTER TABLE table_name MODIFY COLUMN column_name datatype

      alter table books MODIFY COLUMN otherTitle int

  三、Auto-increment 会在新记录插入表中时生成一个唯一的数字。    

    定义一一声明主键,主键后标注Auto-increment

      声明主键( PRIMARY KEY (P_Id) ) 声明Auto-increment  (  P_Id int NOT NULL AUTO_INCREMENT )

      CREATE TABLE Persons ( P_Id int NOT NULL AUTO_INCREMENT, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (P_Id) )

    起始值设定一一默认地,AUTO_INCREMENT 的开始值是 1,每条新记录递增 1。

      ALTER TABLE Persons AUTO_INCREMENT=100

    表中插入新记录,我们不必为 其列规定值(会自动添加一个唯一的值)

      insert语句

  四、查询一个表中有多少个字段

    1>指定数据库一一SELECT COUNT(*) FROM information_schema. COLUMNS WHERE table_schema = '数据库名‘ AND table_name = '表名';

      SELECT COUNT(*) FROM information_schema. COLUMNS WHERE table_schema = 'study' AND table_name = 'books';

    2>默认数据库一一select count(*) from information_schema.COLUMNS where table_name='表名';--表名大小写均可

      select count(*) from information_schema.COLUMNS where table_name='books';

对数据库操作

  查询一个数据库中有多少张表一一SELECT COUNT(*) TABLES, table_schema FROM information_schema.TABLES WHERE table_schema = ‘数据库名’ GROUP BY table_schema;

    SELECT COUNT(*) TABLES, table_schema FROM information_schema.TABLES WHERE table_schema = 'study' GROUP BY table_schema;

  查询一个数据库中一共有多少个字段一一SELECT COUNT(column_name) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = ‘数据库名’;

    SELECT COUNT(column_name) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'study';

  查询一个数据库中的所有表和所有字段、字段类型及注释等信息一一SELECT TABLE_NAME, column_name, DATA_TYPE, column_comment FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = '数据库名' ;

    SELECT TABLE_NAME, column_name, DATA_TYPE, column_comment FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = 'study'

视图

  创建一一create view 表名 asselect * from  表;

    create view viewtable1 asselect * from  study;

  删除视图一一drop view 视图名

    drop view viewtable1

  修改视图一一alter view 表名 as select * from where .id > 20;

    alter view viewtable2 as select * from study where study.id > 20;

关键字、函数等不全部分

  参考W3school:https://www.w3school.com.cn/sql/sql_quickref.asp

  参考菜鸟教程:https://www.runoob.com/sql/sql-tutorial.html

@GF  仅供参考

猜你喜欢

转载自www.cnblogs.com/gwf2020/p/12123111.html
今日推荐